码迷,mamicode.com
首页 > 其他好文 > 详细

hdu 5094 Maze bfs+状态压缩

时间:2015-05-05 23:59:50      阅读:360      评论:0      收藏:0      [点我收藏+]

标签:bfs   状压   

Maze

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 100000/100000 K (Java/Others)
Total Submission(s): 642    Accepted Submission(s): 229


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 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).
 

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


链接:http://acm.hdu.edu.cn/showproblem.php?pid=5094


很明显的状压bfs,但是错了很多地方,比赛的时候一直wa。  一个是初始点,忘记更新钥匙状态了, 还有就是一个地方== 打成了=。    o(︶︿︶)o 唉


题意:n*m大的迷宫 ,有p种钥匙。钥匙最多有10种,所以可以用状压表示门需要钥匙的状态, 还有已经拥有哪几把钥匙的状态。 

然后下来一个k,然后k行表示  (x1,y1),(x2,y2)直接有门或者墙。  如果g==0 ,就是有墙, 如果g>0 表示有门,且门需要第g把钥匙才能开。 

然后下来一个s,然后s行,表示(x,y)这个点有 第g把钥匙。 

问从(1,1)到(n,m)最少几步。


做法:状压, 每个点有四个方向,记录能否通过的状态, 门的话 标记为-1, 0,表示不用钥匙,>0 的话,哪位上是1,表示要第几把钥匙。

然后结构体key 用二进制表示有了哪几把钥匙

然后lim[i][j][k],表示(i,j)点 有k状态 钥匙  有过没有,  没有就入队,否者不走这个点。


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <malloc.h>
#include <ctype.h>
#include <math.h>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
#include <stack>
#include <queue>
#include <vector>
#include <deque>
#include <set>
#include <map> 


int dir[4][2]={
	0,1,
	-1,0,
	0,-1,
	1,0
}; 
int n,m,p;
int mp[60][60];

int bar[60][60][4];
int lim[60][60][1200];


struct point
{
	int x,y,bu,key;
};
int ok(int x,int y)
{
	if(x<0||y<0||x>=n||y>=m)
		return 0;
	return 1;
}

int bfs()
{
	point sta;
	sta.x=sta.y=0;
	sta.bu=0;
	sta.key=mp[0][0]; 
	point nex,nw; 
	queue<point> q;
	q.push(sta);
	while(!q.empty())
	{
		nw=q.front();
		q.pop(); 
		if(nw.x==n-1&&nw.y==m-1)
			return nw.bu; 
		for(int i=0;i<4;i++)
		{
			int xx=nw.x+dir[i][0];
			int yy=nw.y+dir[i][1];
			if(!ok(xx,yy))
				continue;
			if(bar[nw.x][nw.y][i]==-1)
				continue;
			if((bar[nw.x][nw.y][i]&nw.key)==bar[nw.x][nw.y][i])
			{
				nex=nw;
				nex.x=xx;
				nex.y=yy;
				nex.key|=mp[xx][yy];
				nex.bu++; 
				if(lim[xx][yy][nex.key]==0)
				{
					lim[xx][yy][nex.key]=1;
					q.push(nex);
				}
			}
		}
	}
	return -1;
}
int main()
{
	while(scanf("%d%d%d",&n,&m,&p)!=EOF)
	{
		int k;
		scanf("%d",&k);
		memset(mp,0,sizeof mp);
		memset(lim,0,sizeof lim);
		memset(bar,0,sizeof bar);
		for(int i=0;i<k;i++)
		{
			int x1,x2,y1,y2,g;
			scanf("%d%d%d%d%d",&x1,&y1,&x2,&y2,&g);
			x1--;
			y1--;
			x2--;
			y2--;
			int tem;
			if(g==0)
				tem=-1;
			else
				tem=(1<<(g-1));
			for(int j=0;j<4;j++)
			{
				int xx,yy;
				xx=x1+dir[j][0];
				yy=y1+dir[j][1];
				if(xx==x2&&yy==y2)
				{
					if(tem!=-1)
						bar[x1][y1][j]|=tem;
					else 
						bar[x1][y1][j]|=-1;
				}

				xx=x2+dir[j][0];
				yy=y2+dir[j][1];
				if(xx==x1&&yy==y1)
				{
					if(tem!=-1)
						bar[x2][y2][j]|=tem;
					else 
						bar[x2][y2][j]|=-1;
				}
			}
		} 
		int s;
		scanf("%d",&s);
		for(int i=0;i<s;i++)
		{
			int x,y,q;
			scanf("%d%d%d",&x,&y,&q);
			x--;
			y--;
			mp[x][y]|=(1<<(q-1));
		}
		int ans=bfs();
		printf("%d\n",ans);
	}
	return 0;
}
/*
3 3 2
4
1 2 1 3 0
1 3 2 3 0 
2 3 3 3 1
3 2 3 3 2
2
1 3 1 
3 1 2



*/




hdu 5094 Maze bfs+状态压缩

标签:bfs   状压   

原文地址:http://blog.csdn.net/u013532224/article/details/45509619

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!