Maze【BFS 状态压缩】

MazeTime Limit: 2000/1000 MS (Java/Others)Memory Limit: 100000/100000 K (Java/Others)Total Submission(s): 903Accepted Submission(s): 316

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 wallThere 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 991 2 1 3 21 2 2 2 02 1 2 2 02 1 3 1 02 3 3 3 02 4 3 4 13 2 3 3 03 3 4 3 04 3 4 4 022 1 24 2 1

把 map 数组初始化为 -1 了 ,测试样例都过不了,调试了2个多小时才发现 ,真是越来越都比了。

还要注意:一个地方可能多多个不同类型的钥匙,坑啊

#include <cstdio>#include <cstring>#include <algorithm>#include <queue>#define maxn 55using namespace std;int vis[maxn][maxn][1 << 11];int map[maxn][maxn][maxn][maxn];int keynum[maxn][maxn][11];//记录这个地点有哪几种钥匙int dir[4][2] = {0, 1, 0, -1, 1, 0, -1, 0};int n, m, p, s, k;//p代表有几种钥匙。struct node{int x, y ,step, key;friend bool operator < (node a , node b){return a.step > b.step;}};int check(node a, node b){if(b.x <= 0 || b.x > n || b.y <= 0 || b.y > m)return 0;if(map[a.x][a.y][b.x][b.y] == 0)return 0;return 1;}int BFS(){priority_queue<node>q;node now, next;now.x = 1;now.y = 1;now.step = 0;now.key = 0;for(int j = 1; j <= 10; ++j){// 起点有钥匙if(keynum[now.x][now.y][j])now.key = now.key | (1 << j);}q.push(now);vis[now.x][now.y][now.key] = 1;while(!q.empty()){now = q.top();q.pop();if(now.x == n && now.y == m){return now.step;}for(int i = 0; i < 4; ++i){next.x = now.x + dir[i][0];next.y = now.y + dir[i][1];next.step = now.step + 1;//printf("–%d\n", next.step);next.key = now.key;if(check(now, next)){//now 和 next 之间没有墙if(map[now.x][now.y][next.x][next.y] > 0){ //now 和 next 中间有门if(next.key & (1 << map[now.x][now.y][next.x][next.y])){//有这个门的钥匙,,能通过这个门到达nextfor(int j = 1; j <= 10; ++j){ //判断点next是不是有钥匙if(keynum[next.x][next.y][j])next.key = next.key | (1 << j);}if(!vis[next.x][next.y][next.key]){vis[next.x][next.y][next.key] = 1;q.push(next);}}}else {for(int j = 1; j <= 10; ++j){//判断点next是不是有钥匙if(keynum[next.x][next.y][j]){next.key = next.key | (1 << j);}}if(!vis[next.x][next.y][next.key]){vis[next.x][next.y][next.key] = 1;q.push(next);}}}}}return -1;}int main (){while(scanf("%d%d%d", &n, &m, &p) != EOF){scanf("%d", &k);memset(map, -1, sizeof(map));memset(vis, 0, sizeof(vis));memset(keynum, 0, sizeof(keynum));while(k–){int x1, y1, x2, y2, t;scanf("%d%d%d%d%d", &x1, &y1, &x2, &y2, &t);map[x1][y1][x2][y2] = t;map[x2][y2][x1][y1] = t;}scanf("%d", &s);while(s–){int x, y, t;scanf("%d%d%d", &x, &y, &t);keynum[x][y][t] = 1;}printf("%d\n", BFS());}return 0;}

版权声明:本文为博主原创文章,未经博主允许不得转载。

生命不息,奋斗不止,就像我们常说的一句话;

Maze【BFS 状态压缩】

相关文章:

你感兴趣的文章:

标签云: