1. 程式人生 > >hdu 5094 Maze bfs

hdu 5094 Maze bfs

key pid using post target algo 星期 create 才幹

傳送門:上海邀請賽E

給定一個n×m的迷宮,給出相鄰格子之間的墻或者門的信息,墻說明不可走,假設是門則須要有相應的鑰匙才幹通過,問是否可以從(1,1)到達(n,m)


一個帶狀態的bfs,再另記一個狀態表示所帶鑰匙的種類,鑰匙種類數最多僅僅有10,因此能夠用位來表示鑰匙的狀態。

/******************************************************
 * File Name:   5094.cpp
 * Author:      kojimai
 * Create Time: 2014年11月03日 星期一 09時24分27秒
 ******************************************************/

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<iostream>
#include<queue>
using namespace std;
#define FFF 55
bool vis[FFF][FFF][2048];
int map[FFF][FFF],road[FFF][FFF][4];
int move[4][2] ={-1,0,0,1,1,0,0,-1};//0-up 1-right 2-down 3-left
struct node
{
	int x,y,t,key;
}now,tmp;
queue<node> pp;

int getg(int g)
{
	if(g == 0)
		return -1;
	else
		return 1 << g;
}
void solve(int x1,int y1,int x2,int y2,int g)
{
	if(x1 == x2)
	{
		if(y1<y2)
		{
			road[x1][y1][1] = getg(g);
			road[x2][y2][3] = getg(g);
		}
		else
		{
			road[x1][y1][3] = getg(g);
			road[x2][y2][1] = getg(g);
		}
	}
	else if(x1 < x2)
	{
		road[x1][y1][2] = getg(g);
		road[x2][y2][0] = getg(g);
	}
	else
	{
		road[x1][y1][0] = getg(g);
		road[x2][y2][2] = getg(g);
	}
	return;
}
bool judge(int dir)
{
	if(road[now.x][now.y][dir] == -1)
		return false;
	if(road[now.x][now.y][dir] == 0)
		return true;
	if((road[now.x][now.y][dir] & now.key) == 0)
		return false;
	else
		return true;
}
void printr()
{
	for(int k = 0;k <= 3;k++)
	{
		cout<<" "<<k<<endl;
		for(int i = 1;i <= 4;i++)
		{
			for(int j = 1;j <= 4;j++)
			{
				cout<<road[i][j][k]<<‘ ‘;
			}
			cout<<endl;
		}
	}
}
void printm()
{
	for(int i = 1;i <=  4;i++)
	{
		for(int j = 1;j <= 4;j++)
		{
			cout<<map[i][j]<<‘ ‘;
		}
		cout<<endl;
	}
}

int main()
{
	int n,m,p,s,x1,x2,y1,y2,g,k,key;
	while(~scanf("%d",&n))
	{
		while(!pp.empty()) pp.pop();
		scanf("%d%d",&m,&p);
		scanf("%d",&k);
		memset(map,0,sizeof(map));
		memset(road,0,sizeof(road));
		while(k--)
		{
			scanf("%d%d%d%d%d",&x1,&y1,&x2,&y2,&g);
			solve(x1,y1,x2,y2,g);
		}
		scanf("%d",&s);
		while(s--)
		{
			scanf("%d%d%d",&x1,&y1,&key);
			map[x1][y1] |= (1<<key);
		}
		memset(vis,false,sizeof(vis));
		int ans = -1;
		now.x = 1;now.y = 1;now.key = map[1][1];now.t = 0;
		vis[1][1][now.key] = true;
		pp.push(now);
		//printr();
		//printm();
		while(!pp.empty())
		{
			now = pp.front();pp.pop();
			//cout<<" x = "<<now.x<<" y = "<<now.y<<" t = "<<now.t<<" key = "<<now.key<<endl;
			if(now.x == n&&now.y == m)
			{
				ans = now.t;
				break;
			}
			tmp.t = now.t + 1;
			int xx,yy;
			for(int i = 0;i < 4;i++)
			{
				if(judge(i))
				{
					xx = now.x + move[i][0];
					yy = now.y + move[i][1];
					if(xx <= 0 || xx > n || yy <= 0 || yy > m)
						continue;
					else
					{
						tmp.key = now.key | map[xx][yy];
						tmp.x = xx;
						tmp.y = yy;
						if(!vis[tmp.x][tmp.y][tmp.key])
						{
							vis[tmp.x][tmp.y][tmp.key] = true;
							pp.push(tmp);
						}	
					}
				}
			}
		}	
		cout<<ans<<endl;
	}
	return 0;
}


hdu 5094 Maze bfs