zoukankan      html  css  js  c++  java
  • Robot Motion(POJ 1573 模拟题)

    ~题目链接~

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

    输入

    3 6 5
    NEESWE
    WWWESS
    SNWWWW
    4 5 1
    SESWE
    EESNW
    NWEEN
    EWSEN
    0 0 0

    结果

    10 step(s) to exit
    3 step(s) before a loop of 8 step(s)

    输入行数、列数、机器人的位置

    机器人可以出去,则输出步数

    否则,则输出循环的始端和终端

     1 #include<stdio.h>
     2 #include<string.h>
     3 #include<stdlib.h>
     4 
     5 int fx[4]= {-1,1,0,0};
     6 int fy[4]= {0,0,-1,1};
     7 int map[20][20],vis[20][20],st[20][20];
     8 int flag,step,n,m,k,x,y;
     9 
    10 void DFS(int i,int j)
    11 {
    12     int I,J;
    13     st[i][j]=++step;
    14     I=i+fx[map[i][j]];
    15     J=j+fy[map[i][j]];
    16     if(I<0 || I>=n || J<0 || J>=m)
    17         return;
    18     if(!vis[I][J])
    19     {
    20         vis[i][j]=1;
    21         DFS(I,J);
    22     }
    23     else
    24     {
    25         flag=1;
    26         x=st[I][J]-1;
    27         y=st[i][j]-st[I][J]+1;
    28         return;
    29     }
    30 }
    31 
    32 int main()
    33 {
    34     char str;
    35     while(~scanf("%d%d%d",&n,&m,&k) && (n!=0 || m!=0 || k!=0))
    36     {
    37         getchar();
    38         memset(vis,0,sizeof(vis));
    39         memset(map,0,sizeof(map));
    40         memset(st,0,sizeof(st));
    41         for(int i=0; i<n; i++)
    42         {
    43             for(int j=0; j<m; j++)
    44             {
    45                 scanf("%c",&str);
    46                 if(str=='N') map[i][j]=0;
    47                 if(str=='S') map[i][j]=1;
    48                 if(str=='W') map[i][j]=2;
    49                 if(str=='E') map[i][j]=3;
    50             }
    51             getchar();
    52         }
    53         flag=0;
    54         step=0;
    55         DFS(0,k-1);
    56         if(flag)
    57             printf("%d step(s) before a loop of %d step(s)
    ",x,y);
    58         else
    59             printf("%d step(s) to exit
    ",step);
    60     }
    61 
    62     return 0;
    63 }
    View Code
  • 相关阅读:
    泛型接口与NUnit初试
    异步文件IO的应用
    [Andrew McAfee]Enterprise 2.0下的大趋势
    silverlight
    扩展prototype库兼容w3c domajax for firefox
    开张了
    最长平台
    常见C/C++ XML解析器比较
    GIS地图开发
    几种常见 容器 比较和分析 hashmap, map, vector, list ...hash table
  • 原文地址:https://www.cnblogs.com/guoyongzhi/p/3243354.html
Copyright © 2011-2022 走看看