状态压缩+bfs

来源:互联网 发布:传智java视频教程 编辑:程序博客网 时间:2024/04/28 08:30

胜利大逃亡(续)


 

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

 

代码:

#include<iostream>#include<queue>#define Maxn 20using namespace std;struct point{    int x,y,step,key; //key表示钥匙状态    point(){}    point(int x1,int y1,int s,int k):x(x1),y(y1),step(s),key(k){}};int dx[]={0,1,0,-1};int dy[]={1,0,-1,0};char maze[Maxn][Maxn];int key_n[Maxn][Maxn][1<<10];int n,m,tnum;int sx,sy;queue<point> q;int bfs(int num){    q.push(point(sx,sy,0,0));    key_n[sx][sy][0]=num;    while(!q.empty()){        point t(q.front());        q.pop();        for(int i=0;i<4;i++){            int tx=t.x+dx[i],ty=t.y+dy[i];            point tt(tx,ty,t.step+1,t.key);            if(tx<0||ty<0||tx>=n||ty>=m) continue;            if(key_n[tx][ty][tt.key]==num||maze[tx][ty]=='*') continue; //不可能情况            key_n[tx][ty][tt.key]=num; //记录状态            if(tt.step>=tnum) return -1; //可能情况            if(maze[tx][ty]=='^') return tt.step;            if(maze[tx][ty]=='.'||maze[tx][ty]=='@') {q.push(tt);continue;}            if(maze[tx][ty]>='a'&&maze[tx][ty]<='j'){                tt.key|=(1<<(maze[tx][ty]-'a'));                key_n[tx][ty][tt.key]=num;                q.push(tt);                continue;            }            if(maze[tx][ty]>='A'&&maze[tx][ty]<='J'){                if(tt.key&(1<<(maze[tx][ty]-'A')))                    q.push(tt);            }        }    }    return -1; //搜不到'^',但步数没超}int main(){    int num=0;    while(cin>>n>>m>>tnum){        while(!q.empty()) q.pop();        for(int i=0;i<n;i++)            for(int j=0;j<m;j++){                cin>>maze[i][j];                if(maze[i][j]=='@')                    sx=i,sy=j;            }        cout<<bfs(++num)<<endl;    }    return 0;}


 

 

Key Task

Problem Description
The Czech Technical University is rather old — you already know that it celebrates 300 years of its existence in 2007. Some of the university buildings are old as well. And the navigation in old buildings can sometimes be a little bit tricky, because of strange long corridors that fork and join at absolutely unexpected places.

The result is that some first-graders have often di?culties finding the right way to their classes. Therefore, the Student Union has developed a computer game to help the students to practice their orientation skills. The goal of the game is to find the way out of a labyrinth. Your task is to write a verification software that solves this game.

The labyrinth is a 2-dimensional grid of squares, each square is either free or filled with a wall. Some of the free squares may contain doors or keys. There are four di?erent types of keys and doors: blue, yellow, red, and green. Each key can open only doors of the same color.

You can move between adjacent free squares vertically or horizontally, diagonal movement is not allowed. You may not go across walls and you cannot leave the labyrinth area. If a square contains a door, you may go there only if you have stepped on a square with an appropriate key before.


 

Input
The input consists of several maps. Each map begins with a line containing two integer numbers R and C (1 ≤ R, C ≤ 100) specifying the map size. Then there are R lines each containing C characters. Each character is one of the following:



Note that it is allowed to have
  • more than one exit,
  • no exit at all,
  • more doors and/or keys of the same color, and
  • keys without corresponding doors and vice versa.

    You may assume that the marker of your position (“*”) will appear exactly once in every map.

    There is one blank line after each map. The input is terminated by two zeros in place of the map size.

  •  

    Output
    For each map, print one line containing the sentence “Escape possible in S steps.”, where S is the smallest possible number of step to reach any of the exits. If no exit can be reached, output the string “The poor student is trapped!” instead.

    One step is defined as a movement between two adjacent cells. Grabbing a key or unlocking a door does not count as a step.


     

    Sample Input
    1 10*........X1 3*#X3 20#####################XY.gBr.*.Rb.G.GG.y#####################0 0


     

    Sample Output
    Escape possible in 9 steps.The poor student is trapped!Escape possible in 45 steps.

     

    代码:

    #include<iostream>#include<queue>#define Maxn 100using namespace std;struct point{    int x,y,step,key;    point(){}    point(int a,int b,int c,int d):x(a),y(b),step(c),key(d){}};char maze[Maxn][Maxn];int state[Maxn][Maxn][1<<4];int n,m,sx,sy;int dx[]={0,1,0,-1};int dy[]={1,0,-1,0};int val(char s){    if(s=='B'||s=='b') return 0;    if(s=='Y'||s=='y') return 1;    if(s=='R'||s=='r') return 2;    if(s=='G'||s=='g') return 3;}int bfs(int num){    queue<point> q;    q.push(point(sx,sy,0,0));    state[sx][sy][0]=num;    while(!q.empty()){        point t(q.front());        q.pop();        for(int i=0;i<4;i++){            int tx=t.x+dx[i],ty=t.y+dy[i];            point tt(tx,ty,t.step+1,t.key);            if(tx<0||ty<0||tx>=n||ty>=m) continue;            if(maze[tx][ty]=='#'||state[tx][ty][tt.key]==num) continue;            state[tx][ty][tt.key]=num;            if(maze[tx][ty]=='X') return t.step+1;            if(maze[tx][ty]=='B'||maze[tx][ty]=='Y'||maze[tx][ty]=='R'||maze[tx][ty]=='G'){                if(tt.key&(1<<val(maze[tx][ty]))){                   q.push(tt);                   state[tx][ty][tt.key]=num;                }                continue;            }            if(maze[tx][ty]=='b'||maze[tx][ty]=='y'||maze[tx][ty]=='r'||maze[tx][ty]=='g'){                tt.key|=(1<<val(maze[tx][ty]));            }            q.push(tt);        }    }    return -1;}int main(){    int num=0;    while(cin>>n>>m,n){        for(int i=0;i<n;i++)        for(int j=0;j<m;j++){            cin>>maze[i][j];            if(maze[i][j]=='*')                sx=i,sy=j;        }        int tmp=bfs(++num);        if(tmp==-1) cout<<"The poor student is trapped!"<<endl;        else cout<<"Escape possible in "<<tmp<<" steps."<<endl;    }    return 0;}


    注:这两题都是一种类型的题目,bfs+状态压缩;每种状态用二进制数表示,1表示有小写字母,0表示没有小写字母。对没有重复的状态压入队列,尝试继续向下探索。其实状态还可以优化,压入队列的应该是不包含的状态,一些包含关系的状态也是无效的搜索点,但代码实现起来较复杂;在我被WA和TLE了无数次后,放弃了那种优化代码,转而向这种未优化的代码模仿,效率还不错的。 毕竟题目的范围不是很大,状态也不是很多……

    0 0