WA一个点求助

P1162 填涂颜色

respect_lowsmile @ 2022-04-17 08:35:53

我是用广搜做的,测试点4WA了

#include<iostream>
#include<queue> 
using namespace std;
int map[31][31];
int vis[31][31];
int n;
int dx[4]={1,0,0,-1};
int dy[4]={0,1,-1,0};
struct point
{
    int x,y;
};
point now,en;
queue<point> q;
void bfs(int x,int y)
{
    now.x=x;now.y=y;
    vis[x][y]=1;
    if(map[x][y]==0) map[x][y]=3;
    q.push(now);
    while(!q.empty())
    {
        now=q.front();
        q.pop();
        for(int i=1;i<=n;++i)
          {
            en.x=now.x+dx[i];
            en.y=now.y+dy[i];
            if(en.x>=1&&en.x<=n&&en.y>=1&&en.y<=n&&vis[en.x][en.y]==0&&map[en.x][en.y]==0)
              {
                vis[en.x][en.y]=1;
                map[en.x][en.y]=3;
                q.push(en);
              }
          }
    }
}
int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;++i)
      for(int j=1;j<=n;++j)
            scanf("%d",&map[i][j]);
    for(int i=1;i<=n;++i)
      {
        if(map[1][i]==0) bfs(1,i);
        if(map[i][1]==0) bfs(i,1);
        if(map[n][i]==0) bfs(n,i);
        if(map[i][n]==0) bfs(i,n);
      }
    for(int i=1;i<=n;++i)
      {
        for(int j=1;j<=n;++j)
          {
            if(map[i][j]==0) printf("2 ");
            if(map[i][j]==1) printf("1 ");
            if(map[i][j]==3) printf("0 ");
            }
        printf("\n");
      }
    return 0;
}

by respect_lowsmile @ 2022-04-17 15:05:47

好了,不用大家破案了,本蒟蒻发现控制方向的循环打错了,已经AC了(不过为什么没人


|