HDU - 5094 (以点和边和方格建图) 特别好的建图技巧

Maze

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


 

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

Source

2014上海全国邀请赛——题目重现(感谢上海大学提供题目)

Recommend

hujie

思路: 在做这个题的时候脑子闪过一个建图的思路就是以每个点、线和 方格建成 一个图。这样的话原本的门和钥匙就可以在一个图中存在。 这样的话就会比较好处理一些。建成的图是原图的  2倍+1   大小。

坑点: 一个方格可以放多个钥匙。

#include<bits/stdc++.h>

using namespace std;
const int N=105;
char mp[N][N];

int vis[N][N][1026];
int yao[N][N];

int n,m,p;

struct node
{
	int x,y;
	int jud;
	int step;
};

int ne[4][2]={1,0, 0,1, -1,0, 0,-1};

int isok(int x,int y,int jud)
{
	if(x<1||x>n||y<1||y>m) return 0;
	if(vis[x][y][jud]) return 0;
	if(mp[x][y]=='#') return 0;
	if(mp[x][y]>='a'&&mp[x][y]<='z'){
		int num=mp[x][y]-'a';
		//printf("lala %d %d %d \n",x,y,jud);
		int x=jud&(1<<num);
		//cout<<"x : "<<x<<endl;
		if(x==0) return 0;
	}
	return 1;
}

void bfs()
{
	memset(vis,0,sizeof(vis));
	int stx=2;    int sty=2;
	int enx=n-1;  int eny=m-1;
	queue< node >q;

	node tmp;
	tmp.x=stx; tmp.y=sty; tmp.jud=0; tmp.step=0;
	vis[tmp.x][tmp.y][tmp.jud]=1;

	q.push(tmp);
	node tmp1;
	int jud,x,y,tx,ty,step;
	while(!q.empty())
	{
		tmp=q.front(); q.pop();
		x=tmp.x; y=tmp.y; jud=tmp.jud; step=tmp.step;
		//cout<<x<<" "<<y<<" "<<jud<<" "<<step<<endl;
		if(x==enx&&y==eny){
			printf("%d\n",step/2);
			return ;
		}


		for(int i=0;i<4;i++)
		{
			tx=x+ne[i][0];  ty=y+ne[i][1];
			//cout<<tx<<" "<<ty<<endl;
			if(isok(tx,ty,jud))
			{
				vis[tx][ty][jud]=1;
				if(mp[tx][ty]>='0'&&mp[tx][ty]<='9'){
					int c=mp[tx][ty]-'0';
					int _jud=(jud|yao[tx][ty]);
					vis[tx][ty][_jud]=1;
					tmp1.x=tx; tmp1.y=ty;  tmp1.step=step+1;  tmp1.jud=_jud;
					q.push(tmp1);
				}
				else{
					tmp1.x=tx; tmp1.y=ty;  tmp1.step=step+1; tmp1.jud=jud;
					q.push(tmp1);
				}
			}
		}

	}

	printf("-1\n");
	return ;
}

int main()
{
	memset(mp,0,sizeof(mp));
	while(scanf("%d %d %d",&n,&m,&p)!=EOF)
	{

    memset(mp,0,sizeof(mp));
	int tot;
	scanf("%d",&tot);
	int tx1,tx2,ty1,ty2;
	int op;
	int fx,fy;
	for(int i=1;i<=tot;i++)
	{
		cin>>tx1>>ty1>>tx2>>ty2>>op;
		if(tx1==tx2)
		{
			fy=min(ty1,ty2)*2+1;
			fx=tx1*2;
		}
		if(ty1==ty2)
		{
			fx=min(tx1,tx2)*2+1;
			fy=ty1*2;
		}

		if(op==0) mp[fx][fy]='#';
		else{
			mp[fx][fy]='a'+op-1;
		}
	}

	for(int i=1;i<=n+1;i++)
	{
		for(int j=1;j<=m+1;j++)
		{
			fx=i*2-1;
			fy=2*j-1;
			mp[fx][fy]='#';
		}
	}

	n=2*n+1;  m=2*m+1;

	// 对钥匙进行初始化
	for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++){
            yao[i][j]=0;
        }
    }

	int k;
	int _nu1,_nu2;
	cin>>k;
	while(k--)
	{
		cin>>tx1>>ty1>>op;
		fx=tx1*2;  fy=ty1*2;
		mp[fx][fy]='0'+op-1;
		_nu1=(1<<(op-1));
		_nu2=(yao[fx][fy]|_nu1);
		yao[fx][fy]=_nu2;
	}

	for(int i=1;i<=n;i++)
	{
		mp[i][1]=mp[i][m]='#';
	}
	for(int j=1;j<=m;j++)
	{
		mp[1][j]=mp[n][j]='#';
	}

	bfs();

	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/yjt9299/article/details/81070764