HDU2579(bfs迷宫)
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
Problem Description
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.

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.

Input
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.
The next r line is the map’s description.
Output
For each cases, if you can find the girl, output the least time in seconds, else output "Please give me another chance!".
Sample Input
1
6 6 2
...Y..
...#..
.#....
...#..
...#..
..#G#.
Sample Output
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; }
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步