标签:des style c class blog code
Robot
Motion
Time Limit: 1000MS |
|
Memory Limit: 10000K |
Total Submissions: 10130 |
|
Accepted: 4932 |
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)
【题目来源】
Mid-Central
USA 1999
http://poj.org/problem?id=1573
【题目大意】
给定一个网格,每个格子上都有相关的行走方向。玩过“管道工人”这个小游戏的人就很好理解了,这个和那个原理是一样的。
很水的题,一遍AC,只要思路清晰。
#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<bitset>
#define MAX 1000
using namespace std;
struct Node
{
char c;
int step;
bool vis;
};
Node Map[MAX][MAX];
int step1;
int pa_x,pa_y; //当前结点的前驱
void make_Map(int n,int m)
{
int i,j;
for(i=0;i<=n+1;i++)
{
for(j=0;j<=m+1;j++)
{
Map[i][j].step=0;
Map[i][j].vis=false;
}
}
for(i=0;i<=n+1;i++)
Map[i][0].c=Map[i][m+1].c=‘Q‘;
for(i=0;i<=m+1;i++)
Map[0][i].c=Map[n+1][i].c=‘Q‘;
}
int go(int n,int m)
{
int i,j;
if(Map[n][m].c==‘Q‘)
{
printf("%d step(s) to exit\n",step1);
return 1;
}
if(Map[n][m].vis)
{
printf("%d step(s) before a loop of %d step(s)\n",Map[n][m].step,Map[pa_x][pa_y].step+1-Map[n][m].step);
return true;
}
Map[n][m].step=step1++;
Map[n][m].vis=true;
pa_x=n;
pa_y=m;
switch(Map[n][m].c)
{
case ‘N‘:go(n-1,m);break;
case ‘E‘:go(n,m+1);break;
case ‘S‘:go(n+1,m);break;
case ‘W‘:go(n,m-1);break;
}
return false;
}
int main()
{
int n,m,v;
while(scanf("%d%d%d",&n,&m,&v),n+m+v)
{
getchar();
step1=0;
make_Map(n,m);
int i,j;
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
scanf("%c",&Map[i][j].c);
}
getchar();
}
go(1,v);
}
return 0;
}
模拟 --- 搜索模拟,布布扣,bubuko.com
模拟 --- 搜索模拟
标签:des style c class blog code
原文地址:http://www.cnblogs.com/acmer-jsb/p/3746712.html