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

简单的DFS,不过内存用得较多,有什么办法可减少内存的使用?(附代码)

Posted by KosonLau at 2007-05-18 22:08:53 on Problem 1111
#include<string>
#include<stack>
#include<cstdio>
using namespace std;

char image[21][21];
int flag[21][21];
int move[8][2]={ {-1,0},{-1,1},{0,1},{1,1},{1,0},{1,-1},{0,-1},{-1,-1} };
int sum,row,col,rr,cc;

void dfs(int r,int c){
	flag[r][c]=1;
	for(int i=0;i<8;i++){
		int x=r+move[i][0];
		int y=c+move[i][1];
		if(x>=0&&x<row&&y>=0&&y<col){
			if(image[x][y]=='X'&&!flag[x][y]) 
				dfs(x,y);
			else if(image[x][y]=='.'&&(x==r||y==c)) 
					sum++;
		}else{
			if(x==r||y==c) 
				sum++; 
		}	
	}
	return;
}
int main(){
	while(scanf("%d%d%d%d",&row,&col,&rr,&cc)&&row!=0){
		for(int i=0;i<row;i++)
			scanf("%s",image[i]);
		memset(flag,0,sizeof(flag));
		sum=0;
		dfs(rr-1,cc-1);
		printf("%d\n",sum);
	}
	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