程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> HDU-3085-Nightmare Ⅱ(雙向BFS)

HDU-3085-Nightmare Ⅱ(雙向BFS)

編輯:C++入門知識

HDU-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 The next n lines describe the maze. Each line contains m characters. The characters may be:
‘.’ denotes an empty place, all can walk on.
‘X’ denotes a wall, only people can’t walk on.
‘M’ denotes little erriyue
‘G’ denotes the girl friend.
‘Z’ denotes the ghosts.
It is guaranteed that will contain exactly one letter M, one letter G and two letters Z.

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
1
1
-1

Author 二日月
Source HDU 2nd “Vegetable-Birds Cup” Programming Open Contest
思路:鬼分身所在的地方就直接變成牆,接著女孩走1步,男孩走3步,相遇就直接返回時間。(注意:鬼的分身也是可以繼續分的)
#include 
#include 

struct{
int x,y,step;
}que1[1000000],que2[1000000],que[1000000],t;

int n,m,gxa,gya,gxb,gyb,xa,ya,xb,yb,nxt[4][2]={{0,1},{1,0},{0,-1},{-1,0}},top,bottom,gt;
char mp[800][805];
bool vis1[800][800],vis2[800][800],vis[800][800];

void divide()//鬼分身,讓有鬼的地方都變成牆
{
    int i;

    while(top=gt) return;

        t.step++;

        for(i=0;i<4;i++)
        {
            t.x+=nxt[i][0];
            t.y+=nxt[i][1];

            if(t.x>=0 && t.x=0 && t.y=t1) break;

            t.step++;

            for(i=0;i<4;i++)
            {
                t.x+=nxt[i][0];
                t.y+=nxt[i][1];

                if(t.x>=0 && t.x=0 && t.y=t2) break;

            t.step++;

            for(i=0;i<4;i++)
            {
                t.x+=nxt[i][0];
                t.y+=nxt[i][1];

                if(t.x>=0 && t.x=0 && t.y

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved