悬赏关注
  • 板块灌水区
  • 楼主hkl99
  • 当前回复1
  • 已保存回复1
  • 发布时间2024/9/17 22:38
  • 上次更新2024/9/18 16:19:40
查看原帖
悬赏关注
770439
hkl99楼主2024/9/17 22:38

题目点这

为啥有错???

请欣赏结果

#include<bits/stdc++.h>
using namespace std;
int n,m,a[1001][1001],x,y,fx[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
bool b[1001][1001];
struct node{
	int x,y,z;
};
int bfs(int x,int y,int z){
	int cnt=0;
	memset(b,true,sizeof(b));
	queue<node>q;
	node t;
	t.x=x;
	t.y=y;
	t.z=z;
	q.push(t);
	while(!q.empty()){
		t=q.front();
		q.pop();
		int nx,ny;
		for(int i=0;i<4;i++){
			node tt;
			tt.x=t.x+fx[i][0];
			tt.y=t.y+fx[i][1];
			if(tt.x>=1&&tt.y>=1&&tt.x<=n&&tt.y<=n&&b[tt.x][tt.y]==true&&a[tt.x][tt.y]==(0-t.z)){
				cnt++;
				b[tt.x][tt.y]=false;
				tt.z=0-t.z;
				q.push(tt);
			}
		}
	}
	return cnt;
}
int main(){
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++){
		for(int j=1;j<=n;j++){
			scanf("%1d",&x);
			a[i][j]=x;
			if(x==0){
				a[i][j]=-1;
			}
		}
	}
	for(int i=1;i<=m;i++){
		cin>>x>>y;
		printf("%d\n",bfs(x,y,a[x][y]));
	}
	return 0;
}

2024/9/17 22:38
加载中...