求助!!求大佬帮看

P1141 01迷宫

tushanyuan @ 2024-08-29 19:23:34

求大佬帮看

#include<bits/stdc++.h>
using namespace std;
string s[100010];
int n, m, ex, ey, lx, ly, sx, sy, flase = -1, ans[10000][10000],sum=0,t=1;
int dx[4] = {0, 0, 1, -1}, dy[4] = {1, -1, 0, 0}, na[1000][1000];
struct node {
    int x, y; //x轴 y轴
};
void bfs(int x, int y) {
    queue<node>q;//建立队列 
    node a;
    a.x = x;
    a.y = y;
    q.push(a);//输入起点
    na[x][y] = 1; //标记起点已走过 
    while (!q.empty()) {
        node now;
        now = q.front();
        q.pop();
        sum++;
        for (int i = 0; i <= 4; i++) {
            int xx = now.x + dx[i];//x轴+1 
            int yy = now.y + dy[i];//y轴+1 
            if (xx >= 1 && yy >= 1 && xx <= n && yy <= m && na[xx][yy] != 2 && na[xx][yy]!=na[now.x][now.y])//是否越界或是否走过 
            {
                na[xx][yy] = t;//标记为t 
                node al;
                al.x = xx;
                al.y = yy;
                q.push(al);//插入al 
            }
        }
    }
}
int main() {
    cin >> n >> m;
    for (int i = 1; i <= n; i++) 
    {
        for(int j=1;j<=m;j++)
        {
            cin >> s[i][j];
            bfs(i,j);
            ans[i][j]=sum;//将ans[i][j]中的值存入sum中 
            t++;
        }

    }
    for(int i=1;i<=m;i++)
    {
        cin>>sx>>sy;//输入起点的x与y轴 
        cout<<ans[t];//输出ans[t]的值 
    }
}

不知道哪里错了


by Ke9_qux @ 2024-08-29 19:32:01

@tushanyuan 您的输出甚至与输入的sx和sy无关


by Ke9_qux @ 2024-08-29 19:33:05

@tushanyuan bfs应该在整张图全输入完之后进行


by tushanyuan @ 2024-08-29 19:47:43

#include<bits/stdc++.h>
using namespace std;
char s[1010][1010];
int n, m, ex, ey, lx, ly, sx, sy, flase = -1, ans[1000010],sum=0,t=1;
int dx[4] = {0, 0, 1, -1}, dy[4] = {1, -1, 0, 0}, na[1010][1010];
struct node {
    int x, y; //x轴 y轴
};
void bfs(int x, int y) {
    queue<node>q;//建立队列 
    node a;
    a.x = x;
    a.y = y;
    q.push(a);//输入起点
    na[x][y] = t; //标记起点已走过 
    while (!q.empty()) {
        node now;
        now = q.front();
        q.pop();
        sum++;
        for (int i = 0; i < 4; i++) {
            int xx = now.x + dx[i];//x轴+1 
            int yy = now.y + dy[i];//y轴+1 
            if (xx >= 1 && yy >= 1 && xx <= n && yy <= n && na[xx][yy]==0 && na[xx][yy]!=na[now.x][now.y])//是否越界或是否走过 
            {
                na[xx][yy] = t;//标记为t 
                node al;
                al.x = xx;
                al.y = yy;
                q.push(al);//插入al 
            }
        }
    }
}
int main() {
    cin >> n >> m;
    for (int i = 1; i <= n; i++) 
    {
        for(int j=1;j<=n;j++)
        {
            cin>>s[i][j]; 
        }
    }
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            if(na[i][j]==0)
            {
            bfs(i,j);
            ans[na[i][j]]=sum;//将ans中的值存入sum中 
            t++;
            sum=0;
            }
        }
    }
    for(int i=1;i<=m;i++)
    {
        cin>>sx>>sy;//输入起点的x与y轴 
        cout<<ans[na[sx][sy]]<<endl;
    }
}

是这样吗?


|