zoukankan      html  css  js  c++  java
  • HDU

    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
    #include <iostream>
    #include <cstring>
    #include <cstdio>
    #include <cmath>
    #include <queue>
    
    using namespace std;
    
    const int MAXN = 55;
    
    struct Node{
    	int x,y,step,key;//分别记录当前位置,步数,钥匙拥有情况 
    	Node(){}
    	Node(int a,int b,int c,int d):x(a),y(b),step(c),key(d){}
    };
    
    int FX[4][2] = {{-1 , 0} , {1 , 0} , {0 , 1} , {0 , -1}};
    int Map[MAXN][MAXN][5];
    bool flag[MAXN][MAXN][1<<10];//表示当前获得钥匙的状态,防止在同一状态下重复访问
    int N,M,P;
    
    int BFS(){
    	queue<Node> Q;
    	Q.push(Node(1,1,0,Map[1][1][4]));
    	flag[1][1][Map[1][1][4]] = true;
    	Node t;
    	while(!Q.empty()){
    		t = Q.front();
    		Q.pop();
    		for(int i=0 ; i<4 ; ++i){
    			if(Map[t.x][t.y][i] != -1&&(Map[t.x][t.y][i]==0||(Map[t.x][t.y][i]&t.key)>0)){
    				int xt = t.x + FX[i][0];
    				int yt = t.y + FX[i][1];
    				if(xt>=1 && xt<=N && yt>=1 && yt<=M){
    					int keyt = t.key | Map[xt][yt][4];
    					if(flag[xt][yt][keyt])continue;
    					else flag[xt][yt][keyt] = true;
    					if(xt == N && yt == M)return t.step+1;
    					Q.push(Node(xt,yt,t.step+1,keyt));
    				}
    			}
    		}
    	}
    	return -1;
    }
    
    int main(){
    	
    	while(scanf("%d %d %d",&N,&M,&P)!=EOF){
    		memset(Map,0,sizeof Map);
    		memset(flag,false,sizeof flag);
    		int K;
    		scanf("%d",&K);
    		for(int i=1 ; i<=K ; ++i){
    			int x1,x2,y1,y2,t;
    			scanf("%d %d %d %d %d",&x1,&y1,&x2,&y2,&t);
    			for(int j=0 ; j<4 ; ++j){
    				if(x2 - x1 ==FX[j][0] && y2 - y1 == FX[j][1])
                    {
                        if(t == 0) Map[x1][y1][j] = -1 ;//是墙 
                        else Map[x1][y1][j] = (1 << (t - 1)) ;//是门 
                    }
                    else if(x1-x2 == FX[j][0] && y1-y2 == FX[j][1])
                    {
                        if(t == 0) Map[x2][y2][j] = -1 ;
                        else Map[x2][y2][j] = (1 << (t-1)) ;
                    }
    			}
    		}
    		scanf("%d",&K);
    		for(int i=1 ; i<=K ; ++i){
    			int x,y,z;
    			scanf("%d %d %d",&x,&y,&z);
    			Map[x][y][4] |= (1<<(z-1));
    		}
    		if(N == 1 && M == 1){
    			printf("0
    ");
    			continue;
    		}
    		printf("%d
    ",BFS());
    	}
    	
    	return 0;
    } 
  • 相关阅读:
    二进制求和
    删除排序数组中的重复项--leetcode算法题
    vue render
    数字实现千分位分隔符
    用nodejs实现向文件的固定位置插入内容
    工作中用到的正则表达式
    组件toast(类似于element-ui的message组件)的实现
    用svg实现一个环形进度条
    批量删除当前文件夹下面的.svn文件夹
    windows下的包管理器scoop
  • 原文地址:https://www.cnblogs.com/vocaloid01/p/9514063.html
Copyright © 2011-2022 走看看