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


Problem Description
This story happened on the background of Star Trek.

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.
 

Input
The input contains many test cases.

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 x i1, y i1, x i2, y i2, g i; when g i >=1, represents there is a gate of type gi between location (x i1, y i1) and (x i2, y i2); when g i = 0, represents there is a wall between location (x i1, y i1) and (x i2, y i2), ( | x i1 - x i2 | + | y i1 - y i2 |=1, 0<= g i <=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 x i1, y i1 and q i respectively. That means the key type of q i locates on location (x i1, y i1), (1<= q i<=p).
 

Output
Output the possible minimal second that Kirk could reach Spock. 

If there is no possible plan, output -1. 
 

Sample Input
 
 
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
 

Sample Output
 
 
14
 

Source








本文转自mfrbuaa博客园博客,原文链接:http://www.cnblogs.com/mfrbuaa/p/5406741.html,如需转载请自行联系原作者
相关文章
CodeForces 377A-Maze(DFS找连通块)
CodeForces 377A-Maze(DFS找连通块)
CodeForces 377A-Maze(DFS找连通块)
洛谷P1825-[USACO11OPEN]Corn Maze S(BFS)
洛谷P1825-[USACO11OPEN]Corn Maze S(BFS)
洛谷P1825-[USACO11OPEN]Corn Maze S(BFS)
[HDU 4738] Caocao‘s Bridges | Tarjan 求割边
Problem Description Caocao was defeated by Zhuge Liang and Zhou Yu in the battle of Chibi. But he wouldn’t give up. Caocao’s army still was not good at water battles, so he came up with another idea. He built many islands in the Changjiang river,
141 0
HDOJ(HDU) 2161 Primes(素数打表)
HDOJ(HDU) 2161 Primes(素数打表)
111 0
|
机器学习/深度学习
|
人工智能
|
算法 计算机视觉 存储
【HDU 2586 How far away?】LCA问题 Tarjan算法
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2586 题意:给出一棵n个节点的无根树,每条边有各自的权值。给出m个查询,对于每条查询返回节点u到v的最短路径的权值和,按查询顺序输出结果。
1056 0