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 chenxuan123456789 at 2012-07-29 14:25:10 on Problem 1979
#include <stdio.h>
int dx[4]={1,0,0,-1};
int dy[4]={0,1,-1,0};
int times,r,c;
char map[21][21];
void dfs(int s,int e)
{
	int i,tempx,tempy;
	times++;
	map[s][e]='#';
	for(i=0;i<4;i++)
	{
		tempx=s+dx[i];
		tempy=e+dy[i];
		if(tempx>=1&&tempx<=r&&tempy>=1&&tempy<=c&&map[tempx][tempy]=='.')
			dfs(tempx,tempy);
	}
}
int main()
{
 int i,j,sx,sy;
 while(scanf("%d%d",&c,&r)!=EOF&&r&&c)
 {
	 for(i=1;i<=r;i++)
	 {
		 getchar();
		 for(j=1;j<=c;j++)
		 {
			 scanf("%c",&map[i][j]);
			 if(map[i][j]=='@')
			 {
				 sx=i;
				 sy=j;
			 }
		 }
	 }
	 times=0;
	 dfs(sx,sy);
	 printf("%d\n",times);
 }
 return 1;
 }

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