HDU - 2102 - A計劃 (BFS)

題意:

兩層地圖,只有通過時空傳輸機才能在兩層之間移動,問能否在規定時間移動到'P'

#include <iostream>
#include <queue>
#include <string.h>
using namespace std;
const int maxn = 100 + 10;
char G[2][maxn][maxn];
int n,m,t;
int dx[4] = {0,0,1,-1};
int dy[4] = {1,-1,0,0};
struct Point
{
    int x,y,f,step;
};
bool check(Point p)
{
    if(p.step > t || G[p.f][p.x][p.y] == '*' )
        return 0;
    return 1;
}
int bfs()
{
    queue<Point> que;
    Point now,next;
    now.x  = now.y = 1;
    now.f =  now.step = 0;
    que.push(now);
    while(!que.empty())
    {
        now = que.front();
        que.pop();
        if(G[now.f][now.x][now.y] == '*' ) continue;
        if(G[now.f][now.x][now.y] == 'P' )
            return 1;
        G[now.f][now.x][now.y] = '*';
        for(int i = 0; i < 4; i++)
        {
            next.x = now.x + dx[i];
            next.y = now.y + dy[i];
            next.step = now.step + 1;
            next.f = now.f;
            if(!check(next)) continue;
            if(G[next.f][next.x][next.y] == '#')
            {
                G[next.f][next.x][next.y] = '*';
                next.f = 1 - next.f;
                if(G[next.f][next.x][next.y] == '#' || G[next.f][next.x][next.y]=='*')
                {
                    G[next.f][next.x][next.y]  = G[1-next.f][next.x][next.y]  = '*';
                    continue;
                }
            }
            que.push(next);
        }
    }
    return 0;
}
int main()
{
    int c;
    for(cin >> c; c > 0; c--)
    {
        memset(G,'*',sizeof G);
        cin>>n>>m>>t;
        for(int i = 1; i <= n; i++)
        {
            for(int j = 1; j <= m; j++)
            {
                cin>>G[0][i][j];
            }
        }
        for(int i = 1; i <= n; i++)
        {
            for(int j = 1; j <= m; j++)
            {
                cin>>G[1][i][j];
            }
        }
        if(bfs())
        {
            cout<<"YES"<<endl;
        }
        else
        {
            cout<<"NO"<<endl;
        }
    }
    return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章