Swipe Bo

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 1549    Accepted Submission(s): 315

Problem Description
“Swipe Bo” is a puzzle game that requires foresight and skill. 

The main character of this game is a square blue tofu called Bo. We can swipe up / down / left / right to move Bo up / down / left / right. Bo always moves in a straight line and nothing can stop it except a wall. You need to help Bo find the way out.

The picture A shows that we needs three steps to swipe Bo to the exit (swipe up, swipe left, swipe down). In a similar way, we need only two steps to make Bo disappear from the world (swipe left, swipe up)!




Look at the picture B. The exit is locked, so we have to swipe Bo to get all the keys to unlock the exit. When Bo get all the keys, the exit will unlock automatically .The exit is considered inexistent if locked. And you may notice that there are some turning
signs, Bo will make a turn as soon as it meets a 




turning signs. For example, if we swipe Bo up, it will go along the purple line.

Now, your task is to write a program to calculate the minimum number of moves needed for us to swipe Bo to the exit.
 
Input
The input contains multiple cases, no more than 40.

The first line of each test case contains two integers N and M (1≤N, M≤200), which denote the sizes of the map. The next N lines give the map’s layout, with each line containing M characters. A character is one of the following: '#': represents the wall; 'S'
represents the start point of the Bo; 'E' represents the exit; '.' represents an empty block; ‘K’ represents the key, and there are no more than 7 keys in the map; 'L','U','D','R' represents the turning sign with the direction of left, up, down, right.
 
Output
For each test case of the input you have to calculate the minimal amount of moves which are necessary to make Bo move from the starting point to the exit. If Bo cannot reach the exit, output -1. The answer must be written on a single line.
 
Sample Input
5 6
######
#....#
.E...#
..S.##
.#####
5 6
######
#....#
.....#
SEK.##
.#####
5 6
######
#....#
....K#
SEK.##
.#####
5 6
######
#....#
D...E#
S...L#
.#####
 
Sample Output
3
2
7
-1
 
Source
 

题意:有一个迷宫,包括墙、空白格子、起点S、终点E、方向格子(LRUD)和钥匙K。要求例如以下:

(1)每次转弯仅仅能在碰到墙壁时(每次转弯的选择和初始时从S出发的方向选择均称为一次操作);

(2)对于方向格子。若到达该格子,无论周围是不是墙,必须转向该格子指示的方向(这个不算一次操作)。

(3)若迷宫中没有钥匙存在,则求出S到E的最少操作次数;若有钥匙。则必须先遍历到每一个钥匙之后才干去E(在这个过程中能够经过E也就是E不算做障碍)。

#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std; const int N = 225;
const int inf = 1<<29;
struct node{
int x,y,sta,stp;
};
int n,m,k;
char mapt[N][N];
int K[N][N];
bool vist[N][N][1<<7];
int dir[4][2]={0,-1,0,1,-1,0,1,0}; int judge1(node& now,int &e){
int flag=0;
if(now.x<0||now.x>=n||now.y<0||now.y>=m)
return 0;
if(mapt[now.x][now.y]!='#'){ if(mapt[now.x][now.y]=='L')
e=0,flag=1;
else if(mapt[now.x][now.y]=='R')
e=1,flag=1;
else if(mapt[now.x][now.y]=='U')
e=2,flag=1;
else if(mapt[now.x][now.y]=='D')
e=3,flag=1;
else if(mapt[now.x][now.y]=='K')
now.sta|=(1<<K[now.x][now.y]);
if(flag&&vist[now.x][now.y][now.sta])
return 0;
else if(flag) vist[now.x][now.y][now.sta]=1; //固定方向的位置。能够直接标记
return 1;
}
else //遇到墙,退一格,在当前位置停止
{
now.x-=dir[e][0];
now.y-=dir[e][1];
return 2;
} }
int bfs(int sx,int sy){
queue<node>q;
node now,pre; for(int i=0; i<n; i++)
for(int j=0; j<m; j++)
for(int sta=0; sta<(1<<k); sta++)
vist[i][j][sta]=0; now.x=sx,now.y=sy,now.sta=0,now.stp=0;
q.push(now);
vist[now.x][now.y][now.sta]=1; while(!q.empty()){
pre=q.front(); q.pop();
pre.stp++; for(int te=0; te<4; te++){
int e=te;
now=pre;
while(1){ //找到一个停止点。或不能走。或走过了。则跳出
now.x+=dir[e][0];
now.y+=dir[e][1]; int flag=judge1(now,e); if(flag==0)break;
if(flag==1&&mapt[now.x][now.y]=='E'&&now.sta==(1<<k)-1){
return now.stp;
}
if(flag==2){
if(vist[now.x][now.y][now.sta])break;
vist[now.x][now.y][now.sta]=1;
q.push(now);
break;
}
}
}
} return -1;
}
int main()
{
int sx,sy;
while(scanf("%d%d",&n,&m)>0){
k=0;
for(int i=0; i<n; i++){
scanf("%s",mapt[i]);
for(int j=0; j<m; j++)
if(mapt[i][j]=='S')
sx=i,sy=j;
else if(mapt[i][j]=='K')
K[i][j]=k++;
}
printf("%d\n",bfs(sx,sy));
}
}


hdu Swipe Bo(bfs+状态压缩)错了多次的题的更多相关文章

  1. hdu 4634 Swipe Bo bfs+状态压缩

    题目链接 状态压缩记录当前拿到了哪些钥匙, 然后暴力搜索. 搞了好几个小时, 一开始也不知道哪里错了, 最后A了也不知道一开始哪里有问题. #include <iostream> #inc ...

  2. hdu 4856 Tunnels(bfs+状态压缩)

    题目链接:hdu 4856 Tunnels 题目大意:给定一张图,图上有M个管道,管道给定入口和出口,单向,如今有人想要体验下这M个管道,问最短须要移动的距离,起点未定. 解题思路:首先用bfs处理出 ...

  3. hdu 1429(BFS+状态压缩)

    胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Su ...

  4. HDU 3247 Resource Archiver (AC自己主动机 + BFS + 状态压缩DP)

    题目链接:Resource Archiver 解析:n个正常的串.m个病毒串,问包括全部正常串(可重叠)且不包括不论什么病毒串的字符串的最小长度为多少. AC自己主动机 + bfs + 状态压缩DP ...

  5. BFS+状态压缩 hdu-1885-Key Task

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1885 题目意思: 给一个矩阵,给一个起点多个终点,有些点有墙不能通过,有些点的位置有门,需要拿到相应 ...

  6. ACM/ICPC 之 BFS+状态压缩(POJ1324(ZOJ1361))

    求一条蛇到(1,1)的最短路长,题目不简单,状态较多,需要考虑状态压缩,ZOJ的数据似乎比POj弱一些 POJ1324(ZOJ1361)-Holedox Moving 题意:一条已知初始状态的蛇,求其 ...

  7. HDU1429+bfs+状态压缩

    bfs+状态压缩思路:用2进制表示每个钥匙是否已经被找到.. /* bfs+状态压缩 思路:用2进制表示每个钥匙是否已经被找到. */ #include<algorithm> #inclu ...

  8. poj 1753 Flip Game(bfs状态压缩 或 dfs枚举)

    Description Flip game squares. One side of each piece is white and the other one is black and each p ...

  9. BFS+状态压缩 HDU1429

    胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total S ...

随机推荐

  1. HDU 4686

    再不能直视这道题,换INT64就过了....... 同样可以使用矩阵的方法.构造1*5的 D[N],a[n],b[n],a[n]*b[n],1 接着你应该就会了. #include <iostr ...

  2. 鸟书shell 学习笔记(二) shell中正則表達式相关

    通配符与正則表達式的差别 通配符是bash原生支持的语法,正則表達式是处理字符串的一种表示方式, 正則表達式须要支持的工具支持才干够 语系设置 : export LANG=C grep alias 设 ...

  3. Objective-C - 改变NSMutableArray的特定元素

    NSMutableArray改动元素, 使用-insertObject: atIndex: 和-replaceObjectAtIndex: withObject: 都能够, 即通过插入(insert) ...

  4. &lt;LeetCode OJ&gt; 100. Same Tree

    100. Same Tree Total Accepted: 100129 Total Submissions: 236623 Difficulty: Easy Given two binary tr ...

  5. Android学习笔记(十二)——使用意图传递数据的几种方式

    使用意图传递数据的几种方式 点此获取完整代码 我们除了要从活动返回数据,也经常要传递数据给活动.对此我们能够使用Intent对象将这些数据传递给目标活动. 1.创建一个名为PassingData的项目 ...

  6. 英语发音规则---S字母

    英语发音规则---S字母 一.总结 一句话总结: 1.S 在音节开头或清辅音前 /s/? sit /sɪt/ vt.& vi.坐 seat /si:t/ n.席位 sell /sel/ vt. ...

  7. docker中出现容器未运行无法进入容器的操作

  8. BZOJ2440: [中山市选2011]完全平方数(莫比乌斯+容斥原理)

    2440: [中山市选2011]完全平方数 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 4920  Solved: 2389[Submit][Sta ...

  9. 读书笔记第三周 人月神话 刘鼎乾 PB16070837

    读书笔记第三周:人月神话   这本书主要讲述了如何管理一个软件开发团队的问题,其中如何提高团队的效率可以说是本书的重点之一了.感觉这本书地中文版翻译得比较晦涩,很多表达比较模糊,看起来有些吃力,因此下 ...

  10. 快速新建一个纯净的java pom项目 project

    前期的java环境安装就不再阐述了使用步骤java -jar project-creator-0.1.jar projectName [war] 1> 比如你要创建一个项目名字叫 smile-o ...