求助dalao,蒟蒻已找出错误,可不知道怎么改,求助!!!
  • 板块P1162 填涂颜色
  • 楼主leo888
  • 当前回复3
  • 已保存回复3
  • 发布时间2021/7/28 11:58
  • 上次更新2023/11/4 13:04:10
查看原帖
求助dalao,蒟蒻已找出错误,可不知道怎么改,求助!!!
543527
leo888楼主2021/7/28 11:58

错误出现在bfs里面,这个rear指针遍历不下去,导致vis数组判断不了,所以求大佬帮助!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

#include<bits/stdc++.h>
using namespace std;
int n,head,rear,i, j;
const int fx[4]={1,0,-1,0};
const int fy[4]={0,1,0,-1};
int a[10001][10001];
bool vis[1001][1001];
struct node{
	int x1;
	int y1;
	int step;
}q[1001];
int bfs(int x,int y){
	head = 0, rear = 0;
	q[rear].x1=x;
	q[rear].y1=y;
	q[rear].step=0;
	rear++;
	vis[x][y]=1;
	while(head<rear){
		node now=q[head];
		head++;
		for(int k=0;k<4;k++){
			int tx=now.x1+fx[k];
			int ty=now.y1+fy[k];
			if(tx>=0 && tx<n && ty>=0 && ty<n && vis[tx][ty]==0 && a[tx][ty]==0){
				cout << tx << ' ' << ty << endl;
				vis[tx][ty]=1;
				q[rear].x1=tx;
				q[rear].y1=ty;
				rear++;	
			}
		}
	}
}
int main(){
	int n;
	cin>>n;
	for(i=0;i<n;i++){
		for(j=0;j<n;j++){
			cin>>a[i][j];
		}
	}
	for(i=0;i<n;i++){
		for(j=0;j<n;j++){
			if(a[i][j]==0 && (i==0 || j==0 || i==n-1 || j==n-1)){
				bfs(i,j);
			}else continue;
		}
	}
	for(i=0;i<n;i++){
		for(j=0;j<n;j++){
			if(vis[i][j]==1 || a[i][j]==1)cout<<a[i][j]<<' ';
			else cout<<2<<' ';
		}
		cout<<endl;
	}
	return 0;
}
2021/7/28 11:58
加载中...