hdoj 5094 Maze 【BFS + 状态压缩】 【好多坑】
Maze
Spock, the deputy captain of Starship Enterprise, fell into Klingon’s trick and was held as prisoner on their mother planet Qo’noS.
The captain of Enterprise, James T. Kirk, had to fly to Qo’noS to rescue his deputy. Fortunately, he stole a map of the maze where Spock was put in exactly.
The maze is a rectangle, which has n rows vertically and m columns horizontally, in another words, that it is divided into n*m locations. An ordered pair (Row No., Column No.) represents a location in the maze. Kirk moves from current location to next costs
1 second. And he is able to move to next location if and only if:
Next location is adjacent to current Kirk’s location on up or down or left or right(4 directions)
Open door is passable, but locked door is not.
Kirk cannot pass a wall
There are p types of doors which are locked by default. A key is only capable of opening the same type of doors. Kirk has to get the key before opening corresponding doors, which wastes little time.
Initial location of Kirk was (1, 1) while Spock was on location of (n, m). Your task is to help Kirk find Spock as soon as possible.
Each test case consists of several lines. Three integers are in the first line, which represent n, m and p respectively (1<= n, m <=50, 0<= p <=10).
Only one integer k is listed in the second line, means the sum number of gates and walls, (0<= k <=500).
There are 5 integers in the following k lines, represents xi1, yi1, xi2, yi2, gi; when gi >=1, represents there is a gate of type gi between location (xi1, yi1) and (xi2,
yi2); when gi = 0, represents there is a wall between location (xi1, yi1) and (xi2, yi2), ( | xi1 - xi2 | + | yi1 - yi2 |=1, 0<= gi <=p
)
Following line is an integer S, represent the total number of keys in maze. (0<= S <=50).
There are three integers in the following S lines, represents xi1, yi1 and qi respectively. That means the key type of qi locates on location (xi1, yi1), (1<= qi<=p).
If there is no possible plan, output -1.
4 4 9
9
1 2 1 3 2
1 2 2 2 0
2 1 2 2 0
2 1 3 1 0
2 3 3 3 0
2 4 3 4 1
3 2 3 3 0
3 3 4 3 0
4 3 4 4 0
2
2 1 2
4 2 1
14
WA了两次。
。。
注意状态压缩处理二进制时,对门和钥匙的编号要减一处理(不要问我为什么)。
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
#define MAXN 51
using namespace std;
struct Node
{
int x, y, step, key;
friend bool operator < (Node a, Node b)
{
return a.step > b.step;
}
};
int Map[MAXN][MAXN];
int pos[MAXN][MAXN][10];//存储该位置拥有的 钥匙种类
bool vis[MAXN][MAXN][1<<10];//最多10个门
int rec[MAXN][MAXN][MAXN][MAXN];// -2表示两点间有墙 大于或等于0表示两点间有门 -1表示什么都没有
int N, M, p, k;
void getMap()
{
int x, y, x1, y1, x2, y2, op;
memset(Map, 0, sizeof(Map));//0表示该位置什么都没有
memset(rec, -1, sizeof(rec));//-1表示两个位置之间 什么都没有
scanf("%d", &k);
for(int i = 1; i <= k; i++)//k个地方有门 或者 有墙
{
scanf("%d%d%d%d%d", &x1, &y1, &x2, &y2, &op);
if(op == 0)//墙
rec[x1][y1][x2][y2] = rec[x2][y2][x1][y1] = -2;
else//门
rec[x1][y1][x2][y2] = rec[x2][y2][x1][y1] = op-1;
}
memset(pos, 0, sizeof(pos));
int S;//钥匙数目
scanf("%d", &S);
while(S--)
{
scanf("%d%d%d", &x, &y, &op);
Map[x][y] = 1;
if(!pos[x][y][op-1])//该钥匙 还没有
pos[x][y][op-1] = 1;
}
}
bool judge(Node a)//是否越界
{
return a.x >= 1 && a.x <= N && a.y >= 1 && a.y <= M;
}
int wall_or_door(Node a, Node b)//推断两个点间是否有 墙 或有门 或什么都没有
{
return rec[a.x][a.y][b.x][b.y];
}
void BFS(int x, int y)
{
priority_queue<Node> Q;
int move[4][2] = {0,1, 0,-1, 1,0, -1,0};
memset(vis, false, sizeof(vis));
Node now, next;
now.x = x, now.y = y, now.step = 0, now.key = 0;
if(Map[now.x][now.y])//起点可能有钥匙 这里是个坑
{
for(int i = 0; i < 10; i++)
{
if(pos[now.x][now.y][i])
now.key |= (1 << i);
}
}
Q.push(now);
vis[now.x][now.y][now.key] = true;
while(!Q.empty())
{
now = Q.top();
Q.pop();
if(now.x == N && now.y == M)//到达终点
{
printf("%d\n", now.step);
return ;
}
for(int k = 0; k < 4; k++)
{
next.x = now.x + move[k][0];
next.y = now.y + move[k][1];
next.step = now.step + 1;
int t = wall_or_door(now, next);
if(judge(next) && t != -2)//不能越界且中间不能有墙
{
//分有门 和 没有门 来讨论
if(t >= 0)//有门
{
next.key = now.key;
//先看是否有钥匙 要保证能到达目标位置
if(next.key & (1 << t))//有钥匙
{
//推断目标位置是否有钥匙
if(Map[next.x][next.y])//有钥匙 收集钥匙
{
for(int i = 0; i < 10; i++)
{
if(pos[next.x][next.y][i])
next.key |= (1 << i);
}
}
if(!vis[next.x][next.y][next.key])//推断该状态 是否出现过
{
vis[next.x][next.y][next.key] = true;
Q.push(next);
}
}
//没有钥匙这条路眼下不能走
}
else//没有门
{
next.key = now.key;
if(Map[next.x][next.y])//目标位置有钥匙
{
for(int i = 0; i < 10; i++)
{
if(pos[next.x][next.y][i])
next.key |= (1 << i);
}
}
if(!vis[next.x][next.y][next.key])
{
vis[next.x][next.y][next.key] = true;
Q.push(next);
}
}
}
}
}
printf("-1\n");
}
int main()
{
while(scanf("%d%d%d", &N, &M, &p) != EOF)
{
getMap();
BFS(1, 1);
}
return 0;
}
hdoj 5094 Maze 【BFS + 状态压缩】 【好多坑】的更多相关文章
- ACM/ICPC 之 BFS+状态压缩(POJ1324(ZOJ1361))
求一条蛇到(1,1)的最短路长,题目不简单,状态较多,需要考虑状态压缩,ZOJ的数据似乎比POj弱一些 POJ1324(ZOJ1361)-Holedox Moving 题意:一条已知初始状态的蛇,求其 ...
- HDU1429+bfs+状态压缩
bfs+状态压缩思路:用2进制表示每个钥匙是否已经被找到.. /* bfs+状态压缩 思路:用2进制表示每个钥匙是否已经被找到. */ #include<algorithm> #inclu ...
- BFS+状态压缩 hdu-1885-Key Task
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1885 题目意思: 给一个矩阵,给一个起点多个终点,有些点有墙不能通过,有些点的位置有门,需要拿到相应 ...
- 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 ...
- BFS+状态压缩 HDU1429
胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
- HDU 3247 Resource Archiver (AC自己主动机 + BFS + 状态压缩DP)
题目链接:Resource Archiver 解析:n个正常的串.m个病毒串,问包括全部正常串(可重叠)且不包括不论什么病毒串的字符串的最小长度为多少. AC自己主动机 + bfs + 状态压缩DP ...
- hdoj 1429 胜利大逃亡(续) 【BFS+状态压缩】
题目:pid=1429">hdoj 1429 胜利大逃亡(续) 同样题目: 题意:中文的,自己看 分析:题目是求最少的逃亡时间.确定用BFS 这个题目的难点在于有几个锁对于几把钥匙.唯 ...
- HDOJ 1429 胜利大逃亡(续) (bfs+状态压缩)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1429 思路分析:题目要求找出最短的逃亡路径,但是与一般的问题不同,该问题增加了门与钥匙约束条件: 考虑 ...
- hdu - 1429 胜利大逃亡(续) (bfs状态压缩)
http://acm.hdu.edu.cn/showproblem.php?pid=1429 终于开始能够做状态压缩的题了,虽然这只是状态压缩里面一道很简单的题. 状态压缩就是用二进制的思想来表示状态 ...
随机推荐
- hibernate基础工具findBySQL学习
public List<Map<String,Object>> findBySQL(String sql,Map<String,Object> param,int ...
- HDU-2853 Assignment
求二分最大匹配,但还要尽量接近原匹配... 解决方法:对于N个顶点的二分图,每条边同时乘上一个比N稍微大的数N',然后对于在原匹配的边就都+1. 经过这样处理,求得的答案Ans乘除N'即是原图的最大匹 ...
- ubuntu14.04 python + opencv 傻瓜式安装解决方案
ubuntu14.04 python + opencv 傻瓜式安装解决方案 ubuntu下使python和opencv来做开发的话,总要花那么点时间来配置环境.我偶然间发现了一种傻瓜式安装办法希望快 ...
- [AHOI2008]逆序对(dp)
小可可和小卡卡想到Y岛上旅游,但是他们不知道Y岛有多远.好在,他们找到一本古老的书,上面是这样说的: 下面是N个正整数,每个都在1~K之间.如果有两个数A和B,A在B左边且A大于B,我们就称这两个数为 ...
- linux监控平台搭建-磁盘
linux监控平台搭建-磁盘 磁盘:随着大数据快速发展.人工智能.自动化.云平台.数据量指数的增长.磁盘的使用量也在增长.目前的机器基本上采用SSD或者SATA盘,一直有人比较那个好.会不会使用时间短 ...
- 如何发布自己的服务---zookeeper
人肉告知的方式:如果你发现你的服务一台机器不够,要再添加一台,这个时候就要告诉调用者我现在有两个ip了,你们要轮询调用来实现负载均衡:调用者咬咬牙改了,结果某天一台机器挂了,调用者发现服务有一半不可用 ...
- SharePoint 2013 App 开发—Auto Hosted 方式
Auto Hosted 方式,自动使用Windows Azure来作为host,这种模式将App 发布到Office 365上的SharePoint Developer Site上.这种方式可以不用花 ...
- 【HDOJ6217】BBP Formula(公式)
题意:给定一个无穷项的分式,它的和等于π,问π的十六进制表示的小数点后第n位是多少 1 ≤ n ≤ 100000 思路:From https://blog.csdn.net/meopass/artic ...
- 快充 IC BQ25896 如何判斷 手機插著 adapter 充電器時,adapter Iout 大於限制,adapter Vout 小於 限制,導致 battery 不但沒充電且還需放電。
若電池在 放電時,ICHGR 為0. 若電池在 充電時,ICHGR有變化. 下面有寫到 charge current 所以不是 discharge current 狀況: ...
- /sys/class/gpio 文件接口操作IO端口(s3c2440)
http://blog.csdn.net/mirkerson/article/details/8464231 在嵌入式设备中对GPIO的操作是最基本的操作.一般的做法是写一个单独驱动程序,网上大多数的 ...