hdu 1728 逃离迷宫 转弯bfs 巨坑

                                       逃离迷宫

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 33564    Accepted Submission(s): 8198


 

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, x1, y1, x2, y2 (1 ≤ k ≤ 10, 1 ≤ x1, x2 ≤ n, 1 ≤ y1, y2 ≤ m),其中k表示gloria最多能转的弯数,(x1, y1), (x2, y2)表示两个位置,其中x1,x2对应列,y1, y2对应行。

 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

这个题涉及转弯次数那么只需要记录一下每个点的方向以及转弯的次数(当然还有坐标)就行了;

但是更重要的一点是 到达一个点 可能需要转2次弯 或者转 3次弯 ,很明显我们希望 转弯的次数越少越好,所以不能像普通bfs一样把走过的点标记下,然后再次碰到就不走了,也就是标记方法,和加入队列的判断条件要注意;

想清楚这些之后,在思考如果到达同一个点都是转2次弯,第二个点是否就不用加入队列了?

错!!!因为到达这两个点的方向不同,对答案的影响肯定就不一样。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
#include<set>
#include<cmath>
#define sqr(x) ((x) * (x))
using namespace std;
const int MOD=999997;
const int INF=0x3f3f3f3f;
const int maxn=1e6+5;
struct node
{
    int x,y;
    int st;
    int di;
};
int n,m;
int sx,sy,ex,ey;
int k;
int vis[105][105];
char   a[105][105];
int dx[5]={0,0,1,-1};
int dy[5]={1,-1,0,0};
inline bool check(node t)
{
    if(a[t.x][t.y]=='.'&&t.x>=0&&t.x<n&&t.y>=0&&t.y<m) return true;
    return false;
}
void bfs()
{
    queue<node> q;
    memset(vis,-1,sizeof vis);
    node t;
    vis[sx][sy]=k;
    for(int i=0;i<4;++i)
    {
        t.di=i;
        t.st=k;
        t.x=sx+dx[i];
        t.y=sy+dy[i];
        if(check(t))
        {
            vis[t.x][t.y]=k;
            q.push(t);
        }
    }
    while(!q.empty())
    {
        node f=q.front();
        //cout<<f.x<<' '<<f.y<<endl;
        q.pop();
        if(f.x==ex&&f.y==ey)
        {
            puts("yes");
            return ;
        }
        node tt;
        for(int i=0;i<4;++i)
        {
            tt.di=i;
            tt.st=f.st;
            tt.x=f.x+dx[i];
            tt.y=f.y+dy[i];
            if(check(tt))
            {
                if(i!=f.di) tt.st--;
                if(tt.st<0) continue;
                if(tt.st>=vis[tt.x][tt.y])
                {
                    vis[tt.x][tt.y]=tt.st;
                    q.push(tt);
                }
            }
        }
    }
    puts("no");
    return;
}
int main()
{
    int tes;
    cin>>tes;
    while(tes--)
    {
        scanf("%d%d",&n,&m);
        for(int i=0;i<n;++i)
            scanf("%s",a[i]);
        scanf("%d%d%d%d%d",&k,&sy,&sx,&ey,&ex);
        sx--;
        sy--;
        ex--;
        ey--;
        if(sx==ex&&sy==ey)
        {
            puts("yes");
            continue;
        }
        else if(a[ex][ey]=='*')
        {
            puts("no");
            continue;
        }
        bfs();
    }

}

猜你喜欢

转载自blog.csdn.net/codetypeman/article/details/81280643