逃离迷宫
给定一个m × n (m行, n列)的迷宫,迷宫中有两个位置,gloria想从迷宫的一个位置走到另外一个位置,当然迷宫中有些地方是空地,gloria可以穿越,有些地方是障碍,她必须绕行,从迷宫的一个位置,只能走到与它相邻的4个位置中,当然在行走过程中,gloria不能走到迷宫外面去。令人头痛的是,gloria是个没什么方向感的人,因此,她在行走过程中,不能转太多弯了,否则她会晕倒的。我们假定给定的两个位置都是空地,初始时,gloria所面向的方向未定,她可以选择4个方向的任何一个出发,而不算成一次转弯。gloria能从一个位置走到另外一个位置吗?
Input 第1行为一个整数t (1 ≤ t ≤ 100),表示测试数据的个数,接下来为t组测试数据,每组测试数据中,
第1行为两个整数m, n (1 ≤ m, n ≤ 100),分别表示迷宫的行数和列数,接下来m行,每行包括n个字符,其中字符'.'表示该位置为空地,字符'*'表示该位置为障碍,输入数据中只有这两种字符,每组测试数据的最后一行为5个整数k, x 1, y 1, x 2, y 2 (1 ≤ k ≤ 10, 1 ≤ x1, x 2 ≤ n, 1 ≤ y 1, y 2 ≤ m),其中k表示gloria最多能转的弯数,(x 1, y 1), (x 2, y2)表示两个位置,其中x 1,x 2对应列,y 1, y 2对应行。
Output 每组测试数据对应为一行,若gloria能从一个位置走到另外一个位置,输出“yes”,否则输出“no”。Sample Input
2
5 5
...**
*.**.
.....
.....
*....
1 1 1 1 3
5 5
...**
*.**.
.....
.....
*....
2 1 1 1 3
Sample Output
no
yes
题解+解析:
#include <iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include <cstring>
#include <cmath>
#include <string>
#include <queue>
#define INF 0x7f7f7f7f
typedef long long ll;
using namespace std;
int w, h;
int n;
int sx, sy, gx,gy;
char map[105][105];
int m[4][2] = {-1,0,0,-1,1,0,0,1};
struct node
{
int x, y, k, s;//x,y表示坐标点,k表示方向,s表示转弯次数
node(int _x,int _y,int _k,int _s){x = _x; y=_y;k=_k;s=_s;}
};
bool flag;
bool visit[105][105];
int c;
void bfs(int x, int y, int k)
{
if (flag)//已经到达过终点了,剪枝
{
return;
}
queue<node> que;
visit[x][y] = 1;
que.push(node(x,y,k,0));//压入该初始方向的第一个node
while (que.size())
{
node next = que.front(); que.pop();
int nx = next.x- m[next.k][0], ny = next.y-m[next.k][1];//因为刚到的这个点也可以转弯,所以也要遍历这个点
while (1)//把这次转弯可以走到的点遍历
{
nx = nx + m[next.k][0]; ny = ny + m[next.k][1];//向前走
if (0<=nx&&nx<h&&0<=ny<w&&map[nx][ny]=='.')
{
visit[nx][ny]=1;
if (nx == gx&&ny==gy)//到达终点
{
flag = true;
return;
}
else
{
for (int i = 0; i < 4; i++)//遍历该点能走的四个方向
{
if (i%2 != next.k%2)//去掉向前走和向后走
{
int tx = nx + m[i][0], ty = ny + m[i][1];
if (0<=tx&&tx<h&&0<=ty<w&&map[tx][ty]=='.')
{
if (next.s+1 <= n)//符合条件
{
if (visit[tx][ty])//不去去过的点,剪枝
{
}
else
{
visit[tx][ty] = 1;
que.push(node(tx,ty,i,next.s+1));
}
}
else
{
continue;
}
}
else
{
continue;
}
}
else
{
continue;
}
}
}
}
else
{
break;
}
}
}
return;
}
int main()
{
int t;
scanf("%d", &t);
while (t--)
{
flag = false;
scanf("%d%d", &h, &w);
getchar();
c = 0;
memset(visit, 0, sizeof(visit));
for (int i = 0; i < h; i++)//初始化地图
{
gets(map[i]);
}
scanf("%d%d%d%d%d",&n,&sy,&sx,&gy,&gx);//录入起点,终点,转弯数据
sx--;sy--;gx--;gy--;
for (int i = 0; i < 4; i++)//初始四个方向的转完数都是0,遍历一遍
{
memset(visit, 0, sizeof(visit));
int nx = sx+m[i][0], ny = sy + m[i][1];
if (0<=nx&&nx<h&&0<=ny<w&&map[nx][ny]=='.')
{
visit[nx][ny] = 1;
bfs(nx, ny, i);
}
}
if (flag)
{
printf("yes\n");
}
else
{
printf("no\n");
}
}
return 0;
}
AC代码2
#include<set>
#include<map>
#include<cmath>
#include<queue>
#include<string>
#include<cstdio>
#include<cstring>
#include<sstream>
#include<cstdlib>
#include<cstring>
#include<sstream>
#include<iostream>
#include<algorithm>
//#include<bits/stdc++.h>
using namespace std;
const int maxn = 105;
const int INF = 0x3f3f3f3f;
const int mod = 7;
typedef long long ll;
#define PI 3.1415927
int n, m, p;
int k;
int sx, sy, gx, gy;
int flag = false;
char mp[maxn][maxn];
bool vis[maxn][maxn];
int dir[4][2] = {1,0,0,1,-1,0,0,-1};
struct Node
{
int x, y;
int time;
Node(int _x, int _y, int _t){x=_x;y=_y;time=_t;}
};
void bfs()
{
if(sx==gx&&sy==gy)
{
flag = true;
return;
}
queue<Node> que;
que.push(Node(sx,sy,0));
vis[sx][sy] = true;
for(int i = 0 ; i < 4; ++i)
{
int nx=sx+dir[i][0], ny=sy+dir[i][1];
if(0<=nx&&nx<n&&0<=ny&&ny<m&&mp[nx][ny]=='.')
{
while(1)
{
if(nx==gx&&ny==gy)
{
flag = true;
return;
}
vis[nx][ny] = true;
que.push(Node(nx,ny,0));
nx+=dir[i][0], ny+=dir[i][1];
if(0<=nx&&nx<n&&0<=ny&&ny<m&&mp[nx][ny]=='.'&&!vis[nx][ny])
continue;
else
break;
}
}
}
while(que.size())
{
Node nn = que.front(); que.pop();
if(nn.time+1>k) return;
for(int i = 0 ; i < 4; ++i)
{
int nx=nn.x+dir[i][0], ny=nn.y+dir[i][1];
if(0<=nx&&nx<n&&0<=ny&&ny<m&&mp[nx][ny]=='.')
{
while(1)
{
if(nx==gx&&ny==gy)
{
flag = true;
return;
}
if(!vis[nx][ny])
{
vis[nx][ny] = true;
que.push(Node(nx,ny,nn.time+1));
}
nx+=dir[i][0], ny+=dir[i][1];
if(0<=nx&&nx<n&&0<=ny&&ny<m&&mp[nx][ny]=='.')
continue;
else
break;
}
}
}
}
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
scanf("%d", &p);
getchar();
while(p--)
{
flag = false;
memset(vis, 0, sizeof(vis));
scanf("%d%d", &n, &m);getchar();
for(int i = 0; i < n; ++i)
gets(mp[i]);
scanf("%d%d%d%d%d", &k, &sy, &sx, &gy, &gx); getchar();
--sx;--sy;--gx;--gy;
bfs();
if(flag)
printf("yes\n");
else
printf("no\n");
}
return 0;
}