【HDU 3085】 Nightmare Ⅱ

【题目链接】

           http://acm.hdu.edu.cn/showproblem.php?pid=3085

【算法】

           双向BFS

【代码】

           

#include<bits/stdc++.h>
using namespace std;
#define MAXN 810

const int dx[4] = {0,0,-1,1};
const int dy[4] = {-1,1,0,0};

int i,n,m,px,py,qx,qy,T;
char mp[MAXN][MAXN];
bool visited1[MAXN][MAXN];
bool visited2[MAXN][MAXN];

inline bool valid(int x,int y,int k)
{
        if (x <= 0 || x > n || y <= 0 || y > m) return false;
        if (abs(x - px) + abs(y - py) <= 2 * k) return false;
        if (abs(x - qx) + abs(y - qy) <= 2 * k) return false;
        if (mp[x][y] == 'X') return false;
        return true;
}
inline int bfs()
{
        int i,j,step,bx,by,gx,gy,tx,ty,s;
        queue< pair<int,int> > q1,q2;
        pair<int,int> cur;
        px = py = qx = qy = 0;
        for (i = 1; i <= n; i++)
        {
                for (j = 1; j <= m; j++)
                {
                        if (mp[i][j] == 'M')
                        {
                                bx = i;
                                by = j;
                        } 
                        if (mp[i][j] == 'G')
                        {
                                gx = i;
                                gy = j;
                        }
                        if (mp[i][j] == 'Z')
                        {
                                if (!px)
                                {
                                        px = i;
                                        py = j;
                                } else
                                {
                                        qx = i;
                                        qy = j;
                                }
                        }
                }
        }
        step = 0;
        while (!q1.empty()) q1.pop();
        while (!q2.empty()) q2.pop();
        memset(visited1,false,sizeof(visited1));
        memset(visited2,false,sizeof(visited2));
        visited1[bx][by] = true;
        visited2[gx][gy] = true;
        q1.push(make_pair(bx,by));
        q2.push(make_pair(gx,gy));
        while ((!q1.empty()) || (!q2.empty()))
        {
                step++;
                s = q1.size();
                for (i = 1; i <= s; i++)
                {
                        cur = q1.front();
                        q1.pop();
                        if (!valid(cur.first,cur.second,step)) continue;
                        for (j = 0; j < 4; j++)
                        {
                                tx = cur.first + dx[j];
                                ty = cur.second + dy[j];
                                if (valid(tx,ty,step) && !visited1[tx][ty])
                                {
                                        if (visited2[tx][ty]) return step;
                                        visited1[tx][ty] = true;
                                        q1.push(make_pair(tx,ty));
                                }
                        }            
                }
                s = q1.size();
                for (i = 1; i <= s; i++)
                {
                        cur = q1.front();
                        q1.pop();
                        if (!valid(cur.first,cur.second,step)) continue;
                        for (j = 0; j < 4; j++)
                        {
                                tx = cur.first + dx[j];
                                ty = cur.second + dy[j];
                                if (valid(tx,ty,step) && !visited1[tx][ty])
                                {
                                        if (visited2[tx][ty]) return step;
                                        visited1[tx][ty] = true;
                                        q1.push(make_pair(tx,ty));
                                }
                        }            
                }
                s = q1.size();
                for (i = 1; i <= s; i++)
                {
                        cur = q1.front();
                        q1.pop();
                        if (!valid(cur.first,cur.second,step)) continue;
                        for (j = 0; j < 4; j++)
                        {
                                tx = cur.first + dx[j];
                                ty = cur.second + dy[j];
                                if (valid(tx,ty,step) && !visited1[tx][ty])
                                {
                                        if (visited2[tx][ty]) return step;
                                        visited1[tx][ty] = true;
                                        q1.push(make_pair(tx,ty));
                                }
                        }            
                }
                s = q2.size();
                for (i = 1; i <= s; i++)
                {
                        cur = q2.front();
                        q2.pop();
                        if (!valid(cur.first,cur.second,step)) continue;
                        for (j = 0; j < 4; j++)
                        {
                                tx = cur.first + dx[j];
                                ty = cur.second + dy[j];
                                if (valid(tx,ty,step) && !visited2[tx][ty])
                                {
                                        if (visited1[tx][ty]) return step;
                                        visited2[tx][ty] = true;
                                        q2.push(make_pair(tx,ty));
                                }
                        }            
                }
        }
        return -1;
}

int main() 
{
        
        scanf("%d",&T);
        while (T--)
        {
                scanf("%d%d",&n,&m);
                for (i = 1; i <= n; i++) scanf("%s",mp[i]+1);
                printf("%d\n",bfs());    
        }
        
        return 0;
    
}

 

posted @ 2018-07-05 15:49  evenbao  阅读(158)  评论(0)    收藏  举报