• HDU 1885 BFS+状态压缩


    Key Task

    Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 1239    Accepted Submission(s): 495


    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 *........X 1 3 *#X 3 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.
 

Source


/*************************************************************************
	> File Name: 3.cpp
	> Author:yuan
	> Mail:
	> Created Time: 2014年11月26日 星期三 13时09分34秒
 ************************************************************************/

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<queue>
using namespace std;
char mat[105][105];
int ans;
struct node
{
    int x,y;
    int num,key;
};
bool vis[105][105][30];
queue<node> q;
int sx,sy;
int n,m,top,base;
int next[4][2]={1,0,0,1,-1,0,0,-1};

void BFS()
{
    int x1,x2,y1,y2;node p,qq;
    while(!q.empty()){
     p=q.front();
      x1=p.x;y1=p.y;
        if(mat[x1][y1]=='X'){
            ans=p.num;
            return;
        }
        for(int i=0;i<4;i++)
        {
            int key;
            x2=x1+next[i][0];y2=y1+next[i][1];
            if(x2<0||x2>=n||y2<0||y2>=m||mat[x2][y2]=='#') continue;

            if(mat[x2][y2]=='.'||mat[x2][y2]=='*'||mat[x2][y2]=='X')
            {
                key=p.key;
                if(vis[x2][y2][key]==0)
                {
                    qq.x=x2;qq.y=y2;qq.num=p.num+1;qq.key=p.key;
                    q.push(qq);
                    vis[x2][y2][key]=1;
                }
            }

             else if(mat[x2][y2]=='B'||mat[x2][y2]=='Y'||mat[x2][y2]=='R'||mat[x2][y2]=='G')
              {
                key=p.key;
                if(vis[x2][y2][key]==0){
                    int d;
                    if(mat[x2][y2]=='B') d=0;
                    else  if(mat[x2][y2]=='Y') d=1;
                           else  if(mat[x2][y2]=='R') d=2;
                                  else  if(mat[x2][y2]=='G') d=3;
                    int lock=1<<d;
                    if(lock&key){
                         qq.x=x2;qq.y=y2;qq.num=p.num+1;qq.key=p.key;
                         q.push(qq);
                         vis[x2][y2][key]=1;
                    }
                }
            }
            else if(mat[x2][y2]=='b'||mat[x2][y2]=='y'||mat[x2][y2]=='r'||mat[x2][y2]=='g')
            {
                  int d;
                    if(mat[x2][y2]=='b') d=0;
                    else  if(mat[x2][y2]=='y') d=1;
                           else  if(mat[x2][y2]=='r') d=2;
                                  else  if(mat[x2][y2]=='g') d=3;
                key=1<<d;
                if(vis[x2][y2][p.key]==0){
                    qq.x=x2;qq.y=y2;qq.num=p.num+1;qq.key=p.key|key;
                    q.push(qq);
                    vis[x2][y2][p.key]=1;
                }
            }
        }
      q.pop();
    }
}
int main()
{
     while(1){
        scanf("%d%d",&n,&m);
        if(n==0&&m==0) break;
        memset(mat,0,sizeof(mat));
        memset(vis,0,sizeof(vis));
        while(!q.empty()) q.pop();
        for(int i=0;i<n;i++) scanf("%s",mat[i]);
        for(int i=0;i<n;i++)
          for(int j=0;j<m;j++)
        {
            if(mat[i][j]=='*') {sx=i,sy=j;}

        }
        node p;
        p.x=sx;p.y=sy;p.num=0;p.key=0;
        q.push(p);
        vis[sx][sy][0]=1;
        ans=100000000;
        BFS();
        if(ans<100000000) printf("Escape possible in %d steps.
",ans);
        else printf("The poor student is trapped!
");
    }
    return 0;
}



  • 相关阅读:
    面向对象程序设计简介(1/2)
    iOS官方Sample大全
    AFN不支持 "text/html" 的数据的问题:unacceptable content-type: text/html
    谈ObjC对象的两段构造模式
    关于self和super在oc中的疑惑与分析 (self= [super init])
    在Xcode中使用Git进行源码版本控制
    NSObject之二
    NSObject之一
    Objective-C Runtime 运行时之六:拾遗
    Objective-C Runtime 运行时之五:协议与分类
  • 原文地址:https://www.cnblogs.com/codeyuan/p/4254389.html
  • Copyright © 2020-2023  润新知