HDU 1429 胜利大逃亡(续)

来源:互联网 发布:中国化妆品销售数据 编辑:程序博客网 时间:2024/06/07 23:16

胜利大逃亡(续)

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 9153    Accepted Submission(s): 3303


Problem Description
Ignatius再次被魔王抓走了(搞不懂他咋这么讨魔王喜欢)……

这次魔王汲取了上次的教训,把Ignatius关在一个n*m的地牢里,并在地牢的某些地方安装了带锁的门,钥匙藏在地牢另外的某些地方。刚开始Ignatius被关在(sx,sy)的位置,离开地牢的门在(ex,ey)的位置。Ignatius每分钟只能从一个坐标走到相邻四个坐标中的其中一个。魔王每t分钟回地牢视察一次,若发现Ignatius不在原位置便把他拎回去。经过若干次的尝试,Ignatius已画出整个地牢的地图。现在请你帮他计算能否再次成功逃亡。只要在魔王下次视察之前走到出口就算离开地牢,如果魔王回来的时候刚好走到出口或还未到出口都算逃亡失败。
 

Input
每组测试数据的第一行有三个整数n,m,t(2<=n,m<=20,t>0)。接下来的n行m列为地牢的地图,其中包括:

. 代表路
* 代表墙
@ 代表Ignatius的起始位置
^ 代表地牢的出口
A-J 代表带锁的门,对应的钥匙分别为a-j
a-j 代表钥匙,对应的门分别为A-J

每组测试数据之间有一个空行。
 

Output
针对每组测试数据,如果可以成功逃亡,请输出需要多少分钟才能离开,如果不能则输出-1。
 

Sample Input
4 5 17@A.B.a*.*.*..*^c..b*4 5 16@A.B.a*.*.*..*^c..b*
 

Sample Output
16-1
 

Author
LL
 

Source
ACM暑期集训队练习赛(三)
 

Recommend
linle   |   We have carefully selected several similar problems for you:  1254 1072 1401 1175 1240 
 
解:bfs+状态压缩
import java.util.LinkedList;import java.util.Queue;import java.util.Scanner;/** *  * 作者:张宇翔 * 创建日期:2017年8月6日 下午5:28:17 * 描述: */public class Main {private static final int Max = (int) (21);private static int [][]dir=new int[][]{{1,0},{-1,0},{0,1},{0,-1}};private static boolean [][][]vis;private static int startx,starty,endx,endy;private static int n,m,t;private static String []map;private static int ans;private static boolean ok;public static void main(String[] args) {InitData();}private static void InitData() {Scanner cin=new Scanner(System.in);while(cin.hasNext()){map=new String[Max];vis=new boolean[Max][Max][1025];n=cin.nextInt();m=cin.nextInt();t=cin.nextInt();ok=false;for(int i=0;i<n;i++){map[i]=cin.next();for(int j=0;j<m;j++){if(map[i].charAt(j)=='@'){startx=i;starty=j;}if(map[i].charAt(j)=='^'){endx=i;endy=j;}}}GetAns();}};private static void GetAns() {bfs();if(ok){System.out.println(ans);}else{System.out.println(-1);}}private static void bfs(){Queue<Node> queue=new LinkedList<>();Node start=new Node(startx, starty, 0, 0);vis[startx][starty][0]=true;queue.add(start);while(!queue.isEmpty()){Node node=queue.poll();if(node.x==endx&&node.y==endy){if(node.step<t){ans=node.step;ok=true;}return;}for(int i=0;i<4;i++){int newx=node.x+dir[i][0];int newy=node.y+dir[i][1];if(check(newx, newy)){int newstep=node.step+1;char newchar=map[newx].charAt(newy);if(newchar>='a'&&newchar<='j'){int key=node.key|(1<<(newchar-'a'));if(!vis[newx][newy][key]){vis[newx][newy][key]=true;queue.add(new Node(newx, newy,newstep , key));}}else if(newchar>='A'&&newchar<='J'){int key=node.key&(1<<(newchar-'A'));if(key>0&&(!vis[newx][newy][node.key])){vis[newx][newy][node.key]=true;queue.add(new Node(newx, newy, newstep, node.key));}}else{if(!vis[newx][newy][node.key]){vis[newx][newy][node.key]=true;queue.add(new Node(newx, newy, newstep, node.key));}}}}}return;}private static boolean check(int x,int y){if(x>=0&&x<n&&y>=0&&y<m&&(map[x].charAt(y)!='*'))return true;return false;}private static class Node{int x;int y;int step;int key;public Node(int x, int y, int step, int key) {this.x = x;this.y = y;this.step = step;this.key = key;}}}


原创粉丝点击