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


    模拟题

    AC代码:

     1 #pragma comment(linker, "/STACK:1024000000,1024000000")
     2 #include<iostream>
     3 #include<cstdio>
     4 #include<cstring>
     5 #include<cmath>
     6 #include<math.h>
     7 #include<algorithm>
     8 #include<queue>
     9 #include<set>
    10 #include<bitset>
    11 #include<map>
    12 #include<vector>
    13 #include<stdlib.h>
    14 using namespace std;
    15 #define ll long long
    16 #define eps 1e-10
    17 #define MOD 1000000007
    18 #define N 16
    19 #define inf 1e12
    20 int n,m,st;
    21 char mp[N][N];
    22 int vis[N][N];
    23 bool judge(int i,int j){
    24     if(i<0 || i>=n || j<0 || j>=m) return true;
    25     return false;
    26 }
    27 int main()
    28 {
    29     while(scanf("%d%d",&n,&m)==2){
    30         if(n==0 && m==0) break;
    31         memset(vis,0,sizeof(vis));
    32         memset(mp,'',sizeof(mp));
    33         scanf("%d",&st);
    34         st--;
    35         for(int i=0;i<n;i++){
    36             scanf("%s",mp[i]);
    37         }
    38         /*for(int i=0;i<n;i++){
    39             for(int j=0;j<m;j++){
    40                 printf("%c",mp[i][j]);
    41             }
    42         }
    43         */
    44         
    45         int i=0,j=st;
    46         int ans=1;
    47         int loops=-1;
    48         int L;
    49         //vis[i][j]=1;
    50         while(1){
    51             if(mp[i][j]=='W' && vis[i][j]==0){
    52                 vis[i][j]=ans;
    53                 j--;
    54                 
    55                 //printf("%d %d
    ",i,j);
    56             }else if(mp[i][j]=='E' && vis[i][j]==0){
    57                 vis[i][j]=ans;
    58                 j++;
    59                 //printf("%d %d
    ",i,j);
    60                 
    61             }else if(mp[i][j]=='N' && vis[i][j]==0){
    62                 vis[i][j]=ans;
    63                 i--;
    64                 //printf("%d %d
    ",i,j);
    65                 
    66             }else if(mp[i][j]=='S' && vis[i][j]==0){
    67                 vis[i][j]=ans;
    68                 i++;
    69                 //printf("%d %d
    ",i,j);
    70                 
    71             }
    72             else if(vis[i][j]){
    73                 ans--;
    74                 loops = ans-vis[i][j]+1;
    75                 L = vis[i][j];
    76                 break;
    77             }
    78     
    79             else if(judge(i,j)){
    80                 //printf("%d %d
    ",i,j);
    81                 ans--;
    82                 break;
    83             }
    84             ans++;
    85         }
    86         
    87         if(loops==-1){
    88             printf("%d step(s) to exit
    ",ans);
    89         }else{
    90             printf("%d step(s) before a loop of %d step(s)
    ",L-1,loops);
    91         }
    92         
    93         
    94     }
    95     return 0;
    96 }
    View Code
  • 相关阅读:
    路由器01---k2刷Pandora
    nginx+flask02---概念
    Nginx06---实现websocket
    Nginx05---负载均衡 upsteam
    nginx+uwsgi02---django部署(推荐)
    Nginx04---实现直播
    Nginx04---编译安装
    mybatis+mysql insert添加数据后返回数据主键id---(转)
    JSON字符串转换为Map
    Http post请求案例
  • 原文地址:https://www.cnblogs.com/UniqueColor/p/5182476.html
Copyright © 2011-2022 走看看