状态压缩(1)--hdu5094(状态压缩+bfs)(能力题)

                                                         Maze

                                                   Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 100000/100000 K (Java/Others)



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 x i1, y i1, x i2, y i2, g i; 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, y i1) 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 i respectively. That means the key type of q i locates on location (x i1, y i1), (1<= q i<=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),任意两个格子之间的边视为:通路,门,墙。通路可以直接走,门必须早到相应的钥匙,墙永远不能通过。钥匙在一些给定点的格子中(同一个格子中可能有多把钥匙),只有走到这些格子才能获得钥匙,问采取怎样的走法可以得到最少的移动步数 ,另外须注意两个格子之间可能有多个门。
解题思路:vis[x][y][s]表示状态为s时到达(x,y)点是否已经到达过,s表示钥匙的得到情况的状态。然后进行bfs即可 
#include   
#include   
#include  
using namespace std;  
  
int b[]={1,2,4,8,16,32,64,128,256,512,1024,2048};   //每个十进制数对应的二进制表示对应的钥匙或门
int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};  //上下左右四个方向
struct node  
{  
    int x,y,step,key;  
};  
 //wall数组表示两点之间的门或墙 map数组表示该点拥有的钥匙 hash数组作为标记
int wall[51][51][51][51],map[101][101],hash[51][51][2050]; 
int n,m;  
int bfs()  //广搜
{  
    queueq;  
    node cur,next;  
    int i,w;  
  
    cur.x=1;  
    cur.y=1;  
    cur.step=0;  
    cur.key=0;  
    cur.key|=map[1][1];  
    q.push(cur);  
    memset(hash,0,sizeof(hash));  
    hash[1][1][cur.key]=1;  
  
    while (!q.empty())  
    {  
        cur=q.front();  
        q.pop();  
  
        for (i=0;i<4;i++)  
        {  
            next.x=cur.x+dir[i][0];  
            next.y=cur.y+dir[i][1];  
            if (next.x<1 || next.y<1 || next.x>n || next.y>m) continue;  
            w=wall[cur.x][cur.y][next.x][next.y];  
            if ((w&1)==1) continue;    //如果两个格子之间有墙,则不能从一个格子到另一个格子
            if (w>0 && ((cur.key&w)!=w) ) continue;  //判断拥有的钥匙能否开格子之间的所有门
            next.key=cur.key;  
            next.key|=map[next.x][next.y];  
            if (hash[next.x][next.y][next.key]==1) continue;  
            hash[next.x][next.y][next.key]=1;  
            next.step=cur.step+1;  
            if (next.x==n && next.y==m) return next.step;  
            q.push(next);  
        }  
    }  
    return -1;  //不能达到
}  
  
int main()  
{  
    int k,i,x1,x2,y1,y2,g,s,x,y,p;  
    while (scanf("%d%d%d",&n,&m,&p)!=EOF)  
    {  
        scanf("%d",&k);  
        memset(wall,0,sizeof(wall));  
        for (i=1;i<=k;i++)  
        {  
            scanf("%d%d%d%d%d",&x1,&y1,&x2,&y2,&g);  
            wall[x1][y1][x2][y2]|=b[g];  
            wall[x2][y2][x1][y1]|=b[g];  
        }  
        memset(map,0,sizeof(map));  
        scanf("%d",&s);  
        for (i=1;i<=s;i++)  
        {  
            scanf("%d%d%d",&x,&y,&g);  
            map[x][y]|=b[g];  
        }  
        if (n==1 && m==1){  printf("0\n"); continue;} 
        printf("%d\n",bfs());  
		
    }  
    return 0;  
}  


你可能感兴趣的:(【状态压缩】)