2024年3月10日牛客周赛补题

B、小红的小红矩阵构造

异或和: 是位运算并不是数学的异或

#include<bits/stdc++.h>

using namespace std;
const int N=110;
int n,m,x;
int a[N][N];
vector<int> b;
int main(){
    int sum=0;
    cin>>n>>m>>x;
    for(int i=0;i<n;i++)
        for(int j=0;j<m;j++)
        {
            cin>>a[i][j];
            sum+=a[i][j];
        }
    if(sum!=x) {
        cout<<"wrong answer";
        return 0;
    }
    
    //hang
    for(int i=0;i<n;i++){
        int h=0;
        for(int j=0;j<m;j++){
            h^=a[i][j];
        }
        b.push_back(h);
    }
    //lie
    for(int i=0;i<m;i++){
        int l=0;
        for(int j=0;j<n;j++){
            l^=a[j][i];
        }
        b.push_back(l);
    }
    int y=0;
    for(int i=0;i<b.size()-1;i++){
        if(b[i]!=b[i+1]) y++;
    }
    if(y==0) cout<<"accepted";
    else cout<<"wrong answer";
    return 0;
}

C、小红的白色字符串

思路:第一位不用管,遇到大写就删然后多跳一位 

#include<bits/stdc++.h>

using namespace std;

int main(){
    string a;
    cin>>a;
    bool st=true;
    int cnt=0;
    for(int i=1;i<a.length();i++){
        if(a[i]>='A'&&a[i]<='Z') {
            cnt++;
            i++;
        }
    } 
    cout<<cnt;
    return 0;
}

D、小红走矩阵 

bfs

#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int> PII;
string s[1010];
int d[1010][1010];

int main(){
    int n,m;
    cin>>n>>m;
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++)d[i][j]=1e9;
    }
    for(int i=0;i<n;i++)cin>>s[i];
    
    queue<PII>q;
    q.push({0,0});
    d[0][0]=0;
    int dx[4]={0,1,0,-1},dy[4]={1,0,-1,0};
    while(!q.empty()){
        PII t=q.front();
        q.pop();
        for(int i=0;i<4;i++){
            int x=t.first+dx[i],y=t.second+dy[i];
            if(x<0||x>=n||y<0||y>=m)continue;
            if(s[x][y]!=s[t.first][t.second]&&d[x][y]>d[t.first][t.second]+1){
                d[x][y]=d[t.first][t.second]+1;
                q.push({x,y});
            }
        }
    }
    if(d[n-1][m-1]>8e8)cout<< -1;
    else cout<<d[n-1][m-1];
    return 0;
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值