Poj OpenJudge 百练 1573 Robot Motion

1.Link:

http://poj.org/problem?id=1573

http://bailian.openjudge.cn/practice/1573/

2.Content:

Robot Motion

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 10856   Accepted: 5260

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

3.Method:

模拟题,使用arr_mark保存行走路径,走到重复的路则为loop,出边界则为exit

特别注意 0 step(s) before a loop of 4 step(s) 的情况

如:

2 2 1
SW
EN

4.Code:

 1 #include <iostream>
 2 #include <cstring>
 3
 4 using namespace std;
 5
 6 //N,E,S,W
 7 const int idx_x[] = {0,1,0,-1};
 8 const int idx_y[] = {-1,0,1,0};
 9 const char idx_ch[] = {‘N‘,‘E‘,‘S‘,‘W‘};
10 const int num_d = 4;
11
12 int main()
13 {
14     //freopen("D://input.txt","r",stdin);
15
16     int y,x;
17     int i;
18
19     int w,h,s;
20     cin >> h >> w >> s;
21
22     while(h != 0 || w != 0 || s != 0)
23     {
24         int **arr_d = new int*[h];
25         for(y = 0; y < h; ++y) arr_d[y] = new int[w];
26
27         char ch;
28         for(y = 0; y < h; ++y)
29         {
30             for(x = 0; x < w; ++x)
31             {
32                 cin >> ch;
33                 for(i = 0; i < num_d; ++i) if(idx_ch[i] == ch) break;
34                 arr_d[y][x] = i;
35             }
36         }
37
38         //for(y = 0; y < h; ++y)
39         //{
40         //    for(x = 0; x < w; ++x)
41         //    {
42         //        cout << arr_d[y][x] << " ";
43         //    }
44         //    cout << endl;
45         //}
46
47         int **arr_mark = new int*[h];
48         for(y = 0; y < h; ++y)
49         {
50             arr_mark[y] = new int[w];
51             memset(arr_mark[y],0,sizeof(int) * w);
52         }
53
54         y = 0;
55         x = s - 1;
56         int path = 0;
57         int nx,ny;
58         while(!arr_mark[y][x])//loop
59         {
60             nx = x;
61             ny = y;
62             arr_mark[y][x] = ++path;
63
64             x = nx + idx_x[arr_d[ny][nx]];
65             y = ny + idx_y[arr_d[ny][nx]];
66
67             if(y < 0 || y >= h || x < 0 || x >= w) break;//exit
68         }
69
70         if(y < 0 || y >= h || x < 0 || x >=w)
71         {
72             cout << path << " step(s) to exit" << endl;
73         }
74         else
75         {
76             cout << (arr_mark[y][x] - 1) << " step(s) before a loop of " << (arr_mark[ny][nx] - arr_mark[y][x] + 1) << " step(s)" << endl;
77         }
78
79         //for(y = 0; y < h; ++y)
80         //{
81         //    for(x = 0; x < w; ++x) cout << arr_mark[y][x] << " ";
82         //    cout << endl;
83         //}
84
85         for(y = 0; y < h; ++y) delete [] arr_mark[y];
86         delete [] arr_mark;
87
88         for(y = 0; y < h; ++y) delete [] arr_d[y];
89         delete [] arr_d;
90
91         cin >> h >> w >> s;
92     }
93
94     //fclose(stdin);
95
96     return 0;
97 }

5.Reference:

http://poj.org/showmessage?message_id=123463

时间: 2024-10-21 05:31:42

Poj OpenJudge 百练 1573 Robot Motion的相关文章

Poj OpenJudge 百练 2632 Crashing Robots

1.Link: http://poj.org/problem?id=2632 http://bailian.openjudge.cn/practice/2632/ 2.Content: Crashing Robots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7912   Accepted: 3441 Description In a modernized warehouse, robots are used to

Poj OpenJudge 百练 1860 Currency Exchang

1.Link: http://poj.org/problem?id=1860 http://bailian.openjudge.cn/practice/1860 2.Content: Currency Exchange Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 20706   Accepted: 7428 Description Several currency exchange points are working

Poj OpenJudge 百练 2602 Superlong sums

1.Link: http://poj.org/problem?id=2602 http://bailian.openjudge.cn/practice/2602/ 2.Content: Superlong sums Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 22337   Accepted: 6577 Description The creators of a new programming language D++

Poj OpenJudge 百练 2389 Bull Math

1.Link: http://poj.org/problem?id=2389 http://bailian.openjudge.cn/practice/2389/ 2.Content: Bull Math Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13067   Accepted: 6736 Description Bulls are so much better at math than the cows. The

Poj OpenJudge 百练 1062 昂贵的聘礼

1.Link: http://poj.org/problem?id=1062 http://bailian.openjudge.cn/practice/1062/ 2.Content: 昂贵的聘礼 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 37631   Accepted: 10872 Description 年轻的探险家来到了一个印第安部落里.在那里他和酋长的女儿相爱了,于是便向酋长去求亲.酋长要他用10000个金

Poj OpenJudge 百练 Bailian 1008 Maya Calendar

1.Link: http://poj.org/problem?id=1008 http://bailian.openjudge.cn/practice/1008/ 2.content: Maya Calendar Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 66971   Accepted: 20644 Description During his last sabbatical, professor M. A. Ya

1573 Robot Motion

Robot Motion Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 10219   Accepted: 4977 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

[OpenJudge] 百练2754 八皇后

八皇后 Description 会下国际象棋的人都很清楚:皇后可以在横.竖.斜线上不限步数地吃掉其他棋子.如何将8个皇后放在棋盘上(有8 * 8个方格),使它们谁也不能被吃掉!这就是著名的八皇后问题. 对于某个满足要求的8皇后的摆放方法,定义一个皇后串a与之对应,即a=b1b2...b8,其中bi为相应摆法中第i行皇后所处的列数.已经知道8皇后问题一共有92组解(即92个不同的皇后串).给出一个数b,要求输出第b个串.串的比较是这样的:皇后串x置于皇后串y之前,当且仅当将x视为整数时比y小. I

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