1. #include<bits/stdc++.h>
  2. using namespace std;
  3. const int maxN = ;
  4. const int inf = 1e9 + ;
  5. char G[maxN][maxN], snake[maxN][maxN];
  6. int times[maxN][maxN][];
  7. int n, m, sx, sy, ex, ey, ans;
  8. int dir[][] = {{,},{,},{,-},{-,}};
  9.  
  10. struct node
  11. {
  12. int x, y, t, key, killed; //坐标, 步数, 已经拿到的钥匙, 有没有杀蛇
  13. bool operator < (const node& p) const
  14. {
  15. return t > p.t;
  16. }
  17. node(int x, int y, int t, int key, int killed):x(x),y(y), t(t), key(key), killed(killed) {};
  18. };
  19. void bfs()
  20. {
  21. node t = (node)
  22. {
  23. sx, sy, , ,
  24. };
  25. times[sx][sy][] = ;
  26. queue<node> q;
  27. q.push(t);
  28. while(!q.empty())
  29. {
  30. node u = q.front();
  31. q.pop();
  32. int x = u.x, y = u.y, key = u.key, t = u.t, killed = u.killed;
  33. if(x == ex && y == ey && key == m)
  34. {
  35. ans = min(ans, t);
  36. continue;
  37. }
  38. for(int d = ; d < ; d++)
  39. {
  40. int tx = x + dir[d][];
  41. int ty = y + dir[d][];
  42. if(tx < || tx >= n || ty < || ty >= n || G[tx][ty] == '#') //越界
  43. continue;
  44.  
  45. if(G[tx][ty] >= '' && G[tx][ty] <= '') //如果是钥匙
  46. {
  47. int num = G[tx][ty] - '';
  48. if(key + == num && (times[tx][ty][key + ] > t + )) //可以拿
  49. {
  50. q.push(node(tx,ty,t+,key+,killed));
  51. times[tx][ty][key + ] = t + ;
  52. }
  53. else if(((key + ) != num) && (times[tx][ty][key] > t + )) //不可以拿
  54. {
  55. q.push(node(tx,ty,t+,key,killed));
  56. times[tx][ty][key] = t + ;
  57. }
  58. }
  59. else if(G[tx][ty] >= 'A' && G[tx][ty] <= 'E') //蛇
  60. {
  61. int cnt = G[tx][ty] - 'A'; //压位判断
  62.  
  63. if((killed & (<<cnt)) == ) //未杀
  64. {
  65. if(times[tx][ty][key] > t + )
  66. {
  67. q.push(node(tx,ty,t+,key,(killed | (<<cnt))));
  68. times[tx][ty][key] = t + ;
  69. }
  70. }
  71. else //已经杀了
  72. {
  73. if(times[tx][ty][key] > t + )
  74. {
  75. q.push(node(tx,ty,t+,key,killed));
  76. times[tx][ty][key] = t + ;
  77. }
  78. }
  79. }
  80. else if((G[tx][ty] == '.' || G[tx][ty] == 'T' || G[tx][ty] == 'K') && times[tx][ty][key] > t + )
  81. {
  82. q.push(node(tx,ty,t+,key,killed));
  83. times[tx][ty][key] = t + ;
  84. }
  85. }
  86. }
  87. }
  88. int main()
  89. {
  90. // freopen("1.txt","r", stdin);
  91. while(~scanf("%d %d", &n, &m))
  92. {
  93. int cnt = ;
  94. if(n == )
  95. break;
  96. for(int i = ; i < n; i++)
  97. for(int j = ; j < n; j++)
  98. for(int k = ; k < ; k++)
  99. times[i][j][k] = inf;
  100. for(int i = ; i < n; i++)
  101. {
  102. scanf("%s", G[i]);
  103. }
  104. for(int i = ; i < n; i++)
  105. {
  106. for(int j = ; j < n; j++)
  107. {
  108. if(G[i][j] == 'K')
  109. sx = i, sy = j;
  110. if(G[i][j] == 'T')
  111. ex = i, ey = j;
  112. if(G[i][j] == 'S')
  113. {
  114. G[i][j] = (cnt++ + 'A');
  115. }
  116. }
  117. }
  118.  
  119. ans = inf;
  120. bfs();
  121. if(ans == inf)
  122. {
  123. printf("impossible\n");
  124. }
  125. else
  126. {
  127. printf("%d\n", ans);
  128. }
  129. }
  130. return ;
  131. }

HDU 5025 Saving Tang Monk(状态转移, 广搜)的更多相关文章

  1. hdu 5025 Saving Tang Monk 状态压缩dp+广搜

    作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4092939.html 题目链接:hdu 5025 Saving Tang Monk 状态压缩 ...

  2. HDU 5025 Saving Tang Monk 【状态压缩BFS】

    任意门:http://acm.hdu.edu.cn/showproblem.php?pid=5025 Saving Tang Monk Time Limit: 2000/1000 MS (Java/O ...

  3. [ACM] HDU 5025 Saving Tang Monk (状态压缩,BFS)

    Saving Tang Monk Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  4. hdu 5025 Saving Tang Monk(bfs+状态压缩)

    Description <Journey to the West>(also <Monkey>) is one of the Four Great Classical Nove ...

  5. HDU 5025 Saving Tang Monk

    Problem Description <Journey to the West>(also <Monkey>) is one of the Four Great Classi ...

  6. ACM学习历程—HDU 5025 Saving Tang Monk(广州赛区网赛)(bfs)

    Problem Description <Journey to the West>(also <Monkey>) is one of the Four Great Classi ...

  7. 2014 网选 广州赛区 hdu 5025 Saving Tang Monk(bfs+四维数组记录状态)

    /* 这是我做过的一道新类型的搜索题!从来没想过用四维数组记录状态! 以前做过的都是用二维的!自己的四维还是太狭隘了..... 题意:悟空救师傅 ! 在救师父之前要先把所有的钥匙找到! 每种钥匙有 k ...

  8. HDU 5025 Saving Tang Monk --BFS

    题意:给一个地图,孙悟空(K)救唐僧(T),地图中'S'表示蛇,第一次到这要杀死蛇(蛇最多5条),多花费一分钟,'1'~'m'表示m个钥匙(m<=9),孙悟空要依次拿到这m个钥匙,然后才能去救唐 ...

  9. HDU 5025:Saving Tang Monk(BFS + 状压)

    http://acm.hdu.edu.cn/showproblem.php?pid=5025 Saving Tang Monk Problem Description   <Journey to ...

随机推荐

  1. 转 v$session_longops视图

    转http://www.dbdream.com.cn/2013/10/14/vsession_longops%E8%A7%86%E5%9B%BE/ 1.有的时候不准确 ,我看到 session wai ...

  2. 类成员函数的重载、覆盖和隐藏区别 (C++)

    这是本人第一次写博客,主要是想记录自己的学习过程.心得体会,一是可以方便以后回顾相关知识,二是可以与大家相互学习交流. 关于C++中类成员函数的重载.覆盖和隐藏区别,第一次看到这个问题是在准备找工作的 ...

  3. SQLServer数据库表字段超长,找到超长字段脚本

    平时开发系统时偶尔会遇到数据超长导致往数据库中保存时出错. 使用下边的脚本可以方便的找出超长的字段. 1.通过正式表创建临时表,修改临时表中varchar.nvarchar的长度为max ); ); ...

  4. java jmap

    jmap : 命令用于生成堆转储快照.它还可以查询finalize执行队列.Java堆和永久代的详细信息,如空间使用率.当前用的是哪种收集器等. 命令格式: jmap [option] vmid op ...

  5. python-day1作业(感谢视频老师留的作业)

    __author__ = 'zht' #!/usr/bin/env python # -*- coding: utf-8 -*- ''' #努力学习每一天 ''' #尝试次数计数器 tries = 0 ...

  6. Vue的computed和methods区别

    1,computed里面定义的方法是以属性的方式(当然也可以以函数调用的方式)出现在html里面,而methods里面定义的方法是以函数的方式: 2,computed依赖于data里面的数据,只有相关 ...

  7. mysqldatadir 转移

    当mysql data路径与原始目录不一致时 ,请在mysql 安装目录下my-default.ini 进行设置,取消对应#注释的地址,设置新地址,保存,重新启动,即可. 从网上各种搜索啊,各种尝试, ...

  8. CRC检错技术原理

    一.题外话 说来惭愧,一开始是考虑写关于CRC检错技术更深层次数学原理的,然而在翻看<Basic Algebra>后,我果断放弃了这种不切实际的想法.个人觉得不是因为本人数学水平差或者能力 ...

  9. Jenkins上svn更新策略说明

  10. SqlServer中生成一串连续数字

    在SQLServer中一串连续数字,如1,2,3,4,5,....或者 1 2 3 4 5 没有现成方法,网上都用通用表表达式递归生成.今天想到一个还算简单的方法,记录下来: select row_n ...