【HDU 5094】【Maze】

题目:

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, gi; 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, yi1) 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 irespectively. That means the key type of q i locates on location (x i1, y i1), (1<= qi<=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

解题思路:题目问我们从(1,1)到(n,m)的时间,有的点之间存在墙或者门,墙是无法通过的,而门当有相应的钥匙是可以通过的,这里就用到了状压,本身是bfs,所以跑一下广搜就能得到答案。

ac代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<cmath>
using namespace std;

bool vis[51][51][2500];
struct node{
	int x,y,key;//x,y代表坐标。key代表当前拥有的钥匙串 
	long long cost;//cost是走的步数 
	node(){	;}
	node(int xx,int yy,int keyy,int costt)
	{
		x=xx;
		y=yy;
		key=keyy;
		cost=costt;
	}
}; 
int n,m,key_num;
int c[51][51][51][51];//记录两点之间的物质的存在方式 
int mp[55][55];//地图 放钥匙的存在地点 
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};//方向遍历数组 

bool check(node now,int x,int y)
{
	if(c[now.x][now.y][x][y]==-1)
		return true;//空的话,直接可以走 
	if(c[now.x][now.y][x][y]==0)
		return false;//墙无法通过 
	if(c[now.x][now.y][x][y]>0)
	{
		int key=c[now.x][now.y][x][y];
		if(now.key&(1<<key))
			return true;//如果有钥匙可以走 
		else
			return false;//没钥匙无法通过 
	}
}

long long bfs()
{
	queue<node> que;
	que.push(node(1,1,(1<<mp[1][1]),0));//(1,1)点压入 
	vis[1][1][0]=true;
	while(!que.empty())
	{
		node now=que.front();
		que.pop();
		if(now.x==n&&now.y==m)
			return now.cost;
		int nx,ny;
		for(int i=0;i<4;i++)
		{
			nx=now.x+dx[i];
			ny=now.y+dy[i];
			if(nx>=1&&nx<=n&&ny>=1&&ny<=m)
			{
				if(check(now,nx,ny)&&!vis[nx][ny][now.key])
				{
					node temp=now;
					temp.x=nx;
					temp.y=ny;
					if(mp[nx][ny])//如果有钥匙的话,加入钥匙串 
						temp.key=(temp.key|(mp[nx][ny]));
					temp.cost++;
					que.push(temp);
					vis[nx][ny][temp.key]=true;//标记已经走过了 
				}
			}
		}
	}
	return -1;//没有走到正确的点,返回-1 
}

int main()
{
	while(scanf("%d%d%d",&n,&m,&key_num)!=EOF)
	{
		int k,kk;
		int x1,y1,x2,y2,tp;
		scanf("%d",&k);
		memset(c,-1,sizeof(c));
		for(int i=0;i<k;i++)
		{
			scanf("%d%d%d%d%d",&x1,&y1,&x2,&y2,&tp);
			c[x1][y1][x2][y2]=tp;
			c[x2][y2][x1][y1]=tp;
		}//设置(x1,y1)(x2,y2)之间的隔离物质 
		memset(vis,0,sizeof(vis));
		scanf("%d",&kk);
		memset(mp,0,sizeof(mp));
		for(int i=0;i<kk;i++)
		{
			scanf("%d%d%d",&x1,&y1,&tp);
			mp[x1][y1]=(mp[x1][y1]|(1<<tp));
		}//状压 将mp[x1][y1]这点的值 设置为二进制的数值 
		printf("%lld\n",bfs());
	}
	return 0;
}

注释的非常明白了,不多解释了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值