首页 > 其他分享 >HDU 1885 Key Task(BFS)

HDU 1885 Key Task(BFS)

时间:2022-10-18 14:33:55浏览次数:54  
标签:yy HDU Task state int term BFS xx step

Key Task

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


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:#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <math.h>
#include <stdio.h>
#include <queue>
#include <map>

using namespace std;
int n,m1;
char a[105][105];
int vis[105][105][20];
int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
struct Node
{
int x,y;
int state;
int step;
Node(){};
Node(int x,int y,int state,int step)
{
this->x=x;
this->y=y;
this->state=state;
this->step=step;
}
};
int judge(char a)
{
if(a=='B'||a=='Y'||a=='R'||a=='G')
return 1;
if(a=='b'||a=='y'||a=='r'||a=='g')
return 0;
}
int bfs(int x,int y)
{
map<char,int>m;
m['B']=0;m['Y']=1;m['R']=2;m['G']=3;
m['b']=0;m['y']=1;m['r']=2;m['g']=3;
queue<Node> q;
q.push(Node(x,y,0,0));
vis[x][y][0]=1;
while(!q.empty())
{
Node term=q.front();
q.pop();
if(a[term.x][term.y]=='X')
return term.step;
for(int i=0;i<4;i++)
{
int xx=term.x+dir[i][0];
int yy=term.y+dir[i][1];
if(xx<0||yy<0||xx>=n||yy>=m1||a[xx][yy]=='#')
continue;
if(a[xx][yy]=='.'||a[xx][yy]=='X'||a[xx][yy]=='*')
{
if(!vis[xx][yy][term.state])
{
q.push(Node(xx,yy,term.state,term.step+1));
vis[xx][yy][term.state]=1;
continue;
}
}
if(!(term.state&(1<<m[a[xx][yy]]))&&judge(a[xx][yy]))
continue;
if((term.state&(1<<m[a[xx][yy]]))&&judge(a[xx][yy]))
{
if(!vis[xx][yy][term.state])
{
q.push(Node(xx,yy,term.state,term.step+1));
vis[xx][yy][term.state]=1;
continue;
}
}
if(!judge(a[xx][yy]))
{
if(!vis[xx][yy][term.state|(1<<m[a[xx][yy]])])
{
q.push(Node(xx,yy,term.state|(1<<m[a[xx][yy]]),term.step+1));
vis[xx][yy][term.state|(1<<m[a[xx][yy]])]=1;
continue;
}
}
}
}
return -1;
}
int main()
{
int x,y;
while(scanf("%d%d",&n,&m1)!=EOF)
{
if(n==0&&m1==0)
break;
for(int i=0;i<n;i++)
scanf("%s",a[i]);
for(int i=0;i<n;i++)
for(int j=0;j<m1;j++)
{
if(a[i][j]=='*')
x=i,y=j;
}
memset(vis,0,sizeof(vis));
int ans=bfs(x,y);
if(ans==-1)
printf("The poor student is trapped!\n");
else
printf("Escape possible in %d steps.\n",ans);
}
return 0;
}



标签:yy,HDU,Task,state,int,term,BFS,xx,step
From: https://blog.51cto.com/u_15834522/5766676

相关文章

  • HDU-1054 Strategic Game(树形DP)
    StrategicGameTimeLimit:20000/10000ms(Java/Other)MemoryLimit:65536/32768K(Java/Other)TotalSubmission(s):17AcceptedSubmission(s):11Font:......
  • HDU-1520 Anniversary party(树形DP)
    AnniversarypartyTimeLimit:2000/1000MS(Java/Others)MemoryLimit:65536/32768K(Java/Others)TotalSubmission(s):7566AcceptedSubmission(s):3321P......
  • task7
    #include<stdio.h>intmain(){intanswer;charwords[5000];printf("网课学习让一些人欢喜一些人忧.\n");printf("1.作为喜欢自学且自律的人,觉得......
  • task6
    #include<stdio.h>intmain(){charans1,ans2;printf("每次课前认真预习,课后认真复习了没?(输入y或Y表示有,输入n或N表示没有):");ans1=getchar()......
  • task5
    #pragmawarning(disable:4996)#include<stdio.h>intmain(){intage1,age2;chargender1,gender2;scanf("%d%c%d%c",&age1,&gender1,&age2,&g......
  • task4
    #pragmawarning(disable:4996)#include<stdio.h>intmain(){doublex,y;charc1,c2,c3;inta1,a2,a3;scanf("%d%d%d",&a1,&a2,&a3);......
  • task3
    #include<stdio.h>#include<math.h>intmain(){unsignedintsum;intn,a1,q;scanf_s("%d",&n);a1=1;q=2;sum=a1*(1-pow(q......
  • Task
     1#include<stdio.h>#include<stdlib.h>intmain(){printf("0\n");printf("<H>\n");printf("II\n");printf("0\n");printf("<H>\n");......
  • 【题解】CF11D A Simple Task(状压 DP)
    【题解】CF11DASimpleTask题目链接CF11DASimpleTask题意概述给定一张\(n\)个点\(m\)条边的无向图,无重边自环,点数不超过\(19\),求无向图中环的数量。思路分......
  • camunda_18_user_task_assignment
    Camunda后台数据库内建有几个人员/组织数据表,但在Usertaskassignment方便,并不强制我们要将自己的人员/组织数据导入到这些table中.举例讲,我们需要将某个usertask......