zoukankan      html  css  js  c++  java
  • hdoj1072 Nightmare(bfs)

    题目大意:

    在迷宫中有一个炸弹,过六个单位时间就会爆炸,要你求一个起点到迷宫的终点的最短距离,迷宫中有时间重置器,当你走到这个格子,炸弹的爆炸时间重新置为0,迷宫中标识为墙壁的格子不能走,到达任意一个格子时,炸弹计数器为0时,则失败。

    求逃出迷宫最短距离,bfs。

    但是有两个限制条件,1.求最短,2.炸弹时间不小于1(等于零不OK)。

    解锁bfs新姿势。(赞!

     1 #include<iostream>
     2 #include<cstring>
     3 #include<queue>
     4 #define maxn 10
     5 using namespace std;
     6 const int dx[]={-1,1,0,0},dy[]={0,0,-1,1};
     7 int map[maxn][maxn],V[maxn][maxn];
     8 struct point{
     9     int x,y;
    10     int temp,time; // 步数,剩余时间 
    11 }start;
    12 int ans,n,m;
    13 int bfs(){
    14     queue<point> Q;
    15     Q.push(start);
    16     while (!Q.empty()){
    17         point pre=Q.front();
    18         if (map[pre.x][pre.y]==3 && pre.time>0){
    19             ans=pre.temp;
    20             return 1;
    21         }
    22         Q.pop();
    23         for (int i=0;i<4;i++){
    24             point next;
    25             next.x=pre.x+dx[i];
    26             next.y=pre.y+dy[i];
    27             next.temp=pre.temp+1;
    28             next.time=pre.time-1;
    29             if (next.x<0 || next.x>=n || next.y<0 || next.y>=m || next.time<=0 || map[next.x][next.y]==0) continue;
    30             if (map[next.x][next.y]==3){
    31                 ans=next.temp;
    32                 return 1;
    33             }
    34             else if (map[next.x][next.y]==4){
    35                 next.time=6;
    36                 map[next.x][next.y]=0;
    37             }
    38             Q.push(next);
    39         }
    40     }
    41     return 0;
    42 }
    43 int main(){
    44     int t;
    45     cin >> t;
    46     while (t--){
    47         cin >> n >> m;
    48         for (int i=0;i<n;i++){
    49             for (int j=0;j<m;j++){
    50                 cin >> map[i][j];
    51                 if (map[i][j]==2){
    52                     start.x=i;
    53                     start.y=j;
    54                     start.temp=0;
    55                     start.time=6;
    56                 }
    57             }
    58         }
    59         memset(V,0,sizeof(V));
    60         ans=0;
    61         if (bfs()) cout << ans << endl;
    62         else cout << -1 << endl;
    63     }
    64     return 0;
    65 }
  • 相关阅读:
    vim初试(Hello World)
    CSP201809-2 买菜(超简单的方法!!)
    CSP202006-2 稀疏向量
    CSP202012-2 期末预测之最佳阈值
    浮点数表示
    结构体
    全排列-康托展开及逆展开
    CA-031 上手Games101环境 Games101环境怎么配置
    计算机图形学 实验四 AET算法
    计算机图形学 实验三 梁氏裁剪算法
  • 原文地址:https://www.cnblogs.com/changer-qyz/p/8486357.html
Copyright © 2011-2022 走看看