1. 程式人生 > >杭電 HDU 1035 Robot Motion

杭電 HDU 1035 Robot Motion

Robot Motion

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7488    Accepted Submission(s): 3431


Problem Description

A robot has been programmed to follow the instructions in its path. Instructions for the next direction the robot is to move are laid down in a grid. The possible instructions are

N north (up the page)
S south (down the page)
E east (to the right on the page)
W west (to the left on the page)

For example, suppose the robot starts on the north (top) side of Grid 1 and starts south (down). The path the robot follows is shown. The robot goes through 10 instructions in the grid before leaving the grid.

Compare what happens in Grid 2: the robot goes through 3 instructions only once, and then starts a loop through 8 instructions, and never exits.

You are to write a program that determines how long it takes a robot to get out of the grid or how the robot loops around.

Input There will be one or more grids for robots to navigate. The data for each is in the following form. On the first line are three integers separated by blanks: the number of rows in the grid, the number of columns in the grid, and the number of the column in which the robot enters from the north. The possible entry columns are numbered starting with one at the left. Then come the rows of the direction instructions. Each grid will have at least one and at most 10 rows and columns of instructions. The lines of instructions contain only the characters N, S, E, or W with no blanks. The end of input is indicated by a row containing 0 0 0.

Output For each grid in the input there is one line of output. Either the robot follows a certain number of instructions and exits the grid on any one the four sides or else the robot follows the instructions on a certain number of locations once, and then the instructions on some number of locations repeatedly. The sample input below corresponds to the two grids above and illustrates the two forms of output. The word "step" is always immediately followed by "(s)" whether or not the number before it is 1.

Sample Input 3 6 5 NEESWE WWWESS SNWWWW 4 5 1 SESWE EESNW NWEEN EWSEN 0 0
Sample Output
10 step(s) to exit
3 step(s) before a loop of 8 step(s)

我改你千百遍,不他媽還是讓哥哥ac了麼。雖說這是簡單模擬題,但畢竟哥哥人生中第一次親手做,千般周折啊。

#include<iostream>
using namespace std;
struct ls
{
	int x;
	int y;
}cnt[203];

void go(char (*p)[11],int N,int M,int &co)
{
                     //	for(int r=1;r<=N;r++)
	                          //for(int e=1;e<=M;e++)
	                                 //	cout<<p[r][e]<<" ";
int ro=1,i=0,count=0;
	for(int v=0;v<2*N*M;v++)         //這句是最後一次修改的地方 ,也就是最後一次錯誤的地方,試想如果想在矩陣中
	{                                //找到一個如果存在的迴圈,因為走一遍最大步數範圍N*M,最大迴圈2*N*M。
		if(p[ro][co]=='S')
		{
			count++;                 //cout<<"count="<<count<<"!"<<endl;
			                         //cout<<"count="<<count<<"!"<<endl;
			if(ro==N)                //判斷對應的行和列是否是邊界值。
				{
				cnt[++i].x=ro;        //此前這兩句賦值都寫成了i++導致的結果是兩次增1,x,y賦值不在同一結構
				                      //之後把第二句改為i ,發現還是不在同一結構。此後才又把第一個改為++i  
				cnt[i].y=co;
				break;
			}
			else
			{
				cnt[++i].x=ro;       //記錄每走一步的座標
				cnt[i].y=co;
				ro++;                 //cout<<cnt[i].x<<" "<<cnt[i].y<<" "<<endl;
			}
		}
		else if(p[ro][co]=='E')
		{
			count++;                  //cout<<"count="<<count<<"!"<<endl;
			if(co==M)
			{
				cnt[++i].x=ro;
				cnt[i].y=co;
				break;
			}
			else
			{
				cnt[++i].x=ro;
				cnt[i].y=co;
				co++;             //cout<<cnt[i].x<<" "<<cnt[i].y<<" "<<endl;
			}
		}
		else if(p[ro][co]=='N')
		{
			count++;               //cout<<"count="<<count<<"!"<<endl;
			if(ro==1)
			{
				cnt[++i].x=ro;
				cnt[i].y=co;
				break;
			}
			else
			{
				cnt[++i].x=ro;
				cnt[i].y=co;
				ro--;               //cout<<cnt[i].x<<" "<<cnt[i].y<<" "<<endl;
			}
		}
	
		else if(p[ro][co]=='W')
		{
			count++;                  //cout<<"count="<<count<<"!"<<endl;
			if(co==1)
			{
				cnt[++i].x=ro;
				cnt[i].y=co;
				break;
			}
			else
			{
				cnt[++i].x=ro;
				cnt[i].y=co;
				co--;             //cout<<cnt[i].x<<" "<<cnt[i].y<<" "<<endl;
			}
		}
		
	}
	                          //for(int t=1;t<=i;t++)
	                                //	cout<<cnt[t].x<<" "<<cnt[t].y<<"!"<<endl;
	int flag=0,round,k,j;
		for(j=1;j<=i;j++)             //注意結構開始的下標從1
		{
			for( k=j+1;k<=i;k++)
			{
				if(cnt[j].x==cnt[k].x&&cnt[j].y==cnt[k].y)//暴力搜尋最小週期數round,如果發現存在即跳出內層circle
				{
					flag=1;
					round=k-j;
			     	break;
				}
			}
			if(flag==1)//必須加這條條件語句
				break;
		}

if(flag)
     cout<<j-1<<" step(s) before a loop of "<<round<<" step(s)"<<endl;
else
    cout<<count<<" step(s) to exit"<<endl;

}
int main()
{
	int n,m,k;
	while(cin>>n>>m,n+m)//題目說的是n,m不同時為零,對k沒要求
	{
		cin>>k;
		char ls[11][11];
		for(int q=1;q<=n;q++)
			for(int w=1;w<=m;w++)
				cin>>ls[q][w];
			go(ls,n,m,k);
	}
	return 0;
}