HDU 1429 胜利大逃亡(续)(三维BFS)

题目链接

题意 : 中文题不详述。

思路 : 这个题和1885差不多一样的,所以我直接改了改那个代码就交上了,链接

  1 //1429
  2 #include <stdio.h>
  3 #include <string.h>
  4 #include <queue>
  5 #include <string>
  6 #include <iostream>
  7 
  8 using namespace std ;
  9 
 10 struct node
 11 {
 12     int x,y,step ;
 13     int keys ;
 14 } p[25],temp,temp1 ;
 15 
 16 int n,m ,t;
 17 int dir[4][2] = {{0,-1},{0,1},{-1,0},{1,0}} ;
 18 bool vis[25][25][(1 << 10) + 10] ;
 19 char mapp[25][25] ;
 20 
 21 
 22 void BFS(int sx,int sy)
 23 {
 24     queue<node>Q ;
 25     temp.x = sx ;
 26     temp.y = sy ;
 27     temp.step = temp.keys = 0 ;
 28     Q.push(temp) ;
 29     vis[sx][sy][0] = true ;
 30     while(!Q.empty())
 31     {
 32         temp = Q.front() ;
 33         Q.pop() ;
 34         if(mapp[temp.x][temp.y] == '^')
 35         {
 36             if(temp.step < t)
 37             {
 38                 printf("%d
",temp.step) ;
 39                 return ;
 40             }
 41         }
 42         for(int i = 0 ; i < 4 ; i++)
 43         {
 44             temp1.x = temp.x + dir[i][0] ;
 45             temp1.y = temp.y + dir[i][1] ;
 46             temp1.step = temp.step+1;
 47             temp1.keys = temp.keys ;
 48             if(mapp[temp1.x][temp1.y] == '*' || temp1.x < 0 || temp1.x >= n || temp1.y < 0 || temp1.y >= m)
 49                 continue ;
 50             else if(islower(mapp[temp1.x][temp1.y]))
 51             {
 52                 int k = mapp[temp1.x][temp1.y]-'a' ;
 53                 if((temp1.keys & (1 << k)) == 0)//如果这把钥匙没有,就加上。
 54                     temp1.keys += (1 << k) ;
 55                 if(!vis[temp1.x][temp1.y][temp1.keys])
 56                 {
 57                     Q.push(temp1) ;
 58                     vis[temp1.x][temp1.y][temp1.keys] = true ;
 59                 }
 60             }
 61             else if(isupper(mapp[temp1.x][temp1.y]) && mapp[temp1.x][temp1.y] != 'X')
 62             {
 63                 int k = mapp[temp1.x][temp1.y]-'a' ;
 64                 if(temp1.keys & (1 << k))
 65                 {
 66                     if(!vis[temp1.x][temp1.y][temp1.keys])
 67                     {
 68                         vis[temp1.x][temp1.y][temp1.keys] = true ;
 69                         Q.push(temp1) ;
 70 
 71                     }
 72                 }
 73             }
 74             else
 75             {
 76                 if(!vis[temp1.x][temp1.y][temp1.keys])
 77                 {
 78                     vis[temp1.x][temp1.y][temp1.keys] = true ;
 79                     Q.push(temp1) ;
 80                 }
 81             }
 82         }
 83     }
 84     printf("-1
") ;
 85 }
 86 
 87 int main()
 88 {
 89     int sx,sy ;
 90     while(~scanf("%d %d %d",&n,&m,&t))
 91     {
 92         for(int i = 0 ; i < n ; i++)
 93             scanf("%s",mapp[i]) ;
 94         memset(vis,false,sizeof(vis)) ;
 95         for(int i = 0 ; i < n ; i++)
 96         {
 97             for(int j = 0 ; j < m ; j++)
 98             {
 99                 if(mapp[i][j] == '@')
100                 {
101                     sx = i ;
102                     sy = j ;
103                 }
104             }
105         }
106         BFS(sx,sy) ;
107     }
108     return 0 ;
109 }
View Code
原文地址:https://www.cnblogs.com/luyingfeng/p/3676584.html