hdu1035 Robot Motion 模拟

mp[r] [c] [0]保存 地图上(r,c) 的方向

如下

                   N(8)

       W(4)                E(6)

                   S(2)

mp[r] [c] [1]保存点(r,c)被经过的次数

 

当第三次遇到(r,c)是不再继续

否则mp[r] [c] [1]++

 

 

#include<iostream>
using namespace std;

int main()
{
	int mp[12][12][4],r,c,e,x,y,nloop,nstep,i,j;
	char t;
	while(scanf("%d%d%d",&r,&c,&e)&&(r||c||e))
	{
		memset(mp,0,sizeof(mp));
		for(i=1;i<=r;i++)
		{
			getchar();
			for(j=1;j<=c;j++)
			{
				scanf("%c",&t);
				if(t=='E') mp[i][j][0]=6;
				else if(t=='S') mp[i][j][0]=2;
					 else if(t=='W') mp[i][j][0]=4;
				  	 	  else mp[i][j][0]=8;
			}
		}
		nstep=nloop=0;
		x=e;y=1;
		while(1)
		{
			if(mp[y][x][1]==2||y>r||y<1||x>c||x<1) break;
			if(mp[y][x][1]==0) nstep++;
			else nloop++;
			mp[y][x][1]++;
			
			if(mp[y][x][0]==2) y++;
			else if(mp[y][x][0]==8) y--;
				 else if(mp[y][x][0]==4) x--;
				 	  else x++;
			
		}
		
		if(nloop==0)
			printf("%d step(s) to exit\n",nstep);
		else
			printf("%d step(s) before a loop of %d step(s)\n",nstep-nloop,nloop);
		
	}
	
	
	return 0;
}


 

 

 

你可能感兴趣的:(hdu1035 Robot Motion 模拟)