• HDU 1885 Key Task (bfs)


    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.

        bfs,用二进制表示已有的钥匙。标记每个点时还要标记到这个点所有的钥匙。

      

     1 #include<cstdio>
     2 #include<cstring>
     3 #include<queue>
     4 #include<map>
     5 using namespace std;
     6 struct p
     7 {
     8     int x,y,t;
     9 };p f,r;
    10 int n2,n1;
    11 int s[105][105];
    12 int v[1<<5][105][105];
    13 map<char,int>M,m;
    14 int yi[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
    15 int f1(int x,int y)
    16 {
    17     if (x<0||x>=n1||y<0||y>=n2||s[x][y]=='#') return 1;
    18     return 0;
    19 }
    20 int f2(int x,int y,int z)
    21 {
    22     if (s[x][y]>='A'&&s[x][y]<='Z'&&s[x][y]!='X')
    23     {
    24        int cut=M[s[x][y]];
    25        if (((1<<cut)&z)!=0) return 0;
    26        else return 1;
    27     }
    28     return 0;
    29 }
    30 int bfs(int x,int y)
    31 {
    32     queue<p>q;
    33     f.x=x;f.y=y;f.t=0;
    34     q.push(f);
    35     while (!q.empty())
    36     {
    37         r=q.front();
    38         q.pop();
    39         if (s[r.x][r.y]=='X') return v[r.t][r.x][r.y];
    40         for (int i=0;i<4;i++)
    41         {
    42             f.x=r.x+yi[i][0];
    43             f.y=r.y+yi[i][1];
    44             f.t=r.t;
    45             if (f1(f.x,f.y)||f2(f.x,f.y,f.t)) continue;
    46             if (v[f.t][f.x][f.y]!=-1) continue;
    47             if (s[f.x][f.y]>='a'&&s[f.x][f.y]<='z') f.t=((1<<m[s[f.x][f.y]])|r.t);
    48             v[f.t][f.x][f.y]=v[r.t][r.x][r.y]+1;
    49             q.push(f);
    50         }
    51     }
    52     return -1;
    53 }
    54 int main()
    55 {
    56     M['B']=1;m['b']=1;
    57     M['R']=2;m['r']=2;
    58     M['Y']=3;m['y']=3;
    59     M['G']=4;m['g']=4;
    60     int i,j,x,y;
    61     while (~scanf("%d%d",&n1,&n2))
    62     {
    63         if (n1==0&&n2==0) break;
    64         for (i=0;i<n1;i++)
    65         for (j=0;j<n2;j++)
    66         {
    67             scanf(" %c",&s[i][j]);
    68             if (s[i][j]=='*') {x=i;y=j;}
    69         }
    70         memset(v,-1,sizeof(v));
    71         v[0][x][y]=0;
    72         int ans=bfs(x,y);
    73         if (ans==-1) printf("The poor student is trapped!
    ");
    74         else printf("Escape possible in %d steps.
    ",ans);
    75     }
    76 }
  • 相关阅读:
    3DMAX贴图无法显示
    3DMAX2016安装教程【图文】
    OSG学习:转动的小汽车示例
    JAVA Eclipse 快捷键
    解决JQUERY在IE8,7,6下将字符串转成XML对象时产生的BUG
    毕设二:python 爬取京东的商品评论
    redis 注册为服务
    python 爬取bilibili 视频弹幕
    python 爬取36kr 7x24h快讯
    jQuery实现表格冻结行和列
  • 原文地址:https://www.cnblogs.com/pblr/p/4750213.html
Copyright © 2020-2023  润新知