zoukankan      html  css  js  c++  java
  • HDU 1728 逃离迷宫【BFS】

    题意:给一个图。规定至多转弯的次数,起点和终点。问在转弯的次数内能否到达终点。
    注意!给的n*m的map,n是列,m是行。同一个点可能经过多次。由于只统计拐弯次数,会出现走的步数少但拐弯多的情况。
     1 #include <cstdio>
     2 #include <cstring>
     3 #include <algorithm>
     4 #include <queue>
     5 #include <vector>
     6 using namespace std;
     7 typedef long long LL;
     8 
     9 const int INF = 0x3f3f3f3f;
    10 
    11 int k,n,m;
    12 char map[110][110];
    13 int vis[110][110]; //该点的转弯次数
    14 int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
    15 struct node
    16 {
    17     int x,y;
    18     int times; // 转弯次数
    19     int dir;   // 当前方向
    20 };
    21 
    22 int bfs(node s,node e) {
    23     s.dir=-1;
    24     s.times=0;
    25     queue<node> q;
    26     q.push(s);
    27     vis[s.y][s.x] = 0;
    28     while (!q.empty())
    29     {
    30         node cur = q.front();q.pop();
    31         if(cur.x == e.x && cur.y == e.y && cur.times<=k)return 1;
    32         for(int i=0;i<4;i++)
    33         {
    34             node tmp;
    35             tmp.x = cur.x+ dir[i][0];
    36             tmp.y = cur.y+ dir[i][1];
    37             tmp.dir = i;
    38             if(cur.dir==-1)tmp.times=0;
    39             else if(cur.dir!=tmp.dir)tmp.times=cur.times+1;
    40             else tmp.times=cur.times;
    41             //check条件
    42             if(tmp.x >= 1 && tmp.y >= 1 && tmp.x <= n && tmp.y <= m && tmp.times <=k && tmp.times <= vis[tmp.y][tmp.x] && map[tmp.y][tmp.x]=='.')
    43             {
    44                 q.push(tmp);
    45                 vis[tmp.y][tmp.x]=tmp.times; // 记录更小的转弯次数
    46             }
    47         }
    48     }
    49     return 0;
    50 }
    51 
    52 int main()
    53 {
    54     int t;
    55     scanf("%d",&t);
    56     while(t--){
    57         scanf("%d%d",&m,&n);
    58         for(int i=1;i<=m;i++)
    59         {
    60             getchar();
    61             for (int j = 1; j <= n; ++j) {
    62                 scanf("%c",&map[i][j]);
    63                 vis[i][j]=0x3f3f3f3f;
    64             }
    65         }
    66         node s,e;
    67         scanf("%d%d%d%d%d",&k,&s.x,&s.y,&e.x,&e.y);
    68         if(bfs(s,e))printf("yes
    ");
    69         else printf("no
    ");
    70     }
    71     return 0;
    72 }
  • 相关阅读:
    阿里云主机centos设置虚拟内存
    MySQL表名不区分大小写的设置方法
    java.security.KeyException
    Java.net.URLConnection和Apache HTTPClient的主要区别对比
    linux常用命令
    minerd
    kill常用
    阿里云centos 6安装Nginx+PHP+MySQL
    打开MySQL数据库远程访问的权限
    centos 安装MySql 5.6
  • 原文地址:https://www.cnblogs.com/demian/p/9236104.html
Copyright © 2011-2022 走看看