zoukankan      html  css  js  c++  java
  • HDU 2102 A计划

    A计划

    Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 28671    Accepted Submission(s): 7204


    Problem Description
    可怜的公主在一次次被魔王掳走一次次被骑士们救回来之后,而今,不幸的她再一次面临生命的考验。魔王已经发出消息说将在T时刻吃掉公主,因为他听信谣言说吃公主的肉也能长生不老。年迈的国王正是心急如焚,告招天下勇士来拯救公主。不过公主早已习以为常,她深信智勇的骑士LJ肯定能将她救出。
    现据密探所报,公主被关在一个两层的迷宫里,迷宫的入口是S(0,0,0),公主的位置用P表示,时空传输机用#表示,墙用*表示,平地用.表示。骑士们一进入时空传输机就会被转到另一层的相对位置,但如果被转到的位置是墙的话,那骑士们就会被撞死。骑士们在一层中只能前后左右移动,每移动一格花1时刻。层间的移动只能通过时空传输机,且不需要任何时间。
     


    Input
    输入的第一行C表示共有C个测试数据,每个测试数据的前一行有三个整数N,M,T。 N,M迷宫的大小N*M(1 <= N,M <=10)。T如上所意。接下去的前N*M表示迷宫的第一层的布置情况,后N*M表示迷宫第二层的布置情况。
     


    Output
    如果骑士们能够在T时刻能找到公主就输出“YES”,否则输出“NO”。
     


    Sample Input
     
     
     
    样例拉不过来,贴在代码中
     
    Sample Output
    YES

    挺简单的一道 bfs ,之前老是超时。。。后来才发现交错题号了。。。

    主要是刚开始把图处理一下,过程能少判断很多

    当 # 对应过去还是 # ,就把这两个地方都变成 * ,当 # 对应过去是 * ,同样处理

      1 #include <iostream>
      2 #include <cstdio>
      3 #include <cstring>
      4 #include <cmath>
      5 #include <iomanip>
      6 #include <string>
      7 #include <sstream>
      8 #include <algorithm>
      9 #include <stack>
     10 #include <queue>
     11 #include <set>
     12 #include <map>
     13 
     14 using namespace std;
     15 
     16 typedef long long LL;
     17 const int INF = 0x3f3f3f3f;
     18 const int MAXN = 1000005;
     19 const int MOD = 1e9 + 7;
     20 
     21 #define MemI(x) memset(x, -1, sizeof(x))
     22 #define Mem0(x) memset(x, 0, sizeof(x))
     23 #define MemM(x) memset(x, 0x3f, sizeof(x));
     24 
     25 char mp[3][25][25];
     26 int vis[3][25][25];
     27 struct Node
     28 {
     29     int x, y, z;
     30     int step;
     31 }t;
     32 queue<Node> q;
     33 int n, m, num, dis[][2] = {0, 1, 0, -1, -1, 0, 1, 0};
     34 
     35 bool solve()
     36 {
     37     int i, j, k, x, y, z, step;
     38     while(!q.empty())
     39     {
     40         x = q.front().x, y = q.front().y, z = q.front().z, step = q.front().step;
     41         q.pop();
     42         if(mp[z][x][y] == 'P')
     43         {
     44 //            cout << step << endl;
     45             if(step <= num)
     46                 return true;
     47             return false;
     48         }
     49         if(vis[z][x][y])
     50             continue;
     51         vis[z][x][y] = 1;
     52         for(k = 0;k < 4;++k)
     53         {
     54             i = x + dis[k][0];
     55             j = y + dis[k][1];
     56             if(i < 0 || j < 0 || i >= n || j >= m || mp[z][i][j] == '*')
     57                 continue;
     58             t.z = z;
     59             if(mp[z][i][j] == '#')
     60             {
     61                 vis[z][i][j] = 1;
     62                 t.z = !z;
     63             }
     64             t.x = i, t.y = j, t.step = step + 1;
     65 //            cout << t.z << " " << t.x << " " << t.y << " " << t.step << endl;
     66             q.push(t);
     67         }
     68     }
     69     return false;
     70 }
     71 
     72 int main()
     73 {
     74     int T;
     75     cin >> T;
     76     while(T--)
     77     {
     78         while(!q.empty())
     79             q.pop();
     80         Mem0(vis);
     81         cin >> n >> m >> num;
     82         for(int i = 0;i < n;++i)
     83         {
     84             getchar();
     85             for(int j = 0;j < m;++j)
     86                 cin >> mp[0][i][j];
     87         }
     88         for(int i = 0;i < n;++i)
     89         {
     90             getchar();
     91             for(int j = 0;j < m;++j)
     92             {
     93                 cin >> mp[1][i][j];
     94                 if(mp[0][i][j] == '#')
     95                     if(mp[1][i][j] == '#' || mp[1][i][j] == '*')
     96                     mp[0][i][j] = mp[1][i][j] = '*';
     97             }
     98         }
     99         t.x = 0, t.y = 0, t.z = 0, t.step = 0;
    100         q.push(t);
    101         if(solve())
    102             cout << "YES" << endl;
    103         else
    104             cout << "NO" << endl;
    105     }
    106     return 0;
    107 }
    108 /*
    109 1
    110 5 5 14
    111 S*#*.
    112 .#...
    113 .....
    114 ****.
    115 ...#.
    116 
    117 ..*.P
    118 #.*..
    119 ***..
    120 ...*.
    121 *.#..
    122 */
    现在所有的不幸都是以前不努力造成的。。。
  • 相关阅读:
    Lua弱引用table
    Javascript定义类(class)的三种方法
    双检锁技术
    【翻译】ASP.NET缓存管理
    socket python
    mvc项目
    MSBuild
    阅读glibc源码
    MVC3使用Unity实现依赖注入接口与于实现类自动注册
    C# 指针之美
  • 原文地址:https://www.cnblogs.com/shuizhidao/p/9349202.html
Copyright © 2011-2022 走看看