日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

HDU Problem - 3085 Nightmare Ⅱ(双向BFS)

發(fā)布時間:2024/4/18 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 HDU Problem - 3085 Nightmare Ⅱ(双向BFS) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題目鏈接

Problem Description

Last night, little erriyue had a horrible nightmare. He dreamed that he and his girl friend were trapped in a big maze separately. More terribly, there are two ghosts in the maze. They will kill the people. Now little erriyue wants to know if he could find his girl friend before the ghosts find them.You may suppose that little erriyue and his girl friend can move in 4 directions. In each second, little erriyue can move 3 steps and his girl friend can move 1 step. The ghosts are evil, every second they will divide into several parts to occupy the grids within 2 steps to them until they occupy the whole maze. You can suppose that at every second the ghosts divide firstly then the little erriyue and his girl friend start to move, and if little erriyue or his girl friend arrive at a grid with a ghost, they will die.Note: the new ghosts also can devide as the original ghost.

Input

The input starts with an integer T, means the number of test cases.Each test case starts with a line contains two integers n and m, means the size of the maze. (1

Output

Output a single integer S in one line, denotes erriyue and his girlfriend will meet in the minimum time S if they can meet successfully, or output -1 denotes they failed to meet.

Sample Input

3 5 6 XXXXXX XZ..ZX XXXXXX M.G... ......5 6 XXXXXX XZZ..X XXXXXX M..... ..G...10 10 .......... ..X....... ..M.X...X. X......... .X..X.X.X. .........X ..XX....X. X....G...X ...ZX.X... ...Z..X..X

Sample Output

11-1

AC

  • 對鬼魂的處理:每次先計算鬼經過當前時間能否到達M和G的位置(鬼魂先移動),然后判斷當M和G移動之后能否到達(移動之后位置的合法性)
  • 對于M和G:記錄他們所走的位置,因為雖然可能不是同時到達,但是如果他們都曾經到達一個地方(首先這個地方已經安全),那么其中一個人完全可以等著另一個人
  • 用兩個BFS求最短距離
#include <bits/stdc++.h> using namespace std; int n, m; char a[802][802]; bool vis[2][802][802]; vector<pair<int, int> > z; queue<pair<int, int> > q[2]; int dx[4] = {0, 0, 1, -1}; int dy[4] = {1, -1, 0, 0};bool judge(int x, int y, int time) {if (x < 0 || y < 0 || x >= n || y >= m || a[x][y] == 'X')return false;if (fabs(x - z[0].first) + fabs(y - z[0].second) <= time * 2)return false;if (fabs(x - z[1].first) + fabs(y - z[1].second) <= time * 2)return false;return true; } bool bfs(int t, int time) {int len = q[t].size();// 當前所有狀態(tài)移動一位 while (len--) {int x = q[t].front().first;int y = q[t].front().second;q[t].pop();// 鬼魂先走 if (!judge(x, y, time)) continue;int xx, yy;for (int i = 0; i < 4; ++i) {xx = x + dx[i];yy = y + dy[i];if (!judge(xx, yy, time) || vis[t][xx][yy]) continue;if (vis[(t ^ 1)][xx][yy]) return true;if (vis[t][xx][yy]) continue;vis[t][xx][yy] = true;q[t].push(make_pair(xx, yy));}}return false; }int main() { #ifndef ONLINE_JUDGEfreopen("in.txt", "r", stdin); #endifint t;scanf("%d", &t);while (t--) {memset(vis, false, sizeof(vis));scanf("%d%d", &n, &m);for (int i = 0; i < n; ++i) {scanf("%s", a[i]);for (int j = 0; j < m; ++j) {if (a[i][j] == 'M')q[0].push(make_pair(i, j)), vis[0][i][j] = true;if (a[i][j] == 'G')q[1].push(make_pair(i, j)), vis[1][i][j] = true;if (a[i][j] == 'Z')z.push_back(make_pair(i, j));}}int ans = 1;while (1) {// M走三步 if (bfs(0, ans)) break;if (bfs(0, ans)) break;if (bfs(0, ans)) break;// G走一步 if (bfs(1, ans)) break;if (q[0].empty() && q[1].empty()) {ans = -1;break;} ++ans;}printf("%d\n", ans);while (!q[1].empty()) {q[1].pop();}while (!q[0].empty()) {q[0].pop();}z.clear();}return 0; }

總結

以上是生活随笔為你收集整理的HDU Problem - 3085 Nightmare Ⅱ(双向BFS)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。