记录第一篇博客

A签到、B小思维、C bfs(),注意需要和初始位置的值相同、D数学(找规律) E,F待补,比赛时打的暴力

A

#include<bits/stdc++.h>

using namespace std;

string a;

int main()
{
    cin >> a;
    if(a.size() != 2)
    {
        cout << "No" << endl;
        return 0;
    }
    if(a[0] != a[1]) cout << "No" << endl;
    else cout << "Yes" << endl;
    
    return 0;
    
}

B

初始状态不是回文数,修改一个数后的状态是回文数,那么初始状态有且仅有一对数不相等

#include<bits/stdc++.h>
 
using namespace std;
 
const int N = 1050;
int a[N], n, k;
int res = 0, cnt = 0;
 
int main()
{
    cin >> n >> k;
    for(int i = 0; i < n; i ++ ) cin >> a[i];
     
    for(int i = 0; i < n; i ++ )
    {
        int t = i + k;
        if(t > n) break;
        for(int l = i, r = t - 1; l < r; l ++, r --  )
            if(a[l] == a[r])
                cnt += 2;//记录相同的对数
                
        //判断是否有且仅有一对数不满足
        //奇数时中间的数单列,则特殊处理
        if(k % 2)
        {
            if(cnt + 3 == k)
                res ++ ;
        }
        else
        {
            if(cnt + 2 == k)
                res ++ ;
        }
        cnt = 0;
    }
     
    cout << res << endl;
}

C

#include<bits/stdc++.h>
 
using namespace std;
 
const int N = 105;
typedef pair<int, int> PII;
 
int a[N][N];
int n, m;
int t;
queue<PII> q;
 
 
bool bfs(int fin )
{	//表示向右,与向下
    int dx[2] = {0, 1}, dy[2] = {1, 0};
    q.push({0, 0});
     
    while(q.size())
    {
        auto tt = q.front();
        if(tt.first == n - 1 && tt.second == m - 1 )
            return true;
        q.pop();
         
        for(int i = 0; i < 2; i ++ )
        {
            int x = tt.first + dx[i], y = tt.second + dy[i];
            if(x < n && y < m && a[x][y] == fin)
                q.push({x, y});
        }
    }
     
    return false;
}
 
int main()
{
    cin >> t;
    while(t -- )
    {
        cin >> n >> m;
         
        for(int i = 0; i < n; i ++ )
            for(int j = 0; j < m; j ++ )
                cin >> a[i][j];
        int fin = a[0][0];
        
        if(bfs(fin)) cout << "Yes" << endl;
        else cout << "No" << endl;
        //清空
        while(q.size()) q.pop();
    }
     
    return 0;
}

D

只要满足等于x即可,那么我们可以找规律去凑数。既然是要凑数,那么先凑1为主

#include<bits/stdc++.h>
 
using namespace std;
 
int main()
{
    int x;
    cin >> x;
    int t = 1 - x;
    if(x == 1) //因为我是1为主,对x == 1时a[1][2] == 0,不符题意,则特殊处理
    {
        printf("2 1 1\n2 2 1\n1 1 1\n");
        return 0;
    }
    printf("2 1 1\n1 1 %d\n1 1 1", t);
     
    return 0;
}