HDOJ1429(胜利大逃亡(续))

胜利大逃亡(续)

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


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

 

 

//2009-05-04 17:56:27 Accepted 1429 703MS 2648K 2325 B C++ Xredman 
#include <iostream>
#include 
<queue>
#include 
<cstdio>
using namespace std;

const int N = 22;

typedef 
struct
{
    
int x, y;
    
int step;//记录走过步数
    int cnt;//用于压缩路径
    bool ch[10];
}
Point;

int n, m, t;
char graph[N][N];
int sx, sy, ex, ey;
int dir[4][2]={-1,0,1,0,0,-1,0,1};
int bb[] = {1,2,4,8,16,32,64,128,256,512};
int mark[N][N][1030];

void make_graph()
{
    
int i, j, k, flag = 0;
    
for(i = 0; i < n; i++)
    
{
        getchar();
        scanf(
"%s", graph[i]);
        
if(flag != 2)
            
for(j = 0; j < m; j++)
                
if(graph[i][j] == '@')
                    
{ sx = i; sy = j; flag++;}
                
else if(graph[i][j] == '^')
                    
{ ex = i; ey = j; flag++;}
    }


    
for(i = 0; i < n; i++)
        
for(j = 0; j < m;j++)
            
for(k = 0; k < 1030; k++)
                mark[i][j][k] 
= -1;
}


bool isBound(int x, int y)
{
    
if(x < 0 || y < 0)
        
return false;
    
if(x >= n || y >= m)
        
return false;
    
return true;
}


void bfs()
{
    Point a, b;
    queue
<Point> Q;
    
int i, j, cc, tcnt;

    a.x 
= sx; a.y = sy; a.step = a.cnt = 0;
    
for(i = 0; i < 10; i++)
        a.ch[i] 
= false;

    Q.push(a);

    
while(!Q.empty())
    
{
        a 
= Q.front();
        Q.pop();

        
if(a.x == ex && a.y == ey)
        
{
            
if(a.step < t)
            
{
                cout
<<a.step<<endl;
                
return ;
            }

            
else
                
break;
        }


        
for(i = 0; i < 4; i++)
        
{
            b 
= a;
            b.x 
+= dir[i][0];
            b.y 
+= dir[i][1];
            b.step
++;

            
if(isBound(b.x, b.y) && graph[b.x][b.y] != '*')
            
{
                
if(graph[b.x][b.y] >= 'a' && graph[b.x][b.y] <= 'j')
                
{
                    cc 
= graph[b.x][b.y] - 'a';
                    
if(b.ch[cc])
                        tcnt 
= b.cnt;
                    
else
                        tcnt 
= b.cnt + bb[cc];
                    
if(mark[b.x][b.y][tcnt] == -1)
                    
{
                        b.cnt 
= tcnt;
                        mark[b.x][b.y][b.cnt] 
= b.step;
                        b.ch[cc] 
= true;
                        Q.push(b);
                    }

                }

                
else if(graph[b.x][b.y] >= 'A' && graph[b.x][b.y] <= 'J')
                
{
                    cc 
= graph[b.x][b.y] - 'A';
                    
if(b.ch[cc] && mark[b.x][b.y][b.cnt] == -1)
                    
{
                        mark[b.x][b.y][b.cnt] 
= b.step;
                        Q.push(b);
                    }

                }

                
else 
                
{
                    
if(mark[b.x][b.y][b.cnt] == -1)
                    
{
                        mark[b.x][b.y][b.cnt] 
= b.step;
                        Q.push(b);
                    }

                }

            }

            
        }

    }

    cout
<<"-1"<<endl;
}

int main()
{
    
while(scanf("%d%d%d"&n, &m, &t) != EOF)
    
{
        make_graph();
        bfs();
    }

    
return 0;
}

/*
@A.B.
a*.*.
*..*^
c..b*
*/

posted on 2009-05-04 18:03  Xredman  阅读(350)  评论(0编辑  收藏  举报

导航