zoukankan      html  css  js  c++  java
  • hdu1072 Nightmare (BFS)

    题目大意:给一个包含N*M个格子的迷宫,0代表墙,1代表通道,2代表入口,3代表出口,4代表地雷重置点,起始时地雷的爆炸时间是6秒,人每走一个格子需要1秒,问这个人最少多少秒可以走出迷宫,如果不能在爆炸前走出迷宫输出-1。

    思路:因为这题有个地雷重置点所以走过的路任然可以重复去走,所以在用bfs的时候对于已走过的路径不能标记。所以带出一个新的问题就是当两个地雷重置点之间可能会出现一直往复的走的现象,所以我们把只要走过的地雷重置点都标记为0(也就是说每个地雷重置点只用一次,因为多用并没有意义)。之后我们开始去广搜,直到找到出口!

    ps:思路来源于网路。

    思路来源代码:

      1 #include<iostream>  
      2 #include<queue>  
      3 using namespace std;  
      4   
      5 int col,row;  
      6 int map[8][8];  
      7 int mark[8][8];  
      8   
      9 struct node  
     10 {  
     11     int x,y,t_use,t_remine;  //t_use所走步数,t_remine为bomb离爆炸时间  
     12 }start;  
     13   
     14 void store_map()  
     15 {  
     16     for(int i=0;i<row;i++)  
     17     {  
     18         for(int j=0;j<col;j++)  
     19         {  
     20             cin>>map[i][j];  
     21             if(map[i][j]==2)  
     22             {  
     23                 start.x=i;  
     24                 start.y=j;  
     25                 start.t_use=0;  
     26                 start.t_remine=6;  
     27             }  
     28         }  
     29     }  
     30 }  
     31   
     32 void bfs()  
     33 {  
     34     const int help[4][2]={{1,0},{-1,0},{0,1},{0,-1}};  
     35   
     36     queue<node> myqueue;  
     37     myqueue.push(start);  
     38     memset(mark,0,sizeof(mark));    //开始时都是0  
     39     mark[start.x][start.y]=6;       //起点是6  
     40   
     41     bool flag=false;  
     42     int t_remine=0,t_use=0;  
     43     while(!myqueue.empty() && !flag)  
     44     {  
     45         node tmp=myqueue.front();  
     46         myqueue.pop();  
     47   
     48         for(int i=0;i<4;i++)  
     49         {  
     50             node temp;  
     51             temp.x=tmp.x+help[i][0];  
     52             temp.y=tmp.y+help[i][1];  
     53   
     54             if(map[temp.x][temp.y]!=0 && temp.x>=0 && temp.x<row && temp.y>=0 && temp.y<col)  //没越界并不是0,才可访问  
     55             {  
     56                 temp.t_use=tmp.t_use+1;  
     57                 temp.t_remine=tmp.t_remine-1;  
     58   
     59                 if(map[temp.x][temp.y]==4)  
     60                 {  
     61                     temp.t_remine=6;  
     62                 }  
     63                 else if(map[temp.x][temp.y]==3)  
     64                 {  
     65                     t_remine=tmp.t_remine-1;  
     66                     t_use=tmp.t_use+1;  
     67                     flag=true;  
     68                 }  
     69   
     70                 if(temp.t_remine>1 && mark[temp.x][temp.y]<temp.t_remine)   //满足条件才入队  
     71                 {  
     72                     mark[temp.x][temp.y]=temp.t_remine;  
     73                     myqueue.push(temp);  
     74                 }  
     75             }  
     76             if(flag) break;  
     77         }  
     78     }  
     79   
     80     if(flag)  
     81     {  
     82         if(t_remine>0)  
     83             cout<<t_use;  
     84         else  
     85             cout<<-1;  
     86     }  
     87     else  
     88         cout<<-1;  
     89     cout<<endl;  
     90 }  
     91   
     92 int main()  
     93 {  
     94     int t_case;  
     95   
     96     cin>>t_case;  
     97     while(t_case--)  
     98     {  
     99         cin>>row>>col;  
    100   
    101         store_map();  
    102   
    103         bfs();  
    104     }  
    105   
    106     return 0;  
    107 }  

    我的代码:

    View Code
     1 #include<stdio.h>
     2 #include<queue>
     3 #include<string.h>
     4 using namespace std;
     5 int map[8][8];
     6 int row,col;
     7 struct node
     8 {
     9     int x,y,step,time;
    10 };
    11 struct node start;
    12 void make_map()
    13 {
    14     //int i,j;
    15     for(int i=0;i<row;i++)
    16      {
    17         for(int j=0;j<col;j++)
    18         {
    19             scanf("%d",&map[i][j]);
    20             if(map[i][j] == 2)
    21             {
    22                 start.x=i;
    23                 start.y=j;
    24                 start.step=0;
    25                 start.time=6;
    26             }
    27         }
    28      }
    29 }
    30 void BFS()
    31 {
    32     int i;
    33     int help[4][2] = {{1,0},{-1,0},{0,1},{0,-1}};
    34     queue<node> myqueue;
    35     myqueue.push(start);
    36     struct node p1,p2;
    37     while(!myqueue.empty())
    38     {
    39         p1=myqueue.front();
    40         myqueue.pop();
    41         for(i=0;i<4;i++)
    42         {
    43             p2.step = p1.step + 1;
    44             p2.time = p1.time - 1;
    45             p2.x = p1.x + help[i][0];
    46             p2.y = p1.y + help[i][1];
    47             if(map[p2.x][p2.y] != 0&&p2.x >= 0&&p2.x < row&&p2.y >= 0&&p2.y < col&&p2.time > 0)
    48             {
    49                 if(map[p2.x][p2.y]==4)
    50                 {
    51                     map[p2.x][p2.y]=0;
    52                     p2.time=6;
    53                 }
    54                 else if(map[p2.x][p2.y]==3)
    55                 {
    56                     printf("%d\n",p2.step);
    57                     return ;
    58                 }
    59 
    60                 myqueue.push(p2);
    61             }
    62         }
    63     }
    64     printf("-1\n");
    65 }
    66 int main()
    67 {
    68     int t;
    69     scanf("%d",&t);
    70     while(t--)
    71     {
    72         scanf("%d %d",&row,&col);
    73         make_map();
    74         BFS();
    75     }
    76     return 0;
    77 }
  • 相关阅读:
    HTTP BIN测试
    JavaMail
    Linux内存分析
    HDU 4118 树形DP Holiday's Accommodation
    线性方程组的求解(C++)
    线性方程组的求解(C++)
    区间树(segment tree)
    区间树(segment tree)
    同余定理在算法求解中的应用
    同余定理在算法求解中的应用
  • 原文地址:https://www.cnblogs.com/ACshasow/p/2751433.html
Copyright © 2011-2022 走看看