Hdu1429 victory (continued) (State compression + BFS)

Source: Internet
Author: User

Problem DescriptionIgnatius is again taken away by the Devil (I don't know why he is so fond of the devil )......

This time, the devil learned the last lesson, locked Ignatius in an n * m dungeon, installed locks in some places in the dungeon, and hid the keys in some other places in the dungeon. At first Ignatius was shut down in the position of (sx, sy) and left the warehouse's door in the position of (ex, ey. Each minute, Ignatius can only go from one coordinate to one of the four adjacent coordinates. The magic Lord visits the dungeon once every t minutes. If it finds that Ignatius is not in the original position, he will be taken back. After several attempts, Ignatius has drawn a map of the entire dungeon. Please help him calculate whether he can successfully escape again. As long as the devil leaves the dungeon before the next visit, it will be deemed as a failed escape if the devil just leaves the exit or has not yet reached the exit.

The first row of test data in each Input group has three integers: n, m, t (2 <= n, m <= 20, t> 0 ). The next n rows of m column are the map of the dungeon, including:

. Represents the path
* Representing the wall
@ Indicates the starting position of Ignatius
^ Represents the exit of the dungeon
The A-J represents the door with a lock, and the corresponding keys are a-j
A-j Represents the key, and the corresponding door is the A-J

There is a blank line between each group of test data.

Output for each group of test data. If the data can be successfully escaped, Output how many minutes to exit. If not, Output-1.

Sample Input

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

Sample Output
16-1 each location has different key statuses.
#include
  
   #include
   
    #include
    
     #include
     
      using namespace std;typedef struct nnn{    int t,key,x,y;}NODE;int Key[25][25][1<<10],T,n,m;char map[25][25];int abs(int x){    return x>=0?x:-x;}int bfs(int sx,int sy){    queue
      
       q;    NODE now,pre;    int dir[4][2]={0,1,0,-1,1,0,-1,0};    int tx,ty;    now.key=0; now.t=0; now.x=sx; now.y=sy;    Key[sy][sx][0]=1;    q.push(now);    while(!q.empty())    {        pre=q.front(); q.pop();        for(int i=0; i<4; i++)        {            now=pre;            ty=pre.y+dir[i][0]; tx=pre.x+dir[i][1];            if(ty>=0&&ty
       
        =0&&tx
        
         ='a'&&map[ty][tx]<='j'){ now.key|=(1<<(map[ty][tx]-'a')); if(Key[ty][tx][now.key]==0) Key[ty][tx][now.key]=1,q.push(now); } else if(map[ty][tx]>='A'&&map[ty][tx]<='J') { if(Key[ty][tx][now.key]==0&&(pre.key&(1<<(map[ty][tx]-'A')))) Key[ty][tx][now.key]=1, q.push(now); } else if(Key[ty][tx][now.key]==0){ Key[ty][tx][now.key]=1; q.push(now); } } } } return -1;}int main(){ int sx,sy,ex,ey; while(scanf("%d%d%d",&n,&m,&T)>0) { for(int i=0; i
         
          =T) { printf("-1\n"); continue; } memset(Key,0,sizeof(Key)); printf("%d\n",bfs(sx,sy)); }}
         
        
       
      
     
    
   
  


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.