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

一次AC了

Posted by zpdlut at 2010-08-17 12:29:48 on Problem 1562
#include<iostream>
#include<queue>
using namespace std;
int set[101][101];
int visited[101][101];
int result;
struct fn{
	int row;
	int col;
};
queue<fn> q;
int r,c;
void bfs(int i,int j){
	fn f;
	f.row = i;
	f.col = j;
	q.push(f);
	int move[8][2]={{0,1},{0,-1},{1,0},{-1,0},{-1,-1},{-1,1},{1,-1},{1,1}};
	while(!q.empty())
	{
		fn temp = q.front();
	
		q.pop();
		for(int i=0;i<8;i++)
		{	
			int x = temp.row;
			int y = temp.col;
			x = x+move[i][0];
			y = y+move[i][1];
			if(x<0||x>=r||y<0||y>=c||set[x][y]==0||visited[x][y])
				continue;
			else
			{
				visited[x][y]=1;
				f.row = x;
				f.col = y;
				q.push(f);
			}
				
		}

	}
}
int main()
{

	char ch;
	
	while(cin>>r>>c&&r)
	{
		result = 0;
		memset(set,0,sizeof(set));
		memset(visited,0,sizeof(visited));
		for(int i=0;i<r;i++){
			for(int j=0;j<c;j++){
				cin>>ch;
				if(ch != '*')
					set[i][j] = 1;
			}
		}
		for(int i=0;i<r;i++){
			for(int j=0;j<c;j++){
				if(set[i][j]==1&&visited[i][j]==0)
				{
					result++;
					visited[i][j]=1;
					bfs(i,j);
				}
			}
		}
		cout<<result<<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