zoukankan      html  css  js  c++  java
  • hdu 1728(BFS)

    逃离迷宫

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


    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
    被最后的坑了,就是他们所对应的列和行和我们的不一样,
    用BFS从一个方向一直搜
     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cmath>
     4 #include<cstring>
     5 #include<algorithm>
     6 #include<queue>
     7 #include<map>
     8 #include<set>
     9 #include<vector>
    10 #include<cstdlib>
    11 #include<string>
    12 #define eps 0.000000001
    13 typedef long long ll;
    14 typedef unsigned long long LL;
    15 using namespace std;
    16 const int N=100;
    17 char mp[N][N];
    18 int visited[N][N];
    19 int n,m,xx,yy,x2,y2,k;
    20 int _x[4]={1,0,-1,0};
    21 int _y[4]={0,1,0,-1};
    22 struct node{
    23     int x,y;
    24     int step;
    25 }G;
    26 int check(int x,int y){
    27     if(x>=1&&x<=m&&y>=1&&y<=n&&mp[x][y]!='*'){
    28         return 1;
    29     }
    30     return 0;
    31 }
    32 void BFS(){
    33     int flag=0;
    34     queue<node>Q;
    35     Q.push(G);
    36     node first,next;
    37     while(!Q.empty()){
    38         first=Q.front();
    39         Q.pop();
    40         if(first.x==y2&&first.y==x2&&first.step<=k){
    41                // cout<<2<<endl;
    42             flag=1;
    43             break;
    44         }
    45         next.step=first.step+1;
    46         for(int i=0;i<4;i++){
    47             int a=first.x+_x[i];
    48             int b=first.y+_y[i];
    49            // cout<<a<<" "<<b<<endl;
    50             while(check(a,b)){
    51                 if(visited[a][b]==0){
    52                    // cout<<3<<endl;
    53                     visited[a][b]=1;
    54                     next.x=a;
    55                     next.y=b;
    56                     Q.push(next);
    57                 }
    58                 a=a+_x[i];
    59                 b=b+_y[i];
    60             }
    61         }
    62     }
    63     if(flag==1)cout<<"yes"<<endl;
    64     else
    65         cout<<"no"<<endl;
    66 
    67 }
    68 int main(){
    69     int t;
    70     scanf("%d",&t);
    71     while(t--){
    72         memset(visited,0,sizeof(visited));
    73         scanf("%d%d",&m,&n);
    74         for(int i=1;i<=m;i++){
    75         getchar();
    76         for(int j=1;j<=n;j++){
    77             scanf("%c",&mp[i][j]);
    78         }
    79         }
    80         scanf("%d%d%d%d%d",&k,&xx,&yy,&x2,&y2);
    81         G.x=yy;
    82         G.y=xx;
    83         G.step=-1;
    84         visited[G.x][G.y]=1;
    85         BFS();
    86     }
    87 }
  • 相关阅读:
    目标检测之YOLOv3
    残差网络(ResNet)
    FPN详解
    YOLOv2/YOLO 9000深入理解
    批归一化(BN)
    全卷积网络FCN
    基于深度学习的目标检测算法综述
    目标检测两个基础部分——backbone and detection head
    YOLOv1 深入理解
    内置模块
  • 原文地址:https://www.cnblogs.com/Aa1039510121/p/6323657.html
Copyright © 2011-2022 走看看