hdu5094 Maze
……就是爬管道……
还好内存给的多……
不然就不会做了……
#include<iostream>
#include<map>
#include<string>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<queue>
#include<vector>
#include<algorithm>
using namespace std;
const int inf=(1<<31)-1;
int dp[51][51][1<<10];
int road[51][51][51][51];
int key[51][51];
int dx[4]={-1,1,0,0};
int dy[4]={0,0,-1,1};
int n,m,ans;
struct node
{
int x,y,k,step;
node(){}
node(int a,int b,int c,int d){x=a;y=b;k=c;step=d;}
};
bool isin(int x,int y)
{
return x>=1&&x<=n&&y>=1&&y<=m;
}
void bfs()
{
int i;
node now,next;
queue<node>qq;
qq.push(node(1,1,key[1][1],0));
memset(dp,-1,sizeof(dp));
while(qq.size())
{
now=qq.front();
qq.pop();
dp[now.x][now.y][now.k]=now.step;
for(i=0;i<4;i++)
{
next=now;
next.x+=dx[i];
next.y+=dy[i];
next.k|=key[next.x][next.y];
next.step++;
if(!isin(next.x,next.y))
continue;
if(road[now.x][now.y][next.x][next.y]==0)
continue;
if(next.x==n&&next.y==m)
{
ans=min(ans,next.step);
continue;
}
if(road[now.x][now.y][next.x][next.y]>0)
{
if(!((next.k>>road[now.x][now.y][next.x][next.y]-1)&1))
continue;
}
if(dp[next.x][next.y][next.k]!=-1)
{
if(next.step>=dp[next.x][next.y][next.k])
continue;
}
qq.push(next);
}
}
}
int main()
{
int p,sx,sy,ex,ey,t,k,x,y;
while(cin>>n>>m>>p)
{
memset(road,-1,sizeof(road));
cin>>t;
while(t--)
{
cin>>sx>>sy>>ex>>ey>>k;
road[sx][sy][ex][ey]=k;
road[ex][ey][sx][sy]=k;
}
cin>>t;
memset(key,0,sizeof(key));
while(t--)
{
cin>>x>>y>>k;
key[x][y]|=1<<k-1;
}
ans=inf;
bfs();
if(ans==inf)
ans=-1;
cout<<ans<<endl;
}
}
Maze
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 100000/100000 K (Java/Others)
Total Submission(s): 329 Accepted Submission(s): 125
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
hdu5094 Maze的更多相关文章
- Backtracking algorithm: rat in maze
Sept. 10, 2015 Study again the back tracking algorithm using recursive solution, rat in maze, a clas ...
- (期望)A Dangerous Maze(Light OJ 1027)
http://www.lightoj.com/volume_showproblem.php?problem=1027 You are in a maze; seeing n doors in fron ...
- 1204. Maze Traversal
1204. Maze Traversal A common problem in artificial intelligence is negotiation of a maze. A maze ...
- uva705--slash maze
/*这道题我原本是将斜线迷宫扩大为原来的两倍,但是在这种情况下对于在斜的方向上的搜索会变的较容易出错,所以参考了别人的思路后将迷宫扩展为原来的3倍,这样就变成一般的迷宫问题了*/ #include&q ...
- HDU 4048 Zhuge Liang's Stone Sentinel Maze
Zhuge Liang's Stone Sentinel Maze Time Limit: 10000/4000 MS (Java/Others) Memory Limit: 32768/327 ...
- Borg Maze(MST & bfs)
Borg Maze Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9220 Accepted: 3087 Descrip ...
- poj 3026 bfs+prim Borg Maze
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9718 Accepted: 3263 Description The B ...
- HDU 4035:Maze(概率DP)
http://acm.split.hdu.edu.cn/showproblem.php?pid=4035 Maze Special Judge Problem Description When w ...
- POJ 3026 : Borg Maze(BFS + Prim)
http://poj.org/problem?id=3026 Borg Maze Time Limit: 1000MS Memory Limit: 65536K Total Submissions ...
随机推荐
- 洛谷 P1603 斯诺登的密码
我一开始还没看懂非正规数字的意义,以为那里写的单词不算,蒙了好久,而且这题非常考验仔细程度,一不小心就RE,WA. 嗯,好像讲了些废话,那我们看看思路,我的做法和前面的大佬们有些不同,因为这题只有六个 ...
- IE 11 使用 flexbox 垂直居中 bug
不要使用 min-height 改为 height 即可 caniuse: https://caniuse.com/#search=flexbox
- linux根据端口查找进程【原创】
如转载请注明地址 1.利用lsof -i:端口号 lsof -i:端口号 [root@01 ~]# lsof -i:8097COMMAND PID USER FD TYPE DEVICE SIZE/O ...
- Python学习一|anaconda的安装问题以及Python语言的特点
安装时遇到的问题 安装anaconda3.0到D盘之后,配置好两个环境变量:D:\anaconda和D:\anaconda\Scripts.发现在命令行中执行python指令可以,但conda指令却是 ...
- 【mysql】source导入多个文件
在mysql中,可以将表导出为sql文件,比如1.sql, 2.sql等等. 导入一个文件: source /home/somepath/.sql 那么问题来了,如果我想一次导入100个文件呢?总不能 ...
- 基于Prometheus的Pushgateway实战
一.Pushgateway 简介 Pushgateway 是 Prometheus 生态中一个重要工具,使用它的原因主要是: Prometheus 采用 pull 模式,可能由于不在一个子网或者防火墙 ...
- ****timeago.js插件:jquery实现几分钟前、几小时前、几天前等时间差显示效果的代码实例
前端 时间个性化 插件 jquery.timeago.js 关键词 : 时间格式化 刚刚 N分钟前 N小时前 N天前 N月前 N年前 MM-dd hh:mm 或者 yyyy-MM-dd 前端: & ...
- T-SQL语句2
一.修改表 1.alter table 语句 alter table database_name,table_name,column_name,type_name//database_name指数据库 ...
- Elasticsearch常用最全最常用工具清单
https://blog.csdn.net/ZYC88888/article/details/82872558
- Codeforces 280C Game on Tree 期望
Game on Tree 这种题好像在wannfly训练营讲过, 我怎么又不会写啦, 我好菜啊啊啊. 我们按每个点算贡献, 一个点有贡献就说明它是被选中的点, 那么它被选中的概率就为1 / depth ...