您的位置:首页 > 编程语言 > C语言/C++

Robot Motion(搜索)

2015-05-29 11:25 465 查看
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 0


Sample Output

10 step(s) to exit
3 step(s) before a loop of 8 step(s)

这次的代码比起上次的精简了不少,但是却一直忘记了要讨论1 1 1的情况,所以一直wa了,之后改变了一下越界时的位置在
tx,ty那判断,之前是x,y的时候判断的,所以一直Wa。之后才发现第一次根本不用讨论,因为第一次肯定是可以走的。

又一次AC代码:

#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
using namespace std;
#define T 15
int a[T][T],bo[T][T],n,m,flag;
int fx[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
void out()
{
int i,j;
for(i=1;i<=n;++i){
for(j=1;j<=m;++j)
printf("%d ",bo[i][j]);
printf("\n");
}
}
void dfs(int x,int y,int v)
{
if(flag)return;
int tx,ty;
tx = x+fx[a[x][y]][0];
ty = y+fx[a[x][y]][1];
if(tx==0||tx==n+1||ty==0||ty==m+1){
printf("%d step(s) to exit\n",bo[x][y]);flag=1;
/*out();*/
return;
}
if(bo[tx][ty]){
printf("%d step(s) before a loop of %d step(s)\n",bo[tx][ty]-1,v+1-bo[tx][ty]);
/*out();*/
flag=1;
return;
}
bo[tx][ty]=bo[x][y]+1;
dfs(tx,ty,bo[tx][ty]);
}
int main()
{
/*freopen("input.txt","r",stdin);*/
int i,j,k;
char s;
while(scanf("%d%d%d",&n,&m,&k)&&(n&&m&&k))
{
flag=0;
for(i=1;i<=n;++i){
for(j=1;j<=m;++j){
bo[i][j]=0;
scanf(" %c",&s);
if(s=='N')
a[i][j]=0;
else if(s=='S')
a[i][j]=1;
else if(s=='W')
a[i][j]=2;
else
a[i][j]=3;
}
}
bo[1][k]=1;
dfs(1,k,0);
}
return 0;
}

AC代码:

#include <iostream>
using namespace std;
int f[15][15];
int n,m;
int fx[4][2]={{1,0},{-1,0},{0,-1},{0,1}};
struct S
{
int s;
int cur;
}str[15][15];
int jugde(int j,int k,int jj,int kk)//判断是否出界
{
if (j<0||j>=n||k<0||k>=m)
{
cout << str[jj][kk].cur+1 << " step(s) to exit" << endl;
return 1;
}
return 0;
}
int ju(int j,int k,int jj,int kk)//判断是否重复了路线
{
if (f[j][k])
{
cout << str[j][k].cur << " step(s) before a loop of "
<< str[jj][kk].cur-str[j][k].cur+1 << " step(s)" << endl;
return 1;
}
return 0;
}
int main()
{
int i,j,k;
char t;
while (cin >> n >> m >> k&&n&&m&&k)
{
for (i=0;i<n;++i)//预处理上下左右为0,1,2,3
{
for (j=0;j<m;++j)
{
f[i][j]=0;
cin >> t;
str[i][j].cur=0;
if (t=='N')
{
str[i][j].s=1;
}
else if (t=='S')
{
str[i][j].s=0;
}
else if (t=='W')
{
str[i][j].s=2;
}
else
{
str[i][j].s=3;
}

}
}
j=0;k--;
int jj=j,kk=k;
while (true)
{
f[j][k]=1;
if (str[j][k].s==1)
{
jj=j;kk=k;
j+=fx[1][0];
k+=fx[1][1];
if(jugde(j,k,jj,kk)||ju(j,k,jj,kk))break;
str[j][k].cur=str[jj][kk].cur+1;
}
else if (str[j][k].s==0)
{
jj=j;kk=k;
j+=fx[0][0];
k+=fx[0][1];
if(jugde(j,k,jj,kk)||ju(j,k,jj,kk))break;
str[j][k].cur=str[jj][kk].cur+1;
}
else if (str[j][k].s==2)
{
jj=j;kk=k;
j+=fx[2][0];
k+=fx[2][1];
if(jugde(j,k,jj,kk)||ju(j,k,jj,kk))break;
str[j][k].cur=str[jj][kk].cur+1;
}
else
{
jj=j;kk=k;
j+=fx[3][0];
k+=fx[3][1];
if(jugde(j,k,jj,kk)||ju(j,k,jj,kk))break;
str[j][k].cur=str[jj][kk].cur+1;
}
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c++ 搜索