非常标准的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. MySQL - 日常操作二 备份还原

    登录mysql的命令 # 格式: mysql -h 主机地址 -u 用户名 -p 用户密码 mysql -h 110. -P3306 -uroot -p mysql -uroot -p -S /dat ...

  2. Python数据分析学习目录

    python数据分析学习目录 Anaconda的安装和更新 矩阵NumPy pandas数据表 matplotlib-2D绘图库学习目录                      

  3. HNOI2018游记

    第一次参加本省省选,结果又是一次划水 Day 0 喝了一个小时鸡汤 大家看看人家钱学森(sheng) 竞赛生要多发展些爱好 不要一考完就fake,那种下考说"大佬AC辣!太强啦!月莫月莫月莫 ...

  4. mysql 查询优化~ 分页优化讲解

    一 简介:今天咱们来聊聊mysql的分页查询二 语法     LIMIT [offset,] rows     offset是第多少条     rows代表多少条之后的行数    性能消耗    se ...

  5. 解决Ubuntu的root账号无法登录SSH问题-Permission denied, please try again.

    http://www.cnblogs.com/yixius/articles/6971054.html

  6. javascript随笔和常见的知识点

    1.js中循环中用 return只能停止循环,不能停止到函数的定义部分.所以下面的返回值为1 return 100没有意义,只起到终止循环的目的 function bb() { var sum = 0 ...

  7. shell-检测服务是否运行,并记日志

    目的:每隔*分钟检测服务是否运行:若运行中,则记录执行的进程名称:若不运行,记录当前时间 shell: #!/bin/bash date=`date +%Y%m%d` log=/home/mono_$ ...

  8. 优秀的gdb图形化前端调试器

    目前我自己最喜欢的还是 ddd . gdbgui 和 vim-vebugger插件或vimgdb插件 三种. You could try using Insight a graphical front ...

  9. ES系列四、ES6.3常用api之文档类api

    1.Index API: 创建并建立索引 PUT twitter/tweet/ { "user" : "kimchy", "post_date&quo ...

  10. C# 汉字与区位码之间的相互转换(中文数字字母可以,支持空格,但是特殊字符未来得及测试)

    using System; using System.Text; namespace Test { class MainClass { /// <summary> /// 中文空白字符,用 ...