YT14-HDU-機器人走表格

不被看好的青春叫成長發表於2015-02-24

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)

程式碼如下:

#include <iostream>
using namespace std;
char str1[11][11];                  //將表格用字元型二維陣列表示
int main()
{
    int a,b,c,i,j;
    int k,p,q=0;
    while(cin>>a>>b>>c)           //輸入表格的長度a、寬度b和機器人進入表格的位置c
    {
        int num[11][11]={1};      //用一個二維陣列記錄 
        k=0;                      //k代表總步數
        p=1;                      //機器人一定是由第一行進入的
        if (a==0||b==0)
            break;
        for (i=1; i<=a; i++)
        {
            for (j=1; j<=b; j++)
            {
                cin>>str1[i][j];               //輸入表格中各字元。
            }
        }
        q=c;                                   //機器人進入的初始位置(p,q)=(1,c)                                 
        int str2[11][11];         
        for (i=1; i<=a; i++)
            for (j=1; j<=b; j++)
                str2[i][j]=0;                  //將圖用另外一組二維陣列表示,初始都為0
        str2[p][q]=1;                          //機器人進入的那個位置更改為1;
        do
        {
            if (str1[p][q]=='W')                //如果表格當前區域的字元是W,則向
                q--;
            else if (str1[p][q]=='S')
                p++;
            else if (str1[p][q]=='N')
                p--;
            else if (str1[p][q]=='E')
                q++;
            if (str2[p][q]==1&&p>0&&q>0&&p<=a&&q<=b) //如果這一位置已經走過且在表格內
            {
                cout<<num[p][q]<<" step(s) before a loop of "<<k-num[p][q]+1<<" step(s)"<<endl;
                break;                              //輸出第一次進入迴圈和這一迴圈內的步數 
            }
            k++;                                   //總步數+1;
            num[p][q]=k;                           //記錄第一次走到這一位置的步數。
            if (p<=0||q<=0||p>a||q>b)              //如果不在表格內,說明走出了表格
            {
                cout<<k<<" step(s) to exit"<<endl;
                break;
            }
            str2[p][q]=1;                         //將走過的位置更改為1;
        }
        while(1);
    }
    return 0;
}


程式碼思路:

首先題目的大概意思是一個機器人從表格的第一行的某一位置進入表格,按照表格中各位置的字元移動,如果走出了表格,則輸出多少步走出去,如果走不出去,說明陷入了迴圈,輸出第一次進入迴圈時走了多少步和這一迴圈內的步數。

用陣列str1表示字元,str2來表示是否走過這一位置,在這道題中,如果能走出去,直接統計步數輸出就行,當如果走到一個走過的位置,就肯定會陷入迴圈走不出去的,所以當下一步的位置代表的數字為1時,說明陷入了迴圈,這時利用此時的步數減去第一次到達下一步的位置的步數+1就行。(用num陣列記錄了到達每一個位置的步數。)



相關文章