码迷,mamicode.com
首页 > Web开发 > 详细

Nightmare --- 炸弹时间复位

时间:2016-04-03 18:42:30      阅读:173      评论:0      收藏:0      [点我收藏+]

标签:

技术分享

技术分享

题目大意:

       该题为走迷宫,其条件有如下6个:

1, 迷宫用二维数组来表示;

2, 人走动时不能越界,不能在墙上走;

3, 当走到出口时,若剩余时间恰好为0,则失败;

4, 找到炸弹复位装置,若剩余时间恰好为0,则不能使用;

5, 炸弹复位装置可以使用若干次;

6, 只要走到复位装置所在位置,时间自动复置为6;

其中,数组中,0表示墙,1表示通道,2表示初始位置,3表示出口,4表示炸弹复位装置;

求走出迷宫所需要的最少步数,若不能在炸弹爆炸前走出来,输出-1.

大概思路:

       迷宫问题是经典的BFS问题,首先获取初始位置,调用队列,使其入队,定义方向数组,分别用(0,-1)、(-1,0)、(0,1)、(1,0)表示下上左右四个方向,对当前位置的四个方向进行判定,若能走得通,则使其入队。

代码如下:  渣渣代码   不要被吓倒   看见题  想一想 就去干 ! 不要怂就是干 !

 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<math.h>
 4 #include<iostream>
 5 #include<algorithm>
 6 #include<queue>
 7 #include<vector>
 8 #include<set>
 9 #include<stack>
10 #include<string>
11 #include<sstream>
12 #include<map>
13 #include<cctype>
14 using namespace std;
15 struct node
16 {
17     int x,y,time,step;
18 };
19 int a[10][10],n,m,b[4][2]={0,-1,0,1,-1,0,1,0},mark;
20 queue<node>Q;
21 int BFS(int x,int y)
22 {
23     node q={x,y,6,0};
24     Q.push(q);
25     while(!Q.empty())
26     {
27         node e=Q.front();
28         Q.pop();
29         for(int i=0;i<4;i++)
30         {
31             q.x=e.x+b[i][0],q.y=e.y+b[i][1],q.time=e.time-1,q.step=e.step+1;
32             if(q.x>=0&&q.x<m&&q.y>=0&&q.y<n&&q.time>0&&a[q.y][q.x]!=0)
33             {
34                 if(a[q.y][q.x]==3)
35                     return q.step;
36                 if(a[q.y][q.x]==4)
37                 {
38                     q.time=6;
39                     a[q.y][q.x]=0;
40                 }
41                 Q.push(q);
42             }
43         }
44     }
45     return -1;
46 }
47 int main()
48 {
49     int t,sx,sy;
50     scanf("%d",&t);
51     while(t--)
52     {
53         scanf("%d%d",&n,&m);
54         for(int i=0;i<n;i++)
55         {
56             for(int j=0;j<m;j++)
57             {
58                 scanf("%d",&a[i][j]);
59                 if(a[i][j]==2)
60                 {
61                     sx=j;
62                     sy=i;
63                 }
64             }
65         }
66         while(!Q.empty())
67             Q.pop();
68         mark=BFS(sx,sy);
69         printf("%d\n",mark);
70     }
71 }

 

Nightmare --- 炸弹时间复位

标签:

原文地址:http://www.cnblogs.com/A-FM/p/5350172.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!