HDU2579(bfs迷宫)
2016-07-21 14:52
363 查看
Dating with girls(2)
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3006 Accepted Submission(s): 864
[align=left]Problem Description[/align]
If you have solved the problem Dating with girls(1).I think you can solve this problem too.This problem is also about dating with girls. Now you are in a maze and the girl you want to date with is also in the maze.If you can find the girl, then you can date with the girl.Else the girl will date with other boys. What a pity!
The Maze is very strange. There are many stones in the maze. The stone will disappear at time t if t is a multiple of k(2<= k <= 10), on the other time , stones will be still there.
There are only ‘.’ or ‘#’, ’Y’, ’G’ on the map of the maze. ’.’ indicates the blank which you can move on, ‘#’ indicates stones. ’Y’ indicates the your location. ‘G’ indicates the girl's location . There is only one ‘Y’ and one ‘G’. Every seconds you can move left, right, up or down.
[align=left]Input[/align]
The first line contain an integer T. Then T cases followed. Each case begins with three integers r and c (1 <= r , c <= 100), and k(2 <=k <= 10).
The next r line is the map’s description.
[align=left]Output[/align]
For each cases, if you can find the girl, output the least time in seconds, else output "Please give me another chance!".
[align=left]Sample Input[/align]
1
6 6 2
...Y..
...#..
.#....
...#..
...#..
..#G#.
[align=left]Sample Output[/align]
7
思路:若重新走到某位置所需的步数模K的值不同,那么可重复进队。否则意味着进入死循环。
#include <cstdio> #include <string.h> #include <queue> using namespace std; const int MAXN=105; struct Node{ int y,x,step; Node(){} Node(int y,int x,int step) { this->y=y; this->x=x; this->step=step; } }; char mz[MAXN][MAXN]; int n,m,k; int sy,sx; int dy[4]={0,1,0,-1}; int dx[4]={1,0,-1,0}; int vis[MAXN][MAXN][MAXN]; void bfs() { memset(vis,0,sizeof(vis)); queue<Node> que; que.push(Node(sy,sx,0)); vis[sy][sx][0%k]=1; while(!que.empty()) { Node now=que.front();que.pop(); if(mz[now.y][now.x]=='G') { printf("%d\n",now.step); return ; } for(int i=0;i<4;i++) { int ny=now.y+dy[i]; int nx=now.x+dx[i]; int ns=now.step+1; if(0<=ny&&ny<n&&0<=nx&&nx<m&&!vis[ny][nx][ns%k]) { if(mz[ny][nx]!='#') { vis[ny][nx][ns%k]=1; que.push(Node(ny,nx,ns)); } else { if(ns%k==0) { vis[ny][nx][ns%k]=1; que.push(Node(ny,nx,ns)); } } } } } printf("Please give me another chance!\n"); } int main() { int T; scanf("%d",&T); while(T--) { scanf("%d%d%d",&n,&m,&k); for(int i=0;i<n;i++) { scanf("%*c"); for(int j=0;j<m;j++) { scanf("%c",&mz[i][j]); if(mz[i][j]=='Y') { sy=i; sx=j; } } } bfs(); } return 0; }
相关文章推荐
- 干部带队伍的四个知道,一个跟上
- Kafka集群的安装和使用
- LAMP报PDO的错怎么办?
- 反软件盗版的最佳实践
- 通过Eclipse(CDT)使用OpenCV
- Android ListView 分页加载实现
- extjs6 基础组件扩展和自定义组件封装--05权限控制设计思想
- 函数
- 浅谈守护进程
- Android中四大组件(四大天王)
- MongoDB安装及简单主从部署
- mysql在MAC安装时忘记初始随机密码的解决办法
- navicat 结合快捷键
- Quartz 框架(一)
- JavaScript中的不可变性(Immutability)
- 西蒙iphone-OpenGL ES 教程-02 : 绘制矩形
- linux中的/dev/fd详解(linux3.2.0)
- c++学习笔记(四):函数按指针调用和按引用调用
- js实现打字机效果
- HIVE时间处理常用函数