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

用队列和BFS,第一次TL了,第二次AC

Posted by zpdlut at 2010-08-09 13:46:29 on Problem 3620
#include<iostream>
#include<queue>
using namespace std;
#define MAX 101
int set[MAX][MAX];
int visited[MAX][MAX];
int step[MAX][MAX];
int move[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
int r,c,k;
int result;
struct farm
{
	int r,c;
};
queue<farm> q;
void BFS(int x,int y)
{	

	visited[x][y]=1;
	farm m;
	m.r =x;
	m.c =y;
	q.push(m);
	step[x][y] = 1;
	result = 1 ;
	while(!q.empty()){
		farm temp = q.front();
		q.pop();
		for(int i=0;i<4;i++)
		{
			x = temp.r;
			y = temp.c;
			x+=move[i][0];
			y+=move[i][1];
			if(x>=1&&x<=r&&y>=1&&y<=c)
			{
				if(set[x][y]&&!visited[x][y]){
					visited[x][y]=1;
					result++;
					m.r = x;
					m.c = y;
					q.push(m);
					
				}
			}

		}
	}

}
int main()
{
	
	cin>>r>>c>>k;
	int a,b;
	int final = 0;
	for(int i=0;i<k;i++){
		cin>>a>>b;
		set[a][b] = 1;
	}
	for(int i=1;i<=r;i++){
		for(int j=1;j<=c;j++){
			if(set[i][j]==0||visited[i][j]==1)	
				continue;
			
			BFS(i,j);
			if(result > final)
				final = result;

		}
	}
	cout<<final<<endl;
	return 0;
}

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