zoukankan      html  css  js  c++  java
  • 【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
    ",bfs());    
            }
            
            return 0;
        
    }
  • 相关阅读:
    [安卓]AndroidManifest.xml文件简介及结构
    [网络技术][转]PPTP协议解析
    ubuntu 12.04 (64位)下安装oracle 11g过程及问题总结
    deb包制作(转)
    短信部分之PDU简介及其格式(转)
    Siemens3508手机AT指令发送短信的实验
    GDB中应该知道的几个调试方法【转陈浩】
    JLink间接烧写【转自armobbs】
    [转]Java事件处理机制- 事件监听器的四种实现方式
    asp.net验证码
  • 原文地址:https://www.cnblogs.com/evenbao/p/9268724.html
Copyright © 2011-2022 走看看