zoukankan      html  css  js  c++  java
  • 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

    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

  • 相关阅读:
    vim快捷键整理(随时更新)
    值不能为:null 参数名: viewInfo (Microsoft.SqlServer.Management.SqlStudio.Explorer)
    未能从程序集“system.servicemodel,version=3.0.0.0,culture=neutral,publickeytoken=b77a5c561934e089”中加载类型“system.servicemodel.activation.httpmodule”
    linux内存手动释放
    无法获取有关 Windows NT 组/用户 'WIN-***************' 的信息,错误代码 0x534。 [SQLSTATE 42000] (错误 15404)).
    ERROR: child process failed, exited with error number 51
    MongoDB 分片状态recovering
    打不开磁盘“D:vmcentos7.3_3CentOS7.3_3 64 位-000002.vmdk”或它所依赖的某个快照磁盘
    更换内存引发的问题
    AttributeError: 'module' object has no attribute 'random'
  • 原文地址:https://www.cnblogs.com/mobileliker/p/4071131.html
Copyright © 2011-2022 走看看