1. 程式人生 > >搜尋進階------G

搜尋進階------G

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<n, m<800) 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

解這道題重要的是,不用bfs怪獸到達某點的時間,可直接利用兩點間的曼哈頓距離來判斷是否這個點有沒有怪獸,dis <= 2 * t 代表這個點在t秒有怪獸 接下來就是雙向bfs 注意:不能每次擴充套件一個節點,我每次一個節點一個節點的擴充套件,wa的我都想哭,找不到為什麼wa,也沒有資料 但是如果一層一層的擴充套件就可以a了

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#include<queue>
using namespace std;
typedef long long LL;
const int inf = 0x3f3f3f3f;
const int N = 805;
#define pb push_back
#define mp make_pair
#define fi first
#define se second

int col[5] = {0,0,-1,1};
int con[5] = {-1,1,0,0};
int n,m;
char s[N][N];
int vis[N][N];
pair<int,int>ghost[2];
int sx,sy,tx,ty;
int step;
queue<pair<int,int> >que[2];

bool judge(int x,int y)
{
    if(x < 0 || y < 0 || x >= n || y >= m)
        return false;
    if(s[x][y] == 'X')
        return false;
    int dis1 = abs(x - ghost[0].fi) + abs(y - ghost[0].se);
    int dis2 = abs(x - ghost[1].fi) + abs(y - ghost[1].se);
    if(dis1 <= 2 * step || dis2 <= 2 * step)
        return false;
    return true;
}

bool bfs(int flag)
{
    queue<pair<int,int> >ptr = que[flag];
    int len = (flag == 0)? 3 : 1;
    for(int i = 0;i < len;++i)
    {
        while(!ptr.empty()){
            pair<int,int>tmp = ptr.front();
            ptr.pop();
            que[flag].pop();
            //因為是怪獸先走,人後走,所以要先判定怪獸能達到當前位置
            if(!judge(tmp.fi,tmp.se)) continue;
            for(int j = 0;j < 4;++j){
                int x = tmp.fi + col[j];
                int y = tmp.se + con[j];
                if(!judge(x,y) || vis[x][y] == flag)
                    continue;
                if(vis[x][y] == !flag){
                    return true;
                }
                vis[x][y] = flag;
                que[flag].push(mp(x,y));
            }
        }
        ptr = que[flag];
    }
    return false;
}

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d %d",&n,&m);
        int k = 0;
        for(int i = 0;i < n;++i)
        {
            scanf("%s",s[i]);
            for(int j = 0;j < m;++j){
                if(s[i][j] == 'M') sx = i,sy = j;
                if(s[i][j] == 'G') tx = i,ty = j;
                if(s[i][j] == 'Z'){
                    ghost[k++] = mp(i,j);
                }
            }
        }
        while(!que[0].empty()) que[0].pop();
        while(!que[1].empty()) que[1].pop();
        memset(vis,-1,sizeof(vis));
        step = 0;
        int ans = -1;
        que[0].push(mp(sx,sy));
        que[1].push(mp(tx,ty));
        vis[sx][sy] = 0;
        vis[tx][ty] = 1;
        while(!que[0].empty() && !que[1].empty())
        {
            step++;
            if(bfs(0) || bfs(1)){
                ans = step;
                break;
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}