Robot Motion(imitate)

Robot Motion

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 11065   Accepted: 5378

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)

Source

Mid-Central USA 1999

 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<iostream>
 4 using namespace std;
 5 int row , col , o ;
 6 char map[150][150] ;
 7 int a[150][150] ;
 8 int x , y ;
 9
10 void loop (char dir)
11 {
12     switch (dir)
13     {
14         case ‘N‘ : printf ("%d step(s) before a loop of %d step(s)\n" , a[x][y] - 1, a[x + 1][y] - a[x][y] + 1) ; break ;
15         case ‘E‘ : printf ("%d step(s) before a loop of %d step(s)\n" , a[x][y] - 1, a[x][y - 1] - a[x][y] + 1) ; break ;
16         case ‘S‘ : printf ("%d step(s) before a loop of %d step(s)\n" , a[x][y] - 1, a[x - 1][y] - a[x][y] + 1) ; break ;
17         case ‘W‘ : printf ("%d step(s) before a loop of %d step(s)\n" , a[x][y] - 1, a[x][y + 1] - a[x][y] + 1) ; break ;
18     }
19 }
20 void solve ()
21 {
22     x = 1 , y = o ;
23     bool flag = 0;
24     char temp ;
25     a[x][y] = 1 ;
26     while (x >= 1 && x <= row  && y >= 1 && y <= col) {
27         switch (map[x][y])
28         {
29             case ‘N‘ : x-- ; if (a[x][y]) flag = 1 ;
30                         if (!flag)
31                             a[x][y] = a[x + 1][y] + 1 ;
32                         else
33                             temp = ‘N‘ ; break ;
34             case ‘E‘ : y++ ; if (a[x][y]) flag = 1 ;
35                         if (!flag)
36                             a[x][y] = a[x][y - 1] + 1 ;
37                         else
38                             temp = ‘E‘ ; break ;
39             case ‘S‘ : x++ ; if (a[x][y]) flag = 1 ;
40                         if (!flag)
41                             a[x][y] = a[x - 1][y] + 1 ;
42                          else
43                             temp = ‘S‘ ; break ;
44             case ‘W‘ : y-- ; if (a[x][y]) flag = 1 ;
45                         if (!flag)
46                             a[x][y] = a[x][y + 1] + 1 ;
47                         else
48                             temp = ‘W‘ ; break ;
49         }
50         if (flag) {
51             loop (temp) ;
52             break ;
53         }
54     }
55     if (!flag)
56         printf ("%d step(s) to exit\n" , a[x][y] - 1) ;
57 }
58 int main ()
59 {
60  //   freopen ("a.txt" , "r" , stdin) ;
61     while (~ scanf ("%d%d%d" , &row , &col , &o)) {
62         if (row == 0 && col == 0 && o == 0)
63             break ;
64         memset (a , 0 , sizeof(a)) ;
65         for (int i = 1 ; i <= row ; i++)
66             for (int j = 1 ; j <= col ; j++)
67                 cin >> map[i][j] ;
68
69         solve () ;
70     /*    for (int i = 1 ; i <= row ; i++) {
71             for (int j = 1 ; j <= col ; j++) {
72                 printf ("%d " , a[i][j]) ;
73             }
74             puts ("") ;
75         }
76         printf ("\n\n") ;*/
77     }
78     return 0 ;
79 }

时间: 2024-12-18 09:42:07

Robot Motion(imitate)的相关文章

hdoj 1035 Robot Motion

Robot Motion Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 7974    Accepted Submission(s): 3685 Problem Description A robot has been programmed to follow the instructions in its path. Instruct

POJ 1573 Robot Motion 搜索

Robot Motion Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6297    Accepted Submission(s): 2952 Problem Description A robot has been programmed to follow the instructions in its path. Instruc

hdu1035 Robot Motion (DFS)

Robot Motion Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 8180    Accepted Submission(s): 3771 Problem Description A robot has been programmed to follow the instructions in its path. Instruc

poj1573 Robot Motion

题目链接: http://poj.org/problem?id=1573 题目: Robot Motion Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 10202   Accepted: 4971 Description A robot has been programmed to follow the instructions in its path. Instructions for the next direct

HDOJ1035 Robot Motion 【模拟】

Robot Motion Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6079    Accepted Submission(s): 2844 Problem Description A robot has been programmed to follow the instructions in its path. Instruc

POJ1573——Robot Motion

Robot Motion 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)

[ACM] hdu 1035 Robot Motion (模拟或DFS)

Robot Motion 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 t

杭电 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. Instruc

POJ 1573 Robot Motion(模拟)

题目代号:POJ 1573 题目链接:http://poj.org/problem?id=1573 Language: Default Robot Motion Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 14195   Accepted: 6827 Description A robot has been programmed to follow the instructions in its path. Instr