杭电 HDU ACM 1728 逃离迷宫

逃离迷宫

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 18241    Accepted Submission(s): 4409

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

草 今天这个破题 弄的我下午第一节都没去上课,中午还被导员因为昨天晚自习西区省赛训练逃课 训了一顿!三千字检讨,靠 ,然后这个题 就因为一个细节错误wa一下午。

 现在说说这个题,一般我们bfs的时候广搜深度 和邻接表相关,这个因为关注的是方向,我们可以认为转0次弯是一层,1次弯是第二层…………按照这个规定bfs所到达的每个

结点的转弯次数都是最少次数,我错就错在,一开始程序逻辑上并没有一搜到底。而是遇到一个已经发现的结点然后

就不再往下遍历了。悲剧啊。

AC:

#include<string.h>
#include<iostream>
#include<queue>
using namespace std;
int vis[101][101];
char cnt[101][101];
int t,m,n,k,x1,y1,y2,x2;
int dic[4][2]= {{0,1},{1,0},{0,-1},{-1,0}};

struct A
{
    int xx,yy;
    int step ;

} ;

void  bfs(int x,int y)
{
    A cur,next;
    queue<A>ls;
    vis[x][y]=1;
    cur.xx=x;
    cur.yy=y;
    cur.step=-1;
    ls.push(cur);

    while(!ls.empty())
    {
        cur=ls.front();
        ls.pop();

        if(cur.step>=k)  //如果上一次已经搜索完的那一行或列已经等于k的话那么对于 这个结点其他方向的搜索,step肯定更大,直接break完事儿
            break;
        for(int i=0; i<4; i++)          //对于弹出的某个结点,我们深搜到底
        {
            next.xx=cur.xx+dic[i][0];
            next.yy=cur.yy+dic[i][1];
            next.step=cur.step+1;
            while(1)
            {

                if(cnt[next.xx][next.yy]!='*'&&next.xx>=0&&next.yy>=0&&next.xx<m&&next.yy<n)//这里不能加是否已经访问过的判断,就因为这里 wa好几个小时
                {                                             //因为要一搜到底如果可以往某个方向走且不出界,就可以继续走,只是不用他已访问过的压入栈、。
                    if(next.xx==y2-1&&next.yy==x2-1&&next.step<=k)
                    {
                        cout<<"yes"<<endl;
                        return ;
                    }

                    if(!vis[next.xx][next.yy])
                    {
                        vis[next.xx][next.yy]=1;
                        ls.push(next);
                    }
                    next.xx=next.xx+dic[i][0];
                    next.yy=next.yy+dic[i][1];
                }
                else
                {
                    break;
                }
            }
        }
    }
    cout<<"no"<<endl; // 如果搜索完毕没有按规定找到那么,输出no、
}

int main()
{
    cin>>t;
    while(t--)
    {
        cin>>m>>n;
        for(int i=0; i<m; i++)
            for(int j=0; j<n; j++)
                cin>>cnt[i][j];
        cin>>k>>x1>>y1>>x2>>y2;
        if(x1==x2&&y1==y2)            //注意两点重合的情况
        {
            cout<<"yes"<<endl;
            continue;
        }
        memset(vis,0,sizeof(vis));
        bfs(y1-1,x1-1);
    }
    return 0;
}
/****
34
5 6
......
.*.*.*
**.*.*
.*.*.*
......
3 6 1 1 4

2 2
. .
* .
1 1 1 2 2
********/
时间: 2024-08-08 09:41:57

杭电 HDU ACM 1728 逃离迷宫的相关文章

杭电 HDU ACM 1397 Goldbach&#39;s Conjecture

Goldbach's Conjecture Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 4976    Accepted Submission(s): 1901 Problem Description Goldbach's Conjecture: For any even number n greater than or equal

杭电 HDU ACM 5186 zhx&#39;s submissions

zhx's submissions Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 1892    Accepted Submission(s): 507 Problem Description As one of the most powerful brushes, zhx submits a lot of code on many

杭电 HDU ACM 1025 Constructing Roads In JGShining&#39;s Kingdom

Constructing Roads In JGShining's Kingdom Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 17732    Accepted Submission(s): 5023 Problem Description JGShining's kingdom consists of 2n(n is no mo

杭电HDU ACM Uncle Tom&#39;s Inherited Land*(二分图匹配 建模)

Uncle Tom's Inherited Land* Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 2496    Accepted Submission(s): 1028 Special Judge Problem Description Your old uncle Tom inherited a piece of land f

杭电 HDU ACM 圆桌会议

圆桌会议 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 3356    Accepted Submission(s): 2351 Problem Description HDU ACM集训队的队员在暑假集训时经常要讨论自己在做题中遇到的问题.每当面临自己解决不了的问题时,他们就会围坐在一张圆形的桌子旁进行交流,经过大家的讨论后一般没有

杭电 HDU ACM 1046 Tempter of the Bone

Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 83458    Accepted Submission(s): 22740 Problem Description The doggie found a bone in an ancient maze, which fascinated him a

杭电 HDU ACM 1496 Equations

Equations Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 6065    Accepted Submission(s): 2455 Problem Description Consider equations having the following form: a*x1^2+b*x2^2+c*x3^2+d*x4^2=0 a,

杭电 HDU ACM 1283 最简单的计算机

最简单的计算机 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5238    Accepted Submission(s): 2967 Problem Description 一个名叫是PigHeadThree的研究组织设计了一台实验用的计算机,命名为PpMm.PpMm只能执行简单的六种命令A,B,C,D,E,F:只有二个内存M1,M

杭电 HDU ACM 1213 How Many Tables

How Many Tables Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 17049    Accepted Submission(s): 8349 Problem Description Today is Ignatius' birthday. He invites a lot of friends. Now it's dinn