zoukankan      html  css  js  c++  java
  • HDU 1035(走迷宫 模拟)

    题意是给定初始位置在一个迷宫中按照要求前进,判断多少步能离开迷宫或者多少步会走入一个长达多少步的循环。

    按要求模拟前进的位置,对每一步在 vis[ ] 数组中进行已走步数的记录,走出去或走到已走过的位置结束,计算出所要求的步数即可。

    要注意的一点是记得 scanf 在 %c 的时候会读入空格和换行的,而 %s 读入的时候以空格和换行作为读入结束的标志。简单起见直接用 cin / cout 了......

    代码如下:

     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 int vis[100][100];
     4 char mp[100][100];
     5 int main()
     6 {
     7     std::ios::sync_with_stdio(false);
     8     int n,m,x,y,cnt;
     9     while(cin >> n >> m)
    10     {
    11         if(!(n||m)) break;
    12         memset(vis,0,sizeof(vis));
    13         cin >> y;
    14         for(int i = 1; i <= n; ++i)
    15             for(int j = 1; j <= m; ++j)
    16                 cin >> mp[i][j];
    17         x = 1;
    18         cnt = 1;
    19         while(1)
    20         {
    21             if(x<1||x>n||y<1||y>m)
    22             {
    23                 cout << cnt-1 << " step(s) to exit
    ";
    24                 break;
    25             }
    26             if(vis[x][y])
    27             {
    28                 cout << vis[x][y]-1 << " step(s) before a loop of " << cnt-vis[x][y] << " step(s)
    ";
    29                 break;
    30             }
    31             vis[x][y] = cnt++;
    32             if(mp[x][y] == 'N') --x;
    33             else if(mp[x][y] == 'W') --y;
    34             else if(mp[x][y] == 'E') ++y;
    35             else if(mp[x][y] == 'S') ++x;
    36         }
    37     }
    38     return 0;
    39 }
    View Code
    日后若能有更好的想法,再来完善。 希望看到的大神不吝赐教 orz
  • 相关阅读:
    文本溢出隐藏与定位
    css基本属性 内边距 外边距及盒模型
    CSS基本属性2
    CSS基本属性
    CSS入门
    表格与表单
    列表与图片
    html标签类型
    HashMap和HashTable
    Map接口
  • 原文地址:https://www.cnblogs.com/Taskr212/p/9554111.html
Copyright © 2011-2022 走看看