2013 Tencent marathon preliminary ACM Xiaoming series stories - Hide and seek

From , 5 Years ago, written in C, viewed 72 times.
URL https://pastebin.vip/view/5f146156
  1. /*
  2.  
  3. 2013腾讯马拉松初赛第5场
  4.  
  5. 1004 小明系列故事——捉迷藏
  6.  
  7. Time Limit: 0.2 Seconds   Memory Limit: 32768K
  8.  
  9.  
  10. 小明的妈妈生了三个孩子,老大叫大明, 老二叫二明, 老三..., 老三自然就叫小明了。
  11. 一天,小明的妈妈带小明兄弟三人去公园玩耍,公园里面树木很多,有很多地方可以藏身, 于是他们决定玩捉迷藏。经过几轮的猜拳后,第一轮是小明来找其他两个人,游戏规则很简单:
  12. 只要小明可以在规定的时间内找到他们就算小明获胜,并且被发现的两个人猜拳决定谁在下一轮负责找人;如果在规定的时间内只找到一个人,那么没有被发现的人获胜,被找到的人下一轮负责找人;如果在规定的时间内一个人都没有找到,则小明失败了,下一轮还是他来找人。现在小明想知道,在规定时间内,自己是否可以找到所有的人,现在他想请你来帮忙计算一下。
  13. 为了简单起见,把公园看成是n行m列的矩阵,其中’S’表示小明,’D’表示大名,’E’表示二明,’X’表示障碍物,’.’表示通路。这里,我们把发现定义为,可以直接看到对方, 也就是说两个人在同一行或者同一列,并且中间没有障碍物或者没有其他人就可以看到对方。并且假设,大明,二明藏好以后就不会再改变位置,小明每个单位时间可以从当前的位置走到相邻的四个位置之一,并且不会走出公园。
  14.  
  15. Input
  16.  
  17. 测试数据第一行是一个正整数T,表示有T组测试数据。
  18. 每一组测试数据首先是三个正整数n,m,t,分别表示行数、列数和规定的时间,接下来n行,每行m个上述的字符,并且保证有且只有一个’S’,一个’E’,一个’D’。
  19.  
  20. [Technical Specification]
  21. T < 200
  22. 3 <= n, m <= 100
  23. 0 <= t <= 100
  24.  
  25. Output
  26.  
  27. 每组先输出一行Case c:(c表示当前的组数,从1开始计数);
  28. 接下来一行,如果小明可以在规定时间内找到所有的人,则输出最少需要的时间,否则输出-1。
  29.  
  30.  
  31.  
  32. Sample Input
  33. 3
  34. 5 6 3
  35. XXD...
  36. ....E.
  37. ....X.
  38. ....S.
  39. ......
  40. 5 6 3
  41. XDX...
  42. ....E.
  43. ......
  44. ....S.
  45. ......
  46. 5 6 8
  47. XXDX..
  48. .XEX..
  49. ......
  50. ....S.
  51. ......
  52.  
  53. Sample Output
  54. Case 1:
  55. -1
  56. Case 2:
  57. 3
  58. Case 3:
  59. -1
  60.  
  61. */
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69. #include <cmath>
  70. #include <ctime>
  71. #include <iostream>
  72. #include <string>
  73. #include <vector>
  74. #include <cstdio>
  75. #include <cstdlib>
  76. #include <cstring>
  77. #include <queue>
  78. #include <map>
  79. #include <set>
  80. #include <algorithm>
  81. #include <cctype>
  82. #include <stack>
  83. #include <deque>
  84. using namespace std;
  85. typedef  __int64 LL;
  86. #define eps 10e-9
  87. #define inf 0x3f3f3f3f
  88. const int maxn = 100 + 20;
  89. const int mod  = 1000000000 + 7;
  90. char ma[maxn][maxn];
  91. bool vis[maxn][maxn][2][2];
  92. struct node {
  93.     int x, y, step;
  94.     int f1, f2;
  95. } s_pos, d_pos, e_pos;
  96. int dx[] = {1, -1, 0, 0};
  97. int dy[] = {0, 0, 1, -1};
  98. int n, m, t;
  99. void cheak(node &a, node b) {
  100.     if(a.x == b.x) {
  101.         int start = min(a.y, b.y), end = max(a.y, b.y), find = 0;
  102.         for(int i = start + 1; i < end; i++) {
  103.             if(ma[a.x][i] != '.') {
  104.                 find = 1;
  105.                 break;
  106.             }
  107.         }
  108.         if(!find) {
  109.             a.f1 = 1;
  110.         }
  111.     }
  112.     if(a.y == b.y) {
  113.         int start = min(a.x, b.x), end = max(a.x, b.x), find = 0;
  114.         for(int i = start + 1; i < end; i++) {
  115.             if(ma[i][a.y] != '.') {
  116.                 find = 1;
  117.                 break;
  118.             }
  119.         }
  120.         if(!find) {
  121.             a.f1 = 1;
  122.         }
  123.     }
  124.  
  125. }
  126. void cheak2(node &a, node b) {
  127.     if(a.x == b.x) {
  128.         int start = min(a.y, b.y), end = max(a.y, b.y), find = 0;
  129.         for(int i = start + 1; i < end; i++) {
  130.             if(ma[a.x][i] != '.') {
  131.                 find = 1;
  132.                 break;
  133.             }
  134.         }
  135.         if(!find) {
  136.             a.f2 = 1;
  137.         }
  138.     }
  139.     if(a.y == b.y) {
  140.         int start = min(a.x, b.x), end = max(a.x, b.x), find = 0;
  141.         for(int i = start + 1; i < end; i++) {
  142.             if(ma[i][a.y] != '.') {
  143.                 find = 1;
  144.                 break;
  145.             }
  146.         }
  147.         if(!find) {
  148.             a.f2 = 1;
  149.         }
  150.     }
  151.  
  152. }
  153. int bfs() {
  154.     memset(vis, false, sizeof(vis));
  155.     queue<node> q;
  156.     cheak(s_pos, d_pos);
  157.     cheak2(s_pos, e_pos);
  158.     vis[s_pos.x][s_pos.y][s_pos.f1][s_pos.f2] = true;
  159.  
  160.     q.push(s_pos);
  161.     while(!q.empty()) {
  162.         node now = q.front();
  163.         q.pop();
  164.         if(now.f1 > 0 && now.f2 > 0) {
  165.             return now.step;
  166.         }
  167.         for(int i = 0; i < 4; i++) {
  168.             node next = now;
  169.             next.x += dx[i];
  170.             next.y += dy[i];
  171.             if(next.x >= 0 && next.x < n && next.y >= 0 && next.y < m && !vis[next.x][next.y][next.f1][next.f2]
  172.                     && ma[next.x][next.y] == '.') {
  173.                 vis[next.x][next.y][next.f1][next.f2] = true;
  174.                 next.step++;
  175.                 if(!next.f1)
  176.                     cheak(next, d_pos);
  177.  
  178.                 if(!next.f2)
  179.                     cheak2(next, e_pos);
  180.                 if(next.step <= t)
  181.                     q.push(next);
  182.  
  183.             }
  184.         }
  185.     }
  186.     return 10000;
  187. }
  188. int main() {
  189.     int  T, ca = 1;
  190.     cin >> T;
  191.     while(T--) {
  192.         scanf("%d %d %d", &n, &m, &t);
  193.         for(int i = 0; i < n; i++)
  194.             scanf("%s", ma[i]);
  195.  
  196.         for(int i = 0; i < n; i++) {
  197.             for(int j = 0; j < m; j++) {
  198.                 if(ma[i][j] == 'S') {
  199.                     s_pos.x = i;
  200.                     s_pos.y = j;
  201.                     s_pos.step = 0;
  202.                     s_pos.f1 = s_pos.f2 = 0;
  203.                 }
  204.                 if(ma[i][j] == 'D') {
  205.                     d_pos.x = i;
  206.                     d_pos.y = j;
  207.                 }
  208.                 if(ma[i][j] == 'E') {
  209.                     e_pos.x = i;
  210.                     e_pos.y = j;
  211.                 }
  212.             }
  213.         }
  214.         int now = bfs();
  215.         printf("Case %d:\n", ca++);
  216.         if(now <= t) printf("%d\n", now);
  217.         else       printf("-1\n");
  218.  
  219.     }
  220.     return 0;
  221. }
  222.  
  223.  

Reply to "2013 Tencent marathon preliminary ACM Xiaoming series stories - Hide and seek"

Here you can reply to the paste above

captcha

https://burned.cc - Burn After Reading Website