题目链接:http://lightoj.com/volume_showproblem.php?problem=1426

思路:首先我们预处理出每一个"*"在某一方向上最终能到达的位置,这里我们可以用一个四维数组来记录next[i][j][k][2],然后首先判断"impossible"这种情况,我们可以对每个"*"进行dfs,看是否能够到达边界,如果存在某个“*”不能到达边界,那么直接就是"impossible“了。判断好这个之后就可以直接bfs求解了,这里我们用map<vector<int,int>,string >mp来判重,我们可以枚举4个方向,然后对于那些不能到达边界的点,加入到一个vector向量中,如果最后我们能够找到一个为空的vector,那么说明找到了这样的指令可以是所有的"*"都能按照这样的指令到达边界。

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <algorithm>
  5. #include <vector>
  6. #include <queue>
  7. #include <string>
  8. #include <map>
  9. using namespace std;
  10.  
  11. const int MAXN = ;
  12. typedef pair<int,int >Pair;
  13.  
  14. char Map[MAXN][MAXN];
  15. int n,m;
  16. int dir[][] = { {,},{-,},{,},{,-} };
  17. vector<Pair >Pos;
  18. int next[MAXN][MAXN][][];
  19. bool Judge(int x, int y)
  20. {
  21. if(x >= &&x <= n&&y >= &&y <=m)
  22. return true;
  23. return false;
  24. }
  25. bool mark[MAXN][MAXN];
  26. bool dfs(int x, int y)
  27. {
  28. mark[x][y] = true;
  29. for(int i = ; i < ; i++){
  30. int xx = next[x][y][i][];
  31. int yy = next[x][y][i][];
  32. if(!Judge(xx,yy))return true;
  33. if(mark[xx][yy])continue;
  34. if(dfs(xx,yy))return true;
  35. }
  36. return false;
  37. }
  38.  
  39. bool Check()
  40. {
  41. for(int i = ; i <(int)Pos.size(); i++){
  42. Pair pp = Pos[i];
  43. memset(mark, false, sizeof(mark));
  44. int xx = pp.first, yy = pp.second;
  45. if(!dfs(xx,yy))return false;
  46. }
  47. return true;
  48. }
  49.  
  50. string Dir="ENSW";
  51. map<vector<Pair >, string >mp;
  52. queue<vector<Pair > >que;
  53.  
  54. void bfs()
  55. {
  56. mp.clear();
  57. while(!que.empty())que.pop();
  58. mp[Pos]="";
  59. que.push(Pos);
  60. while(!que.empty()){
  61. vector<Pair >q, p = que.front();
  62. que.pop();
  63. if((int)p.size() == ){
  64. cout << mp[p] << endl;
  65. return ;
  66. }
  67. for(int i = ; i < ; i++){
  68. q.clear();
  69. for(int j = ; j <(int)p.size(); j++){
  70. Pair pp = p[j];
  71. int xx = next[pp.first][pp.second][i][];
  72. int yy = next[pp.first][pp.second][i][];
  73. if(!Judge(xx,yy))continue;
  74. q.push_back(make_pair(xx,yy));
  75. }
  76. sort(q.begin(), q.end());
  77. q.erase(unique(q.begin(),q.end()),q.end());
  78. if(mp.find(q) == mp.end()){
  79. mp[q] = mp[p] + Dir[i];
  80. que.push(q);
  81. }
  82. }
  83. }
  84. puts("Impossible");
  85. }
  86.  
  87. int main()
  88. {
  89. int _case,t=;
  90. scanf("%d", &_case);
  91. while(_case--){
  92. Pos.clear();
  93. scanf("%d %d", &n, &m);
  94. for(int i = ; i <= n; i++){
  95. scanf("%s", Map[i] + );
  96. }
  97. for(int i = ; i <= n; i++){
  98. for(int j = ; j <= m; j++){
  99. if(Map[i][j] != '#'){
  100. Pos.push_back(make_pair(i,j));
  101. for(int k = ; k < ; k++){
  102. int x = i, y = j;
  103. while(Judge(x,y)){
  104. if(Map[x][y] == '#'){
  105. x -= dir[k][];
  106. y -= dir[k][];
  107. break;
  108. }
  109. x += dir[k][];
  110. y += dir[k][];
  111. }
  112. next[i][j][k][] = x;
  113. next[i][j][k][] = y;
  114. }
  115. }
  116. }
  117. }
  118. printf("Case %d: ", t++);
  119. if(!Check()){
  120. puts("Impossible");
  121. continue;
  122. }
  123. bfs();
  124. }
  125. return ;
  126. }

loj 1426(dfs + bfs)的更多相关文章

  1. DFS/BFS+思维 HDOJ 5325 Crazy Bobo

    题目传送门 /* 题意:给一个树,节点上有权值,问最多能找出多少个点满足在树上是连通的并且按照权值排序后相邻的点 在树上的路径权值都小于这两个点 DFS/BFS+思维:按照权值的大小,从小的到大的连有 ...

  2. 【DFS/BFS】NYOJ-58-最少步数(迷宫最短路径问题)

    [题目链接:NYOJ-58] 经典的搜索问题,想必这题用广搜的会比较多,所以我首先使的也是广搜,但其实深搜同样也是可以的. 不考虑剪枝的话,两种方法实践消耗相同,但是深搜相比广搜内存低一点. 我想,因 ...

  3. ID(dfs+bfs)-hdu-4127-Flood-it!

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4127 题目意思: 给n*n的方格,每个格子有一种颜色(0~5),每次可以选择一种颜色,使得和左上角相 ...

  4. [LeetCode] 130. Surrounded Regions_Medium tag: DFS/BFS

    Given a 2D board containing 'X' and 'O' (the letter O), capture all regions surrounded by 'X'. A reg ...

  5. HDU 4771 (DFS+BFS)

    Problem Description Harry Potter has some precious. For example, his invisible robe, his wand and hi ...

  6. DFS/BFS视频讲解

    视频链接:https://www.bilibili.com/video/av12019553?share_medium=android&share_source=qq&bbid=XZ7 ...

  7. POJ 3083 -- Children of the Candy Corn(DFS+BFS)TLE

    POJ 3083 -- Children of the Candy Corn(DFS+BFS) 题意: 给定一个迷宫,S是起点,E是终点,#是墙不可走,.可以走 1)先输出左转优先时,从S到E的步数 ...

  8. [LeetCode]695. 岛屿的最大面积(DFS/BFS)、200. 岛屿数量(DFS/BFS待做/并差集待做)

    695. 岛屿的最大面积 题目 给定一个包含了一些 0 和 1的非空二维数组 grid , 一个 岛屿 是由四个方向 (水平或垂直) 的 1 (代表土地) 构成的组合.你可以假设二维矩阵的四个边缘都被 ...

  9. POJ2308连连看dfs+bfs+优化

    DFS+BFS+MAP+剪枝 题意:       就是给你一个10*10的连连看状态,然后问你最后能不能全部消没? 思路:      首先要明确这是一个搜索题目,还有就是关键的一点就是连连看这个游戏是 ...

随机推荐

  1. CEF3开发者系列之JS与C++交互之一

    JS与Native交互是相对于比较困难的技术,在学习这门技术之前,我们先了解下浏览器内核中的JS引擎与chromium内核的V8引擎相关知识.在浏览器应用中,JS与本地代码互相调用,得益于浏览器内核对 ...

  2. gtk+-3.21.4 static build step in windows XP

    In recent days the weather is very hot Unable to sleep properly Under the state of daze research gtk ...

  3. 【Android Studio错误】 If you are behind an HTTP proxy, please configure the proxy settings either in IDE or Gradle.

    解决办法:以管理员身份运行cmd窗口,输入命令“netsh winsock reset” netsh winsock reset命令,作用是重置 Winsock 目录.如果一台机器上的Winsock协 ...

  4. Mathematics:DNA Sorting(POJ 1007)

    DNA排序 题目大意:给定多个ACGT序列,按照字母顺序算出逆序数,按逆序数从小到大排列 这题其实很简单,我们只要用一个归并排序算逆序数,然后快排就可以了(插入排序也可以,数据量不大),但是要注意的是 ...

  5. codeforces 507B. Amr and Pins 解题报告

    题目链接:http://codeforces.com/problemset/problem/507/B 题目意思:给出圆的半径,以及圆心坐标和最终圆心要到达的坐标位置.问最少步数是多少.移动见下图.( ...

  6. 【leetcode】 Generate Parentheses (middle)☆

    Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...

  7. 【linux】find删除指定时间之前的文件

    今天磁盘满了,想删掉一些老的日志文件.开始想写个python脚本,转念一想,可能shell脚本好点.结果发现,根本不用写脚本,一个find指令就可以解决问题了. 先上指令 -exec rm {} \; ...

  8. UIView CALayer 的区别

    UIView与CALayer的区别,很详细 研究Core Animation已经有段时间了,关于Core Animation,网上没什么好的介绍.苹果网站上有篇专门的总结性介绍,但是似乎原理性的东西不 ...

  9. .NET微信公众号开发-1.0初始微信公众号

    一.前言 微信公众号是开发者或商家在微信公众平台上申请的应用账号,该帐号与QQ账号互通,通过公众号,商家可在微信平台上实现和特定群体的文字.图片.语音.视频的全方位沟通.互动 .形成了一 种主流的线上 ...

  10. java中的[Ljava.lang.Object;@2a139a55问题

    数据显示为Ljava.lang.Object;@2a139a55问题,是因为你从数据库读出数据后,存入到list集合上时,如果你没有指定要存入的数据的类型,系统会自动给你赋一个object类型,他是所 ...