HDU 1728

#include <iostream>
#include <queue>
using namespace std;
#define MAX_COL 100
#define MAX_ROW 100
#define PATH '.'
#define BLOCK '*'
struct Pos
{
    int x, y;
};
struct Maze
{
    Pos cur_pos;//当前位置
    int array_dis[ 4 ];//四方向数组上,右,下,左
};

void Find_Way(char maze[MAX_COL][MAX_ROW], const int & row, const int & col);

int main(void)
{
    char  maze[MAX_COL][MAX_ROW];
    int N, row, col;
    cin >> N;
    while ( N-- )
    {
        cin >> row >> col;
        for (int i = 0; i < row; i++)
            for (int j = 0; j < col; j++)
                cin >> maze[ i ][ j ];
        Find_Way(maze, row, col);
    }
    return 0;
}

void Find_Way(char maze[MAX_COL][MAX_ROW], const int & row, const int & col)
{
    int record[MAX_COL][MAX_ROW] = { 0 };
    int k;
    cin >> k;
    Pos start_pos, end_pos;
    cin >> start_pos.y >> start_pos.x >> end_pos.y >> end_pos.x;
    start_pos.x--, start_pos.y--, end_pos.x--, end_pos.y--;
    Maze sta = { start_pos.x, start_pos.y, 0, 0, 0, 0};
    queue<Maze> queue_maze;
    queue_maze.push( sta );
    bool false_get_end = true;//是否未到终点
    if (start_pos.x == end_pos.x       && 
        start_pos.y == end_pos.y       && 
        maze[start_pos.x][start_pos.y] == PATH)
           false_get_end = false;
    while (queue_maze.size() && false_get_end)
    {
        Maze cur = queue_maze.front();
        Maze *dist = &queue_maze.front();
        int base_record = record[cur.cur_pos.x][cur.cur_pos.y];
        for (int i = 0; i < 4; ++i)
            if ( dist->array_dis[i] == 0 )//还没走过的方向
            {
                dist->array_dis[i] = 1;//标记已经走过
                break;
            }
        if (i >= 4)//当前点的4个方向都已经走完
            queue_maze.pop();
        else
        {//还有没走过的方向
            int r, c;//行,列
            switch ( i )
            {
            case 0://上
                r = -1, c = 0;
                break;
            case 1://右
                r = 0, c = 1;
                break;
            case 2://下
                r = 1, c = 0;
                break;
            case 3://左
                r = 0, c = -1;
            }
            cur.cur_pos.x += r, cur.cur_pos.y += c;
            while (cur.cur_pos.x >= 0 && cur.cur_pos.x   <  row  && 
                   cur.cur_pos.y >= 0 && cur.cur_pos.y   <  col  &&
                   maze[cur.cur_pos.x][cur.cur_pos.y]    == PATH &&
                   (record[cur.cur_pos.x][cur.cur_pos.y] == 0    ||//record[cur.cur_pos.x][cur.cur_pos.y] == 0未走过的点
                    record[cur.cur_pos.x][cur.cur_pos.y] >= base_record + 1))//当前路径比上次走的路径转弯少
            {
                record[cur.cur_pos.x][cur.cur_pos.y] = base_record + 1;//已经走过加权
                {//当前点压入队列
                    Maze sta = { cur.cur_pos.x, cur.cur_pos.y, 0, 0, 0, 0};
                    sta.array_dis[ i ] = 1;//标记当前方向已经走过
                    queue_maze.push( sta );
                }
                if (cur.cur_pos.x == end_pos.x && cur.cur_pos.y == end_pos.y)
                {
                    false_get_end = false;
                    break;
                }
                cur.cur_pos.x += r, cur.cur_pos.y += c;
            }//while
        }//else
    }//while
    if (!false_get_end && k + 1 >= record[end_pos.x][end_pos.y])//未到终点
        cout << "yes" << '\n';
    else
        cout << "no" << '\n';
}

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值