1. 程式人生 > >BFS+狀態壓縮-Maze(hdu5094)

BFS+狀態壓縮-Maze(hdu5094)

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

題解:

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

總共0-50把鑰匙,但是鑰匙的種類只有0-10種,說明可能1種鑰匙有好幾把。一個地方可能有多個鑰匙。

#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#define maxn 50*50*(1<<11)+5
using namespace std;
struct point
{
	int x, y, step, statu;
}q[maxn];
int map[60][60][60][60], key[60][60], dirx[4] = {1, -1, 0, 0}, diry[4] = {0, 0, 1, -1}, vis[60][60][2055];
int n, m;
int bfs(){  
    int head=0,tail=0;
    int tmp;
    q[0].x=1;
    q[0].y=1;
    q[0].step=0;
    q[0].statu |= key[1][1];//起點有鑰匙的特判 
    memset(vis,0,sizeof(vis));    
    vis[1][1][q[0].statu] = 1;
    do{
        point now= q[head];
        if(now.x == n&&now.y == m) return now.step;
        for(int i = 0;i < 4; i++)
		{
			point nex;
            nex.x = now.x + dirx[i];
            nex.y = now.y + diry[i];
            nex.statu = now.statu;
            nex.step = now.step;
            if(nex.x <= 0 || nex.x > n || nex.y <= 0 || nex.y > m) continue;//超出格子 
            if(map[now.x][now.y][nex.x][nex.y] == 0) continue;//有牆 
            if(map[now.x][now.y][nex.x][nex.y] > 0)//有門 
			{
                tmp = map[now.x][now.y][nex.x][nex.y]-1;
                if((nex.statu&(1<<tmp)) == 0) continue;//沒鑰匙 
            }
            if(key[nex.x][nex.y]>0)
			{
                nex.statu |= key[nex.x][nex.y];
            }
            if(vis[nex.x][nex.y][nex.statu]==1) continue;//以該狀態走過 
            vis[nex.x][nex.y][nex.statu]=1;
            tail++;
            q[tail].x = nex.x;
            q[tail].y = nex.y;
            q[tail].statu = nex.statu;
            q[tail].step = nex.step+1;
        } 
        head++;
    }while(head <= tail);
    return -1;
}
int main()
{
	int p, k, s;
	while(cin>>n>>m>>p)
	{
		memset(map, -1, sizeof(map));
		memset(key, 0, sizeof(key));
		cin>>k;
		for(int i = 1; i <= k; i++)
		{
			int x1, y1, x2, y2, g;
			cin>>x1>>y1>>x2>>y2>>g;
			map[x1][y1][x2][y2] = g;
			map[x2][y2][x1][y1] = g;
		}
		cin>>s;
		for(int i = 1; i <= s; i++)
		{
			int x1, y1, ke;
			cin>>x1>>y1>>ke;
			key[x1][y1] |= (1<<(ke-1));//一個位置多個鑰匙 
		}
		cout<<bfs()<<endl;
	}
	return 0;
}