社区讨论
dalao帮看一下QAQ,WA了第二个点。。。
P114101迷宫参与者 3已保存回复 8
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 8 条
- 当前快照
- 1 份
- 快照标识符
- @mi6ywhx4
- 此快照首次捕获于
- 2025/11/20 13:05 4 个月前
- 此快照最后确认于
- 2025/11/20 13:05 4 个月前
CPP
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
using namespace std;
const int MAXN=2001;
int n,m,ans=0,d=0;
int f[MAXN][MAXN];
int book[MAXN][MAXN];
int cnt[5000001]={0};
int xx[5]={0,1,0,-1,0};
int yy[5]={0,0,1,0,-1};
bool vist[MAXN][MAXN];
queue<int>q;
queue<int>p;
void bfs(int sx,int sy)
{
int x,y,nx,ny;
book[sx][sy]=d;
vist[sx][sy]=true;
q.push(sx);
p.push(sy);
while(!q.empty())
{
x=q.front();
y=p.front();
q.pop();
p.pop();
for(int t=1;t<=4;t++)
{
nx=x+xx[t];ny=y+yy[t];
if(nx>=1&&nx<=n&&ny>=1&&ny<=n&&f[nx][ny]!=f[x][y]&&!vist[nx][ny])
{
q.push(nx);
p.push(ny);
vist[nx][ny]=true;
ans++;
book[nx][ny]=d;
}
}
}
}
int main()
{
//freopen("text.in","r",stdin);
// freopen("text.out","w",stdout);
memset(vist,false,sizeof(vist));
memset(book,0,sizeof(0));
scanf("%d %d\n",&n,&m);
for(int i=1;i<=n;i++)
{
char ch;
for(int j=1;j<=n;j++)
{
scanf("%c",&ch);
f[i][j]=ch-'0';
}
scanf("\n");
}
for(int i=1;i<n;i++)
{
for(int j=1;j<n;j++)
{
if(book[i][j]==0)
{
ans=0;
d++;
bfs(i,j);
cnt[d]=ans+1;
}
}
}
for(int i=1;i<=m;i++)
{
int a,b;
scanf("%d %d\n",&a,&b);
printf("%d\n",cnt[book[a][b]]);
}
return 0;
}
回复
共 8 条回复,欢迎继续交流。
正在加载回复...