Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register
欢迎参加IJCAI 2020麻将智能体竞赛,大奖等你拿!Welcome to IJCAI 2020 Mahjong AI competition with amazing prizes! | 北京大学《ACM/ICPC大学生程序设计竞赛训练》暑期课面向全球招生!

用bfs把相邻的1改成0并且统计数量,为什么re了0.0

Posted by qq2567722641 at 2019-10-02 13:04:08 on Problem 3494
#include<iostream>
#include<queue>
#include<cstring>
using namespace std;

const int maxn=2000+10;
int a[maxn][maxn];
int vis[maxn][maxn];
int dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
struct node{
	int x,y;
	node(int a,int b):x(a),y(b){}
};
int bfs(int x,int y){
	int s=1;
	queue<node>st;
	vis[x][y]=1;
	st.push(node(x,y));
	while(!st.empty()){
		node now=st.front();
		st.pop();
		for(int i=0;i<4;i++){
			int fx=now.x+dir[i][0];
			int fy=now.y+dir[i][1];
			if(a[fx][fy]==1&&!vis[fx][fy]){
				vis[fx][fy]=1;
				s++;
				//cout<<fx<<" :"<<fy<<endl;
				a[fx][fy]=0;
				st.push(node(fx,fy));
			}
		}
	}
	return s;
}
int main(){
	int n,m;
	while(scanf("%d%d",&n,&m)!=EOF){
		memset(a,0,sizeof(a));
		memset(vis,0,sizeof(vis));
		for(int i=0;i<n;i++){
			for(int j=0;j<m;j++){
				scanf("%d",&a[i][j]);
			}
		}
		int _max1=0;
		for(int i=0;i<n;i++){
			for(int j=0;j<n;j++){
				if(a[i][j]==1){
					_max1=max(_max1,bfs(i,j));
				}
			}
		}
		printf("%d\n",_max1);
	}
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator