题目链接: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,那么说明找到了这样的指令可以是所有的"*"都能按照这样的指令到达边界。

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <string>
#include <map>
using namespace std; const int MAXN = ;
typedef pair<int,int >Pair; char Map[MAXN][MAXN];
int n,m;
int dir[][] = { {,},{-,},{,},{,-} };
vector<Pair >Pos;
int next[MAXN][MAXN][][];
bool Judge(int x, int y)
{
if(x >= &&x <= n&&y >= &&y <=m)
return true;
return false;
}
bool mark[MAXN][MAXN];
bool dfs(int x, int y)
{
mark[x][y] = true;
for(int i = ; i < ; i++){
int xx = next[x][y][i][];
int yy = next[x][y][i][];
if(!Judge(xx,yy))return true;
if(mark[xx][yy])continue;
if(dfs(xx,yy))return true;
}
return false;
} bool Check()
{
for(int i = ; i <(int)Pos.size(); i++){
Pair pp = Pos[i];
memset(mark, false, sizeof(mark));
int xx = pp.first, yy = pp.second;
if(!dfs(xx,yy))return false;
}
return true;
} string Dir="ENSW";
map<vector<Pair >, string >mp;
queue<vector<Pair > >que; void bfs()
{
mp.clear();
while(!que.empty())que.pop();
mp[Pos]="";
que.push(Pos);
while(!que.empty()){
vector<Pair >q, p = que.front();
que.pop();
if((int)p.size() == ){
cout << mp[p] << endl;
return ;
}
for(int i = ; i < ; i++){
q.clear();
for(int j = ; j <(int)p.size(); j++){
Pair pp = p[j];
int xx = next[pp.first][pp.second][i][];
int yy = next[pp.first][pp.second][i][];
if(!Judge(xx,yy))continue;
q.push_back(make_pair(xx,yy));
}
sort(q.begin(), q.end());
q.erase(unique(q.begin(),q.end()),q.end());
if(mp.find(q) == mp.end()){
mp[q] = mp[p] + Dir[i];
que.push(q);
}
}
}
puts("Impossible");
} int main()
{
int _case,t=;
scanf("%d", &_case);
while(_case--){
Pos.clear();
scanf("%d %d", &n, &m);
for(int i = ; i <= n; i++){
scanf("%s", Map[i] + );
}
for(int i = ; i <= n; i++){
for(int j = ; j <= m; j++){
if(Map[i][j] != '#'){
Pos.push_back(make_pair(i,j));
for(int k = ; k < ; k++){
int x = i, y = j;
while(Judge(x,y)){
if(Map[x][y] == '#'){
x -= dir[k][];
y -= dir[k][];
break;
}
x += dir[k][];
y += dir[k][];
}
next[i][j][k][] = x;
next[i][j][k][] = y;
}
}
}
}
printf("Case %d: ", t++);
if(!Check()){
puts("Impossible");
continue;
}
bfs();
}
return ;
}

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. Ebase Xi The world's best RAD platform.

    Ebase Xi : http://www.ebasetech.com/ The world's best RAD platform. Xi combines browser and mobile a ...

  2. TokuDB 引擎安装测试

    背景: TokuDB引擎是有Tokutek开发的一个数据库存储引擎,在设计之初便引入了独特的索引算法,在其官网测试的文章中看到TokuDB性能比InnoDB高出很多. MySQL是一个插件式的数据库, ...

  3. Divide and conquer:Garland(POJ 1759)

     挂彩灯 题目大意:就是要布场的时候需要挂彩灯,彩灯挂的高度满足: H1 = A Hi = (Hi-1 + Hi+1)/2 - 1, for all 1 < i < N HN = B Hi ...

  4. MST:Bad Cowtractors(POJ 2377)

    坏的牛圈建筑 题目大意:就是现在农夫又要牛修建牛栏了,但是农夫想不给钱,于是牛就想设计一个最大的花费的牛圈给他,牛圈的修理费用主要是用在连接牛圈上 这一题很简单了,就是找最大生成树,把Kruskal算 ...

  5. windows系统下ftp上传下载和一些常用命令

    先假设一个ftp地址 用户名 密码 FTP Server: home4u.at.china.com User: yepanghuang Password: abc123 打开windows的开始菜单, ...

  6. cocos2d-x 第二篇 HelloWorld的流程

    这篇博客主要是带领大家一起了解整个游戏的执行过程,其中涉及的一些譬如导演,场景,层之类的概念将会在后面讲解. 看main函数的区别: #import <UIKit/UIKit.h> // ...

  7. .net在当前日期的基础上加一天

    比如今天是:2015-11-10 18:57:01,在这个基础上加一天,那么就是2015-11-11 18:57:01,代码如下: DateTime now_dt = DateTime.Now; ). ...

  8. iOS第三方分享-ShareSDK

    网址链接:http://mob.com/Download/detail?type=1&plat=2 由于新版的简单分享很多功能都有,而且打包后体积比全版本的少了几M所以在这里用这个 1.在官网 ...

  9. September 8th 2016 Week 37th Thursday

    The secret of high-impact business is early preparation. 高效商务,赢在未雨绸缪. Early and best preparation is ...

  10. mongodb3.x版本用户管理方法

    db.auth() 作用:验证用户到数据库. 语法: db.auth( { user: <username>, pwd: <password>, mechanism: < ...