中文--
题目:给定一个m×n(m行,n列)的迷宫,迷宫中有两个位置,gloria想从迷宫的一个位置走到另外一个位置,当然迷宫中有些地方是空地,gloria可以穿越,有些地方是障碍,她必须绕行,从迷宫的一个位置,只能走到与它相邻的4个位置中,当然在行走过程中,格洛里亚不能走到迷宫外面去。令人头痛的是,格洛里亚是个没什么方向感的人,因此,她在行走过程中,不能转太多弯了,否则她会晕倒的。我们假定给定的两个位置都是空地,初始时,凯莱所面向的方向未定,她可以选择4个方向的任何一个出发,而不算成一次转弯.gloria能从一个位置走到另外一个位置吗?
输入: 第1行为一个整数t(1≤t≤100),表示测试数据的个数,接下来为t组测数据,
第1行为两个整数m,n(1≤m,n ≤100),表示该位置为障碍,输入数据中只有这两种字符,每组测试数据的最后一行为5个整数K,X 1,Y 1,X 2,Y 2(1≤ķ≤10,1≤X 1,X 2 ≤N,1个≤ÿ 1,Y 2 ≤米),其中ķ表示格洛里亚最多能转的弯数,(X 1,Y 1),(X 2,Y 2)表示两个位置,其中X 1,X 2对应列,Y 1,y 2对应行。
输出:
每组测试数据对应为一行,若凯莱能从一个位置走到另外一个位置,输出“是”,否则输出“不”。
数据输入:
2
5 5
... **
*。**。
.....
.....
* ....
1 1 1 1 3
5 5
... **
*。**。
.....
.....
* ....
2 1 1 1 3
数据输出:
no
yes
#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std; //所有需要的头文件
char str[105][105]; //字符串数组
int id[105][105]; //标记坐标是否走过的数组
int n,m; //行 列
int k,x1,y1,x2,y2; //变量:弯数以及起末坐标
int dir[4][2]={-1,0,1,0,0,-1,0,1}; //上下左右四个方向,用x,y坐标值的变化来实现
int flag; //最后到达终点的标志
struct node
{
int x;
int y;
int k;
}; //结构体(节点)
void bfs()
{
node t;
t.x=x1;
t.y=y1;
t.k=-1; //起点
id[x1][y1]=1; //已访问
queue<node> q; //队列<数据类型> 变量;
q.push(t); //入队
while(q.empty()==0) //若队列为空
{
node t1;
t1=q.front(); //队首
q.pop(); //出队
if(t1.x==x2&&t1.y==y2&&t1.k<=k) //是否满足要求到达终点
flag=true; //标记为真
for(int i=0;i<4;i++)
{
node t2;
t2.x=t1.x+dir[i][0]; //走下一步
t2.y=t1.y+dir[i][1];
while(t2.x>=1&&t2.x<=n&&t2.y>=1&&t2.y<=m&&str[t2.x][t2.y]!='*') //是否不出界、不撞墙
{
if(id[t2.x][t2.y]==0) //未访问
{
t2.k=t1.k+1; //弯数+1
id[t2.x][t2.y]=1; //标记为访问
q.push(t2); //入队
}
node t3;
t3.x=t2.x+dir[i][0];
t3.y=t2.x+dir[i][1];
t2=t3; //t2=一个方向上能走的路
}
}
}
}
int main()
{
int N;
scanf("%d",&N);
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
scanf(" %c,&str[i][j]);
memset(id,0,sizeof(id));
scanf("%d%d%d%d%d",&k,&x1,&y1,&x2,&y2);
flag=false;
bfs();
if(flag==true)
printf("yes\n");
else
printf("no\n);
return 0;
}