#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';
}
HDU 1728
最新推荐文章于 2020-02-10 21:10:31 发布