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

深搜简单吧!如果改广搜,怎么改?

Posted by zhb_msqx at 2007-08-23 15:47:19 on Problem 1562
#include <iostream>
#include <fstream>
using namespace std;

int row,col;
char grid[110][110];

int mark[110][110];

int dx[8]={1,-1,0,0,1,1,-1,-1};
int dy[8]={0,0,1,-1,1,-1,1,-1};

bool bond(int x,int y){
	if(x>=0&&x<row&&y>=0&&y<col)return true;
	return false;
}

void search(int x,int y){
	for(int i=0;i<8;i++){
		int curx=x+dx[i];
		int cury=y+dy[i];
		if(bond(curx,cury)&&mark[curx][cury]==0&&grid[x][y]=='@'){
			mark[curx][cury]=1;
			search(curx,cury);
		}
	}
}

void main(){
//	ifstream cin("data.txt");
	while(1){
		cin>>row>>col;
		if(row==0&&col==0)break;
		int plot=0;
		for(int i=0;i<row;i++){
			for(int j=0;j<col;j++){
				cin>>grid[i][j];
				if(grid[i][j]=='@')plot++;
			}
		}
		if(plot==0)cout<<0<<endl;
		else{
			memset(mark,0,sizeof(mark));
			int res=0;
			for(int i=0;i<row;i++){
				for(int j=0;j<col;j++){	
					if(grid[i][j]=='@'&&mark[i][j]==0){
						res++;
						search(i,j);
					}
				}		
			}
			cout<<res<<endl;
		}
	}

}

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