杭电OJ 2102

 深搜A计划

#include <iostream>
#include <string>
#include <string.h>
#include <stack>
#include <vector>
#include <queue>
using namespace std;


int N, ROW, COLUMN, T;
int transportNum = 0;
char map[10][10][3];

struct Position
{
    int row;
    int col;
    int floor;

    Position()
    {
        row = 0;
        col = 0;
        floor = 0;
    }

    Position(const int row,
             const int col,
             const int floor)
    {
        this->row = row;
        this->col = col;
        this->floor = floor;
    }

    Position(Position tranPos, int floor)
    {
        this->row = tranPos.row;
        this->col = tranPos.col;
        this->floor = floor;
    }

    bool operator == (const Position& other)
    {
        if ((other.row == this->row) &&
            (other.col == this->col) &&
            (other.floor == this->floor))
        {
            return true;
        }
        return false;
    }

    bool operator != (const Position& other)
    {
        if ((other.row != this->row) &&
            (other.col != this->col) &&
            (other.floor != this->floor))
        {
            return true;
        }
        return false;
    }

    Position operator = (const Position& other)
    {
        this->row = other.row;
        this->col = other.col;
        this->floor = other.floor;
        return *this;
    }

}target, entrance;

bool savePrincess(Position curPos, int step, bool flag[][10][3]);

std::ostream& operator << (ostream& os, const Position& pos)
{
    return os << "row:" << pos.row
                << ", col:" << pos.col
                << ", floor:" << pos.floor;
}

int otherFloor(int floor)
{
    if (floor == 1)
    {
        return 2;
    }
    return 1;
}

bool move(int row, int col, int floor, int step, bool flag[][10][3])
{
    Position nextPos;
    if (map[row][col][floor] == '#')
    {
        if (flag[row][col][otherFloor(floor)])
        {
            return false;
        }
        nextPos = Position(row, col, otherFloor(floor));
    }
    else
    {
        nextPos = Position(row, col, floor);
    }
    flag[nextPos.row][nextPos.col][nextPos.floor] = true;
    if(savePrincess(nextPos, step+1, flag))
    {
        return true;
    }
    flag[nextPos.row][nextPos.col][nextPos.floor] = false;
    return false;
}

bool savePrincess(Position curPos, int step, bool flag[][10][3])
{
    // cout << curPos << ", step:" << step << endl;
    if (step > T)
    {
        return false;
    }

    if (curPos == target)
    {
        return true;
    }

    Position nextPos;
    if (curPos.row - 1 >= 0 &&
        map[curPos.row - 1][curPos.col][curPos.floor] != '*' &&
        flag[curPos.row - 1][curPos.col][curPos.floor] != true)    // 上
    {
        if (move(curPos.row - 1, curPos.col, curPos.floor, step, flag))
        {
            return true;
        }
    }
    if (curPos.row + 1 < ROW &&
        map[curPos.row + 1][curPos.col][curPos.floor] != '*' &&
        flag[curPos.row + 1][curPos.col][curPos.floor] != true)  // 下
    {
        if (move(curPos.row + 1, curPos.col, curPos.floor, step, flag))
        {
            return true;
        }
    }
    if (curPos.col - 1 >= 0 &&
        map[curPos.row][curPos.col - 1][curPos.floor] != '*' &&
        flag[curPos.row][curPos.col - 1][curPos.floor] != true)  // 左
    {
        if (move(curPos.row, curPos.col - 1, curPos.floor, step, flag))
        {
            return true;
        }
    }
    if (curPos.col + 1 < COLUMN &&
        map[curPos.row][curPos.col + 1][curPos.floor] != '*' &&
        flag[curPos.row][curPos.col + 1][curPos.floor] != true)  // 右
    {
        if (move(curPos.row, curPos.col + 1, curPos.floor, step, flag))
        {
            return true;
        }
    }

    return false;
}

void markUnAvailableTransportAsWall(Position* transPortPos)
{
    for (int i = 0; i < transportNum; i++)
    {
        if ((transPortPos[i].floor == 1) &&
            (map[transPortPos[i].row][transPortPos[i].col][2] == '*' ||
             map[transPortPos[i].row][transPortPos[i].col][2] == '#'))
        {
            map[transPortPos[i].row][transPortPos[i].col][transPortPos[i].floor] = '*';
        }
        else if ((transPortPos[i].floor == 2) &&
            (map[transPortPos[i].row][transPortPos[i].col][1] == '*' ||
             map[transPortPos[i].row][transPortPos[i].col][1] == '#'))
        {
            map[transPortPos[i].row][transPortPos[i].col][transPortPos[i].floor] = '*';
        }
    }
}

void valueRecv()
{
    cin >> ROW >> COLUMN >> T;
    transportNum = 0;
    Position transPos[200];
    for (int floor = 1; floor <= 2; floor++)
    {
        for (int row = 0; row < ROW; row++)
        {
            for (int col = 0; col < COLUMN; col++)
            {
                cin >> map[row][col][floor];
                if (map[row][col][floor] == '#')
                {
                    transPos[transportNum].row = row;
                    transPos[transportNum].col = col;
                    transPos[transportNum].floor = floor;
                    transportNum++;
                }
                else if (map[row][col][floor] == 'P')
                {
                    target.row = row;
                    target.col = col;
                    target.floor = floor;
                }
                else if (map[row][col][floor] == 'S')
                {
                    entrance.row = row;
                    entrance.col = col;
                    entrance.floor = floor;
                }
            }
        }
    }

    markUnAvailableTransportAsWall(transPos);
}

int main()
{
    cin >> N;
    for (int i = 0; i < N; i++)
    {
        memset(map, '*', sizeof map);
        valueRecv();
        
        // cout << "entrance:" << endl << entrance  << endl; 
        // cout << "target:" << endl << target << endl << endl;

        bool flag[10][10][3];
        memset(flag, false, sizeof flag);

        flag[entrance.row][entrance.col][entrance.floor] = true;
        if(savePrincess(entrance, 0, flag))
        {
            cout << "YES" << endl;
        }
        else
        {
            cout << "NO" << endl;
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值