Gym 100947H-Phobia
来源:互联网 发布:python list fuzhi 编辑:程序博客网 时间:2024/05/29 19:55
Description
standard input/output
Announcement
Statements
Sally has a phobia of cockroaches. Every night she is having the same horrible nightmare! Where she is standing alone in a big maze surrounded by millions and millions of cockroaches. She wakes up horrified and screaming when she sees a huge cockroach standing on her foot. After many sleepless nights like that one, she finally decided to ask you for help.
Since you have some experience with psychology (after all you are a programmer, right?) you tell her that the best way to cure her phobia is to fight it. She must be strong and fight these stinky little insects. After many argument she agrees with you and decides that she must overcome her fears. To do so she must reach for her sandals (you know, she needs a weapon for this great battle). Sally marked her sandals with a big red X sign to make sure they are clearly noticeable.
Here comes your job! You thought that you should write a program to help her train for this battle. Given the maze information, your program should tell her whether if she could reach her sandals without being touched by any cockroach.
The maze is an N × M rectangular grid, each cell of this grid is either empty or a wall or a hole. Cockroaches emerge from these holes and spread in all directions. Sally and Cockroaches cannot pass through walls, in other words Sally can only move to an empty cell, and cockroaches can only occupy an empty cell.
Formally, at time t if cell (i, j) is occupied by cockroaches then in time t + 1 cells (i + 1, j), (i - 1, j), (i, j + 1), (i, j - 1) as well as cell (i, j) will be occupied by cockroaches if the respective cells are empty and are inside the borders of the maze.
In one second, Sally can move to an adjacent empty cell. Formally if at time t Sally is at cell (i, j), then in time t + 1 she can move to one of these cells: (i + 1, j), (i - 1, j), (i, j + 1), (i, j - 1) if the respective cells are empty and are inside the borders of the maze.
One empty cell contains Sally starting position, and One other empty cell Contains her Sandals. Sally’s goal is to reach her sandals without touching any cockroach. Sally touches a cockroach when Sally’s current position is occupied by cockroaches.
Input
The first line will be the number of test cases T.
Each test case contains two integers N and M (1 ≤ N, M ≤ 100) followed by N × M grid that represents the maze where:
‘S’ : sally’s starting position.
‘X’ : Sally’s sandals position.
‘#’ : a Wall.
‘*’ : a hole.
‘.’ : an empty cell.
Output
For each test case print “yes” if Sally can exit from the maze without touching any cockroach or “no” otherwise.
Sample Input
Input
2
5 5
S..#*
…#.
…##
…..
….X
5 5
S..#*
…#.
…#.
…..
….X
Output
yes
no
题意就是问你能不能在黑色的格子追到你之前走到目的地,bfs即可,比较人和黑色格子到达目的地的最短时间,若人比黑色格子快,则可以。
代码
#include <cstdio>#include <algorithm>#include <queue>#include <iostream>#include <map>#include <cstring>using namespace std;int n,m;struct node{ int x;int y;int step;};char maze[105][105];int used[105][105];int dx[4]={1,-1,0,0};int dy[4]={0,0,1,-1};bool check(int x,int y){ if(x>=0&&x<n&&y>=0&&y<m&&!used[x][y]&&maze[x][y]=='.'|| x>=0&&x<n&&y>=0&&y<m&&!used[x][y]&&maze[x][y]=='X') return true; return false;}int bfs(int sx,int sy){ queue <node> que; while(!que.empty()) que.pop(); node a; a.x=sx;a.y=sy;a.step=0; que.push(a); used[sx][sy]=1; while(!que.empty()) { node b; a=que.front(); que.pop(); if(maze[a.x][a.y]=='X') return a.step; for(int i=0;i<4;i++) { int nx=a.x+dx[i]; int ny=a.y+dy[i]; if(check(nx,ny)) { used[nx][ny]=1; b.x=nx; b.y=ny; b.step=a.step+1; que.push(b); } } } return 0x7fffffff;}int main (void){ int t ; cin>>t; while(t--) { scanf("%d %d",&n,&m); for(int i=0;i<n;i++) { scanf("%s",maze[i]); } int min1=0x7fffffff; int min2=0x7fffffff; //int min2; for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { if(maze[i][j]=='*') { memset(used,0,sizeof(used)); min1=min(min1,bfs(i,j)); } if(maze[i][j]=='S') { memset(used,0,sizeof(used)); min2=min(min2,bfs(i,j)); //min2=bfs(i,j); } } } if(min2<min1) { printf("yes\n"); } else printf("no\n"); } return 0;}
- Gym 100947H-Phobia
- GYM 100523H
- codeforces GYM 100792H
- Gym 100712H Bridges
- Gym - 100735H
- Gym-101490H
- Gym 101164.H
- Gym 101246.H
- Gym 101482.H
- Gym 100825 H
- Gym 101201.H
- Gym 101617H
- Gym 100187H - Mysterious Photos
- Gym 100015H Hidden Code
- Gym-101257H Card【贪心】
- 【Codeforces Gym】100162 H Temperature
- 【GYM】101170H Hamiltonian Hypercube
- Gym 101128H想法题
- Google Play APK扩展文件机制及开发流程详解
- MD5保存密码方案代码
- easyui 分页中page和rows参数
- MYSQLI预编译
- HDU 1003(水DP)
- Gym 100947H-Phobia
- nyoj1253 Turing equation(第七届河南省程序设计大赛)
- Form表单(url)中的加号处理
- Linux Shell编程一:shell命令
- 生成二维码
- Sift 十大问
- Java并发编程:阻塞队列
- java根据时间排序list,使用collections.sort()
- 资料收藏