ZOJ3750 Dot Dot Dot 枚举+BFS+DFS

这道搜索的关键是,触发器总数不超过5个,那么我们可以枚举触发的顺序,每一个触发顺序可以生成多个状态的地图,每一个状态地图的起点就是生成当前状态地图情况下我们所触发的最后一个触发器。然后不同状态地图之间就会有一个距离(不同触发器之间的距离),并且每一个状态的地图,从起点触发器到终点,也会有一个距离。最后,我们最初那张没有触发任何触发器的地图的触发器起点就是s。

根据这一系列的关系我们可以建成一张DAG图,然后DFS求出最短距离即可。

代码主要多在生成地图以及求距离上。理清了逻辑后这道题目不难。


极限状态数 5*4*3*2*1+5*4*3*2+5*4*3+5*4+5+1 


Dot Dot Dot

Time Limit: 2 Seconds       Memory Limit: 65536 KB

Recently DZB is playing a game called Dot Dot Dot. It's a simple game but very interesting. The rule is like this:

The game is played on a N*M grid map, each grid is adjacent with 4 grids(up, down, left, right). There's only one exit on the map, the game finished when the player reach the exit. When the game starts, the player go to the exit step by step. In each step he can only move to adjacent grid. But there's some barriers on the map, each barrier is yellow or green. The player can not go to the grid when there is a barrier or there is outside wall.

The only way to destroy the barriers is to stand on some grid which has a trigger. Each trigger is also yellow or green and can be used only once. When the player stand on it, it will sent shock waves to destroy all the barriers with the same color it could reach directly. Shock waves spread as wide as possible. It can spread on all the connected grids which no barrier is on it, that means if it reach a barrier with different color, it will be blocked too. Notice if the wave can not reach a barrier directly, it can not be destroied, even if it's the same color with the trigger.

Now DZB not only wants to finish the game, but also wants to finish the game with the minimal step, so he ask you for help.

Input

There are multiple test cases. For each test case:

The first line contains two integers N(1≤N≤40), M(1≤M≤40), N is the number of rows of the map, and M is the number of the columns.

The after N lines describe the map, each line has M characters. A character could be:

  • '.' means there's nothing, you can stand there.
  • '*' means the outside wall, we guarantee that the outside wall form a close area and the player is in it.
  • 'Y' means a yellow barrier, you can not stand there unless you have destroied the barrier.
  • 'G' means a green barrier, similar with 'Y'.
  • 'y' means a yellow trigger.
  • 'g' means a green trigger. Notice the number of yellow trigger plus the number of green trigger is no more than 5.
  • 's' means the initial position of the player.
  • 'e' means the exit, the player must go to there finally to finish the game.

There is a blank line between every two cases.

Process to the end of input.

Output

One line for each test case. The minimal step. If can not reach the exit, print -1 instead.

Sample Input
5 17
*****************
*.......Y.......*
*.y.s...Y....e..*
*.......Y.......*
*****************
Sample Output
13

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<map>
#include<cstdio>
#include<vector>
#include<utility>
#include<queue>

using namespace std;

char str[55],G[3000][50][50],NOW;
int x[50],y[50],n,m,st=0,ed=6,p=0;
int dis[3000][50][50];
bool vist[50][50],vis[7];
int selects[7];
int last[3000],cnt,q;
vector<pair<int,int> > lastMap[3000];
map<string,int> msi;

bool jud(int x,int y){
    if(x<1 ||x>n||y<1||y>m)
        return false;
    return true;
}

//求每种状态地图从开始的点到其他点的距离,用于建立最终的地图,以及用于接下来的地图更新
//cur:当前地图状态
void bfs1(int cur,int x,int y,int dist){
    queue<pair<int,int> > que;
    vist[x][y]=1;
    dis[cur][x][y]=dist;
    que.push(make_pair(x,y));
    while(!que.empty()){
        pair<int ,int> pii=que.front();
        que.pop();
        int xx=pii.first,yy=pii.second;
        if(G[cur][xx][yy]=='Y'||G[cur][xx][yy]=='G'||G[cur][xx][yy]=='*'){
            dis[cur][xx][yy]=-1;
            continue;
        }
        //此处注意如果走到了触发器,则不能继续走下去,因为一旦到了触发器就会触发,就会进入下一个状态的地图。
        if(G[cur][xx][yy]=='y'||G[cur][xx][yy]=='g')
            continue;
        if(!vist[xx+1][yy]&&jud(xx+1,yy)){
                dis[cur][xx+1][yy]=dis[cur][xx][yy]+1;
                que.push(make_pair(xx+1,yy));
                vist[xx+1][yy]=1;
        }
        if(!vist[xx-1][yy]&&jud(xx-1,yy)){
                dis[cur][xx-1][yy]=dis[cur][xx][yy]+1;
                que.push(make_pair(xx-1,yy));
                vist[xx-1][yy]=1;
        }
        if(!vist[xx][yy+1]&&jud(xx,yy+1)){
                dis[cur][xx][yy+1]=dis[cur][xx][yy]+1;
                que.push(make_pair(xx,yy+1));
                vist[xx][yy+1]=1;
        }
        if(!vist[xx][yy-1]&&jud(xx,yy-1)){
                dis[cur][xx][yy-1]=dis[cur][xx][yy]+1;
                que.push(make_pair(xx,yy-1));
                vist[xx][yy-1]=1;
        }
    }
}

//消除障碍
void dfs2(int cur,int x,int y){
    vist[x][y]=1;
    if(G[cur][x][y]=='*')
        return;
    if(G[cur][x][y]=='Y'||G[cur][x][y]=='G')
        if(G[cur][x][y]!=NOW) return;
    if(G[cur][x][y]==NOW){
        G[cur][x][y]='.';
        return;
    }
    if(!vist[x+1][y]&&jud(x+1,y)) dfs2(cur,x+1,y);
    if(!vist[x-1][y]&&jud(x-1,y)) dfs2(cur,x-1,y);
    if(!vist[x][y-1]&&jud(x,y-1)) dfs2(cur,x,y-1);
    if(!vist[x][y+1]&&jud(x,y+1)) dfs2(cur,x,y+1);
}

//生成每种状态的地图并求出距离。
void generates(){
    int pre=0;
    for(int i=0;i<cnt;i++){
        string s="";
        for(int j=0;j<=i;j++)
            s=s+char(selects[j]+'0');
        if(msi.count(s)!=0){
            //可能有重复的状态
            pre=msi[s];
            continue;
        }
        ++q;
        last[q]=selects[i];
        int xx=x[last[q]],yy=y[last[q]];
        if(dis[pre][xx][yy]==-1){
            //无法到达下一个地图状态的起点触发器
            q--;
            break;
        }
        //从上一个状态pre地图的起点触发器到当前状态q地图的起点触发器的距离,建图
        lastMap[pre].push_back(make_pair(q,dis[pre][xx][yy]));
        memset(dis[q],-1,sizeof(dis[q]));
        memcpy(G[q],G[pre],sizeof(G[q]));
        memset(vist,0,sizeof(vist));
        NOW=G[0][xx][yy]-32;
        G[q][xx][yy]='.';
        dfs2(q,xx,yy);//更新地图

        memset(vist,0,sizeof(vist));
        bfs1(q,xx,yy,0); //更新状态q地图下从起点触发器到其他点的距离
        pre=q;
        msi[s]=q;
    }

}

//枚举触发器触发序列
void getSeq(int sel){
    if(sel==cnt){
        generates();
    }
    else{
        for(int i=1;i<=cnt;i++){
            if(vis[i])
                continue;
            vis[i]=1;
            selects[sel]=i;
            getSeq(sel+1);
            vis[i]=0;
        }
    }
}

int ret;
//构图为DAG ,注意生成的图是DAG
void dfs(int u,int d){
    if(u==q+1){
        ret=min(ret,d);
        return;
    }
    for(int i=0;i<lastMap[u].size();i++){
        int v=lastMap[u][i].first;
        int c=lastMap[u][i].second;
        dfs(v,c+d);
    }

}

void solve(){
    q=0;
    msi.clear();
    for(int i=0;i<3000;i++)
        lastMap[i].clear();
    memset(dis[0],-1,sizeof(dis[0]));
    memset(vist,0,sizeof(vist));
    bfs1(0,x[st],y[st],0);
    memset(vis,0,sizeof(vis));
    getSeq(0);
    //一共生成了q个状态的地图,判断每一个状态的地图是否能够到达终点,再分别加边
    for(int i=0;i<=q;i++)
        if(dis[i][x[ed]][y[ed]]!=-1)
            lastMap[i].push_back(make_pair(q+1,dis[i][x[ed]][y[ed]]));
    ret=0x3f3f3f3f;
    dfs(0,0);
    if(ret==0x3f3f3f3f)
        printf("-1\n");
    else
        printf("%d\n",ret);

}

int main(){
    while(~scanf("%d%d",&n,&m)){
        p=0;cnt=0;
        //0:初始状态
        for(int i=1;i<=n;i++){
            scanf("%s",str);
            for(int j=0;j<strlen(str);j++){
                G[0][i][j+1]=str[j];
                if(G[0][i][j+1]=='s') x[st]=i,y[st]=j+1;
                else if(G[0][i][j+1]=='e') x[ed]=i,y[ed]=j+1;
                else if(G[0][i][j+1]=='y' || G[0][i][j+1]=='g') x[p+1]=i,y[++p]=j+1,cnt++;
            }
        }
        solve();
    }
    return 0;
}
/*
5 17
*****************
*...e...G.......*
*.g.s...........*
*.......G.......*
*****************

5 17
*****************
*.......GY.....e*
*.gys...GY......*
*.......GY......*
*****************

5 17
.........e.......
*****************
*.gys...GY......*
*.......GY......*
*****************
*/


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值