怎么办?帮帮我吧

P1162 填涂颜色

_orange_ @ 2023-01-06 23:00:42

#include<bits/stdc++.h>
using namespace std;
struct node{
    int x;
    int y;
};
char mp[501][501];
bool vis[501][501];
queue<node>q;
int n;
int main(){
    memset(vis,true,sizeof(vis));
    scanf("%d",&n);
    getchar();
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            mp[i][j]=getchar();
            getchar();
            vis[i][j]=false;
        }
    }
    for(int i=1;i<=n;i++){
        if(mp[1][i]=='0'){
            q.push(node{1,i});
            break;
        }
        if(mp[1][i]=='0'){
            q.push(node{i,1});
            break;
        }
        if(mp[1][n-i+1]=='0'){
            q.push(node{1,n-i+1});
            break;
        }
        if(mp[n-i+1][1]=='0'){
            q.push(node{n-i+1,1});
            break;
        }
    }
    while(!q.empty()){
        node nw=q.front();q.pop();
        if(mp[nw.x][nw.y]=='1'||mp[nw.x][nw.y]=='u')continue;
        if(mp[nw.x][nw.y]=='0')mp[nw.x][nw.y]='u';
        if(!vis[nw.x][nw.y+1]){
            vis[nw.x][nw.y+1]=true;
            q.push(node{nw.x,nw.y+1});
        }
        if(!vis[nw.x+1][nw.y]){
            vis[nw.x+1][nw.y]=true;
            q.push(node{nw.x+1,nw.y});
        }
        if(!vis[nw.x][nw.y-1]){
            vis[nw.x][nw.y-1]=true;
            q.push(node{nw.x,nw.y-1});
        }
        if(!vis[nw.x-1][nw.y]){
            vis[nw.x-1][nw.y]=true;
            q.push(node{nw.x-1,nw.y});
        }
    }
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            if(mp[i][j]=='1')printf("1");
            else if(mp[i][j]=='0')printf("2");
            else if(mp[i][j]=='u')printf("0");
            if(j!=n)printf(" ");
        }
        printf("\n");
    }
    return 0;
}

在本地完全可以过,一上洛谷就报too short


by QAQ_YTH @ 2023-02-17 22:38:46

too short是指答案太短,就是WA,纯属代码问题,不是洛谷的问题


by QAQ_YTH @ 2023-02-17 22:40:40

肯定是有一种情况mp[i][j]既不是1,也不是0,也不是u,以至于不输出


by _orange_ @ 2023-03-12 11:01:27

感谢


|