re了谢谢,求大佬debug
  • 板块P1141 01迷宫
  • 楼主Jared0503
  • 当前回复0
  • 已保存回复0
  • 发布时间2024/1/28 22:15
  • 上次更新2024/1/29 09:19:16
查看原帖
re了谢谢,求大佬debug
1074812
Jared0503楼主2024/1/28 22:15

代码献上

#include<bits/stdc++.h>
using namespace std;
struct node{
	int x,y;
};
const int N=1010;
int cell[N][N];
int n,m; 
int bfs(int sx, int sy) {
	int ans=0;
	queue<node> q;
	q.push({sx,sy});  //起点入队 
	cell[sx][sy]=2;
	while(!q.empty()) 
	{
		node t=q.front();  //取出队头元素 
		q.pop();  //弹出队头
		int x=t.x,y=t.y;
		//枚举(x,y)上下左右四个方向		
		for(int i=0;i<4;i++) {
			for(int xx=x-4;xx<=x+4;xx++)
			{
				for(int yy=y-4;yy<=y+4;yy++)
				{
					if(!cell[x][y])
					{
						if(cell[xx][yy]&&cell[xx][yy]!=2)
						{
							if(xx>=1&&xx<=n&&yy>=1&&yy<=m) {
								cell[xx][yy]=2;
								q.push({xx, yy});  //添加到队尾 
								ans++;
							}
						}
					}
					else 
					{
						if(!cell[xx][yy]) 
						{
							if(xx>=1&&xx<=n&&yy>=1&&yy<=m) {
								cell[xx][yy]=2;
								q.push({xx, yy});  //添加到队尾 
								ans++; 
							}
						}
						else continue;
					}
				}
			}
			//保证(xx,yy)在边界之内 
			
		}
	}
	return ans;
}
int main() {
	cin>>n>>m;
	for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) cin>>cell[i][j];
	for(int k=0;k<m;k++) 
	{
		int i,j;
		cin>>i>>j;
		cout<<bfs(i,j);
	}
//	for(int i=1;i<=n;i++) {
//		for(int j=1;j<=m;j++) {
//			if(cell[i][j] != '0') {
//				ans ++;
//				bfs(i, j);
//			}
//		}
//	}
	return 0;
}
2024/1/28 22:15
加载中...