逃离迷宫
Time Limit : 1000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other)
Problem Description
给定一个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[sub]1[/sub], y[sub]1[/sub], x[sub]2[/sub], y[sub]2[/sub] (1 ≤ k ≤ 10, 1 ≤ x[sub]1[/sub], x[sub]2[/sub] ≤ n, 1 ≤ y[sub]1[/sub], y[sub]2[/sub] ≤ m),其中k表示gloria最多能转的弯数,(x[sub]1[/sub], y[sub]1[/sub]), (x[sub]2[/sub], y[sub]2[/sub])表示两个位置,其中x[sub]1[/sub],x[sub]2[/sub]对应列,y[sub]1[/sub], y[sub]2[/sub]对应行。
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
中文 就不用题意了 要注意的是 题目给出的 先列后行
还有就是 自己提交错了 几次的地方 忘记了清空队列。。。
开始 有一个judge函数,就是一直错,我就把他给去了 直接if里面写了 不是很好看。。
还有就是 我的就是 turn 是从-1开始 我的第一次转弯也是有算的
根据第一 我们为了转弯此次 就是一条路走到黑,然后转弯,直到大于那个最大,或者无法转了
就回到前一步。
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
int k,n,m,f[105][105];
char ma[105][105];
int dir[4][2]= {{-1,0},{1,0},{0,-1},{0,1}};
struct node
{
int x,y,turn;
};
queue <node> vis;
int bfs(node en,int load)
{
node next,cur;
while(1)
{
load++;
if(load>k) return 0;
cur=vis.front();
while(cur.turn==load-1)
{
for(int i=0; i<4; i++)
{
next.x=cur.x+dir[i][1];
next.y=cur.y+dir[i][0];
next.turn=load;
while(next.x>0&&next.x<=n&&next.y>0&&next.y<=m&&ma[next.y][next.x]=='.'&&f[next.y][next.x]>=load)
{
if(next.y==en.y&&next.x==en.x)
return 1;
vis.push(next);
f[next.y][next.x]=load;
next.y=next.y+dir[i][0];
next.x=next.x+dir[i][1];
next.turn=load;
}
}
vis.pop();
if(vis.empty()) return 0;
else cur=vis.front();
}
}
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d %d",&m,&n);
for(int i=1; i<=m; i++)
{
for(int j=1; j<=n; j++)
{
cin>>ma[i][j];
f[i][j]=100;
}
}
node en,st;
scanf("%d%d%d%d%d",&k,&st.x,&st.y,&en.x,&en.y);
if (ma[en.y][en.x] == '*')
printf("no\n");
else if (st.y == en.y && st.x == en.x)
printf("yes\n");
else
{
st.turn=-1;
f[st.y][st.x]=-1;
vis.push(st);
if(bfs(en,-1))
printf("yes\n");
else
printf("no\n");
while(!vis.empty())
vis.pop();
}
}
return 0;
}