标签:c++ acm 杭电 编程 算法
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;
}
杭电 HDU 1035 Robot Motion
标签:c++ acm 杭电 编程 算法
原文地址:http://blog.csdn.net/lsgqjh/article/details/44508463