非常标准的BFS

第一次写错了很多

1、到达4时设置为墙就好了  避免了死循环

2、不用开d数组   在结构体里面就行了

3、结构体初始化函数的写法: Node(int x=0,int y=0,int oil=0):x(x),y(y),oil(oil){}

4、bfs的FOR里面的判断条件可以写的很清晰!就判断可以的  不可以的直接不处理!

#include<bits/stdc++.h>
using namespace std;
int sx,sy,ex,ey;
int d[][],a[][],d2[][];
bool f[][];
int n,m;
struct aa
{
int x;
int y;
int oil;
aa(int x=,int y=,int oil=):x(x),y(y),oil(oil){}
}; void bfs()
{ const int dr[]={-,,,};
const int dc[]={,,,-}; queue<aa>q;
memset(d,-,sizeof(d)); memset(f,true,sizeof(f));
aa u(sx,sy,);d[sx][sy]=;
q.push(u);
// printf("u:%d %d %d\n",u.x,u.y,u.oil);
while(!q.empty())
{ aa u=q.front();q.pop(); if(u.x==ex&&u.y==ey) {printf("%d\n",d[ex][ey]);return;}
for(int i=;i<=;i++)
{
aa v(u.x+dr[i],u.y+dc[i],u.oil-);
d[v.x][v.y]=d[u.x][u.y]+; if(v.x<||v.x>n||v.y<||v.y>m) continue ;
if(v.oil==)continue ;
if(a[v.x][v.y]==){ v.oil=; a[v.x][v.y]=; q.push(v); }
if(a[v.x][v.y]!=)
{
//printf("v:%d %d %d\n",v.x,v.y,v.oil);
q.push(v);} } } printf("-1\n");
} int main()
{
int cas;cin>>cas;
while(cas--)
{ cin>>n>>m;
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
{
scanf("%d",&a[i][j]);
if(a[i][j]==){sx=i;sy=j;}
if(a[i][j]==){ex=i;ey=j;}
}
bfs(); } return ;
}

第二次:简洁了许多

#include<bits/stdc++.h>
using namespace std; int world[][];int sx,sy,ex,ey;int n,m; struct node
{
int x,y,d,oil; node(int x=,int y=,int d=,int oil=):x(x),y(y),d(d),oil(oil){}
}; void bfs()
{
int dx[]={,,,-};
int dy[]={,,-,}; node u(sx,sy,,);
queue<node>q;
q.push(u); while(!q.empty())
{
node u=q.front();q.pop();
if(u.x==ex&&u.y==ey){printf("%d\n",u.d);return;} for(int i=;i<;i++)
{
node v(u.x+dx[i],u.y+dy[i],u.d+,u.oil-);
if(v.x>=&&v.x<=n&&v.y>=&&v.y<=m&&v.oil)
{
if(world[v.x][v.y]==){v.oil=;world[v.x][v.y]=;q.push(v);} else if(world[v.x][v.y]>) q.push(v);
}
}
}
printf("-1\n");
} int main()
{
int cas;cin>>cas;
while(cas--)
{
cin>>n>>m;
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
{scanf("%d",&world[i][j]);
if(world[i][j]==){sx=i;sy=j;}
if(world[i][j]==){ex=i;ey=j;} } bfs(); } return ;
}

大神的简洁代码:

#include <iostream>
#include <algorithm>
#include <queue>
#include <memory.h>
#include <stdio.h>
using namespace std;
#define Size 8
/*
这题目可以重复道路.只需要把控制炸弹的地方使用后,变成墙壁即可.
*/
struct Node
{
int x;
int y;
int time;//time代表已用的时间.
int rest;//rest代表剩余的时间.
//按照时间从高到低排列.
bool operator < (Node a) const
{
return this->time > a.time;
}
}; int world[Size][Size];
int n, m;
int temp;
int dir[][] = { { , }, { , }, { -, }, { , - } };
int sx, sy;
int rx, ry;
/*
0代表墙壁.1代表正常路.2代表起点.3代表终点.4代表炸弹控制器.
*/
int bfs()
{
priority_queue<Node> temp;
Node now, next, s;
s.x = sx;
s.y = sy;
s.time = ;
s.rest = ;
temp.push(s); while (!temp.empty())
{
now = temp.top();
temp.pop(); if (now.x == rx && now.y == ry && now.rest > )
{
return now.time;
}
//减枝.当剩余时间为1时.还没找到出口,说明到不了了.
if (now.rest == )
continue; for (int i = ; i < ; ++i)
{
next.x = now.x + dir[i][];
next.y = now.y + dir[i][];
next.time = now.time + ;
next.rest = now.rest - ;
//判断位置是否合理.
if (next.x >= && next.y >= && next.x < n && next.y < m && world[next.x][next.y] != && next.rest >= )
{
//如果他到了炸弹这里.
if (world[next.x][next.y] == )
{
next.rest = ;
//改为墙壁即可.
world[next.x][next.y] = ;
}
temp.push(next);
}
}
}
return -;
} int main()
{
int t;
scanf("%d", &t);
for (int i = ; i < t; ++i)
{
scanf("%d%d", &n,&m);
memset(world, , sizeof(world));
for (int j = ; j < n; ++j)
{
for (int k = ; k < m; ++k)
{
scanf("%d", &temp);
//初始位置.
if (temp == )
{
sx = j;
sy = k;
}
//目标位置.
else if (temp == )
{
rx = j;
ry = k;
}
world[j][k] = temp;
}
}
printf("%d\n", bfs());
} return ;
}

Nightmare HDU1072的更多相关文章

  1. Nightmare(DFS)

    Nightmare    hdu1072 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Oth ...

  2. HDU-1072 Nightmare (bfs+贪心)

    Nightmare Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Sub ...

  3. hdu1072(Nightmare)bfs

    Nightmare Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total ...

  4. HDU1072 Nightmare(BFS) 2016-07-24 14:02 40人阅读 评论(0) 收藏

    Nightmare Problem Description Ignatius had a nightmare last night. He found himself in a labyrinth w ...

  5. HDU1072:Nightmare [DFS]

    题目链接:Nightmare 题意: 给出一张n*m的图,0代表墙,1代表可以走,2代表起始点,3代表终点,4代表炸弹重置点 问是否能从起点到达终点 分析: 一道很好的DFS题目,炸弹重置点必然最多走 ...

  6. HDU1072:Nightmare

    传送门 题意 给出一张n*m的图 0.墙 1.可走之路 2.起始点 3.终点 4.时间重置点 问是否能到达终点 分析 我的训练专题第一题,一开始我设个vis数组记录,然后写炸,不能处理重置点根vis的 ...

  7. HDU 1072 Nightmare

    Description Ignatius had a nightmare last night. He found himself in a labyrinth with a time bomb on ...

  8. Nightmare基于phantomjs的自动化测试套件

    今天将介绍一款自动化测试套件名叫nightmare,他是一个基于phantomjs的测试框架,一个基于phantomjs之上为测试应用封装的一套high level API.其API以goto, re ...

  9. POJ 1984 Navigation Nightmare 带全并查集

    Navigation Nightmare   Description Farmer John's pastoral neighborhood has N farms (2 <= N <= ...

随机推荐

  1. Prezento – 轻量、简单的 jQuery 幻灯片插件

    Prezento 是一个超级简单的 jQuery 幻灯片插件.可以让你网页以新颖的交互方式呈现.另外,Prezento 支持响应式设计,配置项也很灵活,可以根据你需要的效果配置. 您可能感兴趣的相关文 ...

  2. linux4.10.8 内核移植(一)---环境搭建及适配单板。

    一.环境搭建 源码包下载:git clone https://www.kernel.org/pub/linux/kernel/v4.x/linux-4.10.8.tar.gz 或者直接去kernel. ...

  3. dp题

    1.luogu 1484种树 50分思路:dp,但是数据规模过大没法dp选择奇怪贪心 dp方程 到i坑种j树 dp[i][j]=max(dp[i-1][j],dp[i-2][j-1]) 100分思路: ...

  4. 整数数字调节框QSpinBox

    样式: import sys from PyQt5.QtWidgets import QApplication, QWidget, QSpinBox, QDoubleSpinBox, QHBoxLay ...

  5. Linux之包管理工具总结[RPM/DPKG]-[YUM/APT]

    0.关键词解释 RPM:Red Hat Package Manager(原名),RPM Package Manager(现名,递归缩写,类似于GNU的命名); 解释:RPM软件包管理器 YUM:Yel ...

  6. android的五个进程优先级,内存不足时被清理的顺序

    Android操作系统尝试尽可能长时间的保持应用的进程,但当可用内存很低时最终要移走一部分进程.怎样确定那些程序可以运行,那些要被销毁,Android让每一个进程在一个重要级的基础上运行,重要级低的进 ...

  7. 【转】CentOS 7.X 系统安装及优化

    [转]CentOS 7.X 系统安装及优化 centos的演变 启动流程sysvinit 串行启动:一次一个,一个一个启动 并行启动:全部的一起启动 init优点 运行非常良好.主要依赖于shell脚 ...

  8. mysql安装与卸载(阿里云)

    1.安装rpm包rpm -Uvh http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm 2.安装mysqlyum -y i ...

  9. tomcat jsp页面乱码解决

    浏览器接收服务器响应的中文参数: JSP页面中告诉浏览器使用什么编码: <%@ page language="java" contentType="text/htm ...

  10. python装饰器@用法

    这个是我见过比较好的讲解链接: [廖雪峰的官方网站 - 装饰器]