牛客周赛 Round 36

牛客周赛 Round 36

A.小红的数位删除 https://ac.nowcoder.com/acm/contest/76609/A

#include <bits/stdc++.h>
using namespace std;

int main(){
    int n;
    cin >> n;
    for(int i = 1; i <= 3; i++) n /= 10;
    cout << n;
    return 0;
}

B.小红的小红矩阵构造https://ac.nowcoder.com/acm/contest/76609/B

异或,两数的二进制相同输出0,不同输出1

两次循环分别固定行和列

#include <bits/stdc++.h>
using namespace std;

const int N = 110;
set<int> q;
int n, m, x, sum = 0;
int g[N][N];
bool flag = 1;

int main(){
    cin >> n >> m >> x;
    
    for(int i = 0; i < n; i++){
        for(int j = 0; j < m; j++){
            cin >> g[i][j];
            sum += g[i][j];
        }
    }

    if(sum != x) flag = 0;

    for(int i = 0; i < n; i++){
        int temp = 0;
        for(int j = 0; j < m; j++){
            temp ^= g[i][j];
        }
        q.insert(temp);
    }

    for(int i = 0; i < m; i++){
        int temp = 0;
        for(int j = 0; j < n; j++){
            temp ^= g[j][i];
        }
        q.insert(temp);
    }
    
    if(q.size() != 1) flag = 0;

    if(flag) puts("accepted");
    else puts("wrong answer");
    return 0;
}

C.小红的白色字符串 https://ac.nowcoder.com/acm/contest/76609/C

规律是大写字母的前一个格为空白

#include <bits/stdc++.h>
using namespace std;

string s;
int len, ans = 0;
bool space[200005];

int main(){
    cin >> s;
    len = s.size();
    
    for(int i = 1; i < len; i++){
        if(s[i] < 'a' && !space[i - 1]){
            ans ++;
            space[i] = 1;
        }
    }
    cout << ans;
    return 0;
}

D.小红走矩阵 https://ac.nowcoder.com/acm/contest/76609/D

bfs,记录上一格的字符要用t.first & t.second

#include <bits/stdc++.h>
using namespace std;

typedef pair<int, int> PII;
const int N = 1010;
int n, m, ans = 0;
char g[N][N];
bool st[N][N];
int f[N][N];
int dx[4] = {0, 0, -1, 1}, dy[4] = {-1, 1, 0, 0};

void bfs(int a, int b){
    st[a][b] = true;
    queue<PII> q;
    q.push({a, b});

    while(q.size()){
        PII t = q.front();
        q.pop();
        for(int i = 0; i < 4; i++){
            int x = dx[i] + t.first, y = dy[i] + t.second;
            if(x >= 0 && x < n && y >= 0 && y < m && !st[x][y] && g[x][y] != g[t.first][t.second]){
                f[x][y] = f[t.first][t.second] + 1;
                st[x][y] = true;
                q.push({x, y});
            }
        }
    }
}

int main(){
    memset(f, 0, sizeof f);
    memset(st, 0, sizeof st);
    cin >> n >> m;
    
    for(int i = 0; i < n; i++)
        for(int j = 0; j < m; j++)
            cin >> g[i][j];
    
    bfs(0, 0);

    if(st[n - 1][m - 1]) cout << f[n - 1][m - 1];
    else cout << "-1";
    return 0;
}
  • 9
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值