设为首页 加入收藏

TOP

hdu 1429 (bfs+状态压缩)
2015-07-20 17:36:27 来源: 作者: 【 】 浏览:3
Tags:hdu 1429 bfs 状态 压缩

胜利大逃亡(续)

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


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


每个钥匙对应二进制数的一个位,一共2^10=1024个状态,题目不难。


#include
  
   
#include
   
     #include
    
      #include
     
       #include
      
        #include
       
         using namespace std; #define LL __int64 #define N 25 #define M 10000000 const int inf=(int)1e9; char g[N][N]; int n,m,t; int dir[4][2]={0,1,0,-1,-1,0,1,0}; int mark[N][N][1025]; struct node { int x,y,t; //时间 LL s; //状态 friend bool operator<(node a,node b) { return a.t>b.t; } }; void inti(int l) { int i,j,k; for(i=0;i
        
         q; node cur,next; cur.t=cur.s=0; cur.x=x; cur.y=y; q.push(cur); mark[x][y][0]=0; while(!q.empty()) { cur=q.top(); q.pop(); for(i=0;i<4;i++) { next.x=di=cur.x+dir[i][0]; next.y=dj=cur.y+dir[i][1]; next.t=cur.t+1; next.s=cur.s; if(di<0||di>=n||dj<0||dj>=m) continue; if(g[di][dj]=='*'||next.t>=t) continue; if(g[di][dj]=='^') return next.t; if(g[di][dj]>='a'&&g[di][dj]<='j') { int k=g[di][dj]-'a'; if((next.s&(1<
         
          ='A'&&g[di][dj]<='J') { int k=g[di][dj]-'A'; if((next.s&(1<
          
           ='A'&&g[i][j]<='J') k=max(k,g[i][j]-'A'); } } inti(k+1); printf("%d\n",bfs(si,sj)); } return 0; } 
          
         
        
       
      
     
    
   
  


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Restore IP Addresses [leetcode] 下一篇poj 2991 Crane(线段树)

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·PostgreSQL 索引 - (2025-12-25 22:20:43)
·MySQL Node.js 连接 (2025-12-25 22:20:41)
·SQL 撤销索引、表以 (2025-12-25 22:20:38)
·Linux系统简介 (2025-12-25 21:55:25)
·Linux安装MySQL过程 (2025-12-25 21:55:22)