拯救行动

题目链接http://noi.openjudge.cn/ch0205/4980/

描述

公主被恶人抓走,被关押在牢房的某个地方。牢房用N*M (N, M <= 200)的矩阵来表示。矩阵中的每项可以代表道路(@)、墙壁(#)、和守卫(x)。 
英勇的骑士(r)决定孤身一人去拯救公主(a)。我们假设拯救成功的表示是“骑士到达了公主所在的位置”。由于在通往公主所在位置的道路中可能遇到守卫,骑士一旦遇到守卫,必须杀死守卫才能继续前进。 
现假设骑士可以向上、下、左、右四个方向移动,每移动一个位置需要1个单位时间,杀死一个守卫需要花费额外的1个单位时间。同时假设骑士足够强壮,有能力杀死所有的守卫。

给定牢房矩阵,公主、骑士和守卫在矩阵中的位置,请你计算拯救行动成功需要花费最短时间。

输入
第一行为一个整数S,表示输入的数据的组数(多组输入)
随后有S组数据,每组数据按如下格式输入 
1、两个整数代表N和M, (N, M <= 200). 
2、随后N行,每行有M个字符。"@"代表道路,"a"代表公主,"r"代表骑士,"x"代表守卫, "#"代表墙壁。
输出
如果拯救行动成功,输出一个整数,表示行动的最短时间。
如果不可能成功,输出"Impossible"
样例输入
2
7 8
#@#####@
#@a#@@r@
#@@#x@@@
@@#@@#@#
#@@@##@@
@#@@@@@@
@@@@@@@@ 
13 40
@x@@##x@#x@x#xxxx##@#x@x@@#x#@#x#@@x@#@x
xx###x@x#@@##xx@@@#@x@@#x@xxx@@#x@#x@@x@
#@x#@x#x#@@##@@x#@xx#xxx@@x##@@@#@x@@x@x
@##x@@@x#xx#@@#xxxx#@@x@x@#@x@@@x@#@#x@#
@#xxxxx##@@x##x@xxx@@#x@x####@@@x#x##@#@
#xxx#@#x##xxxx@@#xx@@@x@xxx#@#xxx@x#####
#x@xxxx#@x@@@@##@x#xx#xxx@#xx#@#####x#@x
xx##@#@x##x##x#@x#@a#xx@##@#@##xx@#@@x@x
x#x#@x@#x#@##@xrx@x#xxxx@##x##xx#@#x@xx@
#x@@#@###x##x@x#@@#@@x@x@@xx@@@@##@@x@@x
x#xx@x###@xxx#@#x#@@###@#@##@x#@x@#@@#@@
#@#x@x#x#x###@x@@xxx####x@x##@x####xx#@x
#x#@x#x######@@#x@#xxxx#xx@@@#xx#x#####@
样例输出
13
7

查看

刚开始使用dfs怎么优化都超时,后来改成BFS,本以为会很容易的AC,但还是WA了,上网搜了一下,才明白。原来按照普通的BFS的做法,求出的是最短路的时间,而不是最短时间。原因每次都拓展相邻的状态,第一次遇到目标,得到的是最短的路径。正确的做法是每次拓展的状态应该是最短的时间的状态,即要令什么最小就以什么为尺度扩张。自然在实现的过程中,要用到priority_queue。

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<string>
#include<vector>
#include<cmath>
#include<queue>
#define DEBUG(x) cout<<#x<<" = "<<x<<endl
using namespace std;
const int MAXN=210;
const int INF=0x3f3f3f3f;
char Map[MAXN][MAXN];
int visited[MAXN][MAXN];
int minTime=INF;
int N,M;
int dirx[]={1,-1,0,0};
int diry[]={0,0,1,-1};
///dfs失败
int ex,ey;
struct Node{
    int x,y;
    int time;
    Node(){}
    Node(int xx,int yy,int t){
        x=xx,y=yy,time=t;
    }
    friend bool operator<(const Node &a,const Node &b)
    {
        return a.time>b.time;
    }
};
bool legal(int x,int y)
{
    return x>=0&&x<N&&y>=0&&y<M;
}
int main()
{
//    freopen("in.txt","r",stdin);
    int t;
    scanf("%d",&t);
    priority_queue<Node>q;
    while(t--){
        int x,y;
        while(!q.empty())q.pop();
        memset(visited,0,sizeof(visited));
        scanf("%d%d",&N,&M);
        getchar();
        for(int i=0;i<N ;i++ ){
            for(int j=0;j<M ;j++ ){
                char c;
                scanf("%c",&c);
                Map[i][j]=c;
                if(c=='r'){
                    x=i,y=j;
                }
                if(c=='a'){
                    Map[i][j]='@';
                    ex=i,ey=j;
                }
            }
            getchar();
        }
        q.push(Node(x,y,0));
        visited[x][y]=true;
        bool succ=false;
        while(!q.empty()){
            Node t=q.top();
            q.pop();
            if(t.x==ex&&t.y==ey){
                succ=true;
                printf("%d\n",t.time);
                break;
            }
            for(int i=0;i<4 ;i++ ){
                int xx=t.x+dirx[i];
                int yy=t.y+diry[i];
                if(legal(xx,yy)&&!visited[xx][yy]){
                    if(Map[xx][yy]=='@'){
                        q.push(Node(xx,yy,t.time+1));
                        visited[xx][yy]=true;
                    }
                    else if(Map[xx][yy]=='x'){
                        q.push(Node(xx,yy,t.time+2));
                        visited[xx][yy]=true;
                    }
                }
            }
        }
        if(!succ)puts("Impossible");
    }
}

参考资料

https://blog.csdn.net/PKU_ZZY/article/details/51584961

http://www.cnblogs.com/Maki-Nishikino/p/6056072.html

猜你喜欢

转载自www.cnblogs.com/MalcolmMeng/p/9273436.html