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

各位大牛看一看?WHY WA

Posted by zhouxiaozi at 2009-04-08 21:32:48 on Problem 1573
#include <stdio.h>
#include <iostream>
#include <string.h>

char s[15][15];
int use[15][15];
int ans,n,m,flag,a[15],b[15];
void dfs(int x,int y)
{
	use[x][y]++;
	ans++;
	a[ans]=x;
	b[ans]=y;
	if(s[x][y]=='W'&&flag==0)
	{
		if(y-1<0)
		{
			flag=1;
		}
		else if(y-1>=0&&use[x][y-1]==0)
			dfs(x,y-1);
		else 
		{
			use[x][y-1]++;
			//printf("%d %d %d\n",x,y-1,use[x][y-1]);
			flag=2;
		}
	}
	if(s[x][y]=='S'&&flag==0)
	{
		if(x+1>=n)
		{
			flag=1;
		}
		else if(x+1<n&&use[x+1][y]==0)
			dfs(x+1,y);
		else 
		{
			use[x+1][y]++;
			//printf("%d %d %d\n",x+1,y,use[x+1][y]);
			flag=2;
		}
	}
	if(s[x][y]=='E'&&flag==0)
	{
		if(y+1>=m)
		{
			flag=1;
		}
		else if(y+1<m&&use[x][y+1]==0)
			dfs(x,y+1);
		else
		{
			use[x][y+1]++;
		//	printf("%d %d %d\n",x,y+1,use[x][y+1]);
			flag=2;
		}
	}
	if(s[x][y]=='N'&&flag==0)
	{
		if(x-1<0)
		{
			flag=1;
		}
		else if(x-1>=0&&use[x-1][y]==0)
			dfs(x-1,y);
		else 
		{
			use[x-1][y]++;
			//printf("%d %d %d\n",x-1,y,use[x-1][y]);
		    flag=2;
		}
	}
}
int main()
{
	int i,k,x,y;
	while(scanf("%d%d%d",&n,&m,&k)!=EOF)
	{
		if(n==0&&m==0&&k==0)
			break;
		for(i=0;i<n;i++)
			scanf("%s",s[i]);
		memset(use,0,sizeof(use));
		memset(b,0,sizeof(a));
		memset(a,0,sizeof(b));
		x=0,y=k-1;
		ans=0,flag=0;
		dfs(x,y);
		int m2=0;
		if(flag==2)
		{
			for(i=1;i<=ans;i++)
			{
				if(use[a[i]][b[i]]==2)
					break;
				m2++;
			}
			printf("%d step(s) before a loop of %d step(s)\n",m2,ans-m2);
		}
		else
			printf("%d step(s) to exit\n",ans);
	}
	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