Nightmare HDU1072
非常标准的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的更多相关文章
- Nightmare(DFS)
Nightmare hdu1072 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Oth ...
- HDU-1072 Nightmare (bfs+贪心)
Nightmare Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total Sub ...
- hdu1072(Nightmare)bfs
Nightmare Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total ...
- 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 ...
- HDU1072:Nightmare [DFS]
题目链接:Nightmare 题意: 给出一张n*m的图,0代表墙,1代表可以走,2代表起始点,3代表终点,4代表炸弹重置点 问是否能从起点到达终点 分析: 一道很好的DFS题目,炸弹重置点必然最多走 ...
- HDU1072:Nightmare
传送门 题意 给出一张n*m的图 0.墙 1.可走之路 2.起始点 3.终点 4.时间重置点 问是否能到达终点 分析 我的训练专题第一题,一开始我设个vis数组记录,然后写炸,不能处理重置点根vis的 ...
- HDU 1072 Nightmare
Description Ignatius had a nightmare last night. He found himself in a labyrinth with a time bomb on ...
- Nightmare基于phantomjs的自动化测试套件
今天将介绍一款自动化测试套件名叫nightmare,他是一个基于phantomjs的测试框架,一个基于phantomjs之上为测试应用封装的一套high level API.其API以goto, re ...
- POJ 1984 Navigation Nightmare 带全并查集
Navigation Nightmare Description Farmer John's pastoral neighborhood has N farms (2 <= N <= ...
随机推荐
- mysql 案例~select引起的性能问题
案例1 背景:测试环境下发现大量select查询,而且负载飙升到90+ 排查思路: 1 老规则,按照排错脚本走一圈,规划出几个元素(1 针对库访问的统计 2针对具体语句类型的统计),发现有大量的sel ...
- 课程5:Spring框架2016版视频--视频列表目录
\day01视频\01-今天内容介绍.avi; \day01视频\02-spring的相关概念.avi; \day01视频\03-spring的ioc底层原理(一).avi; \day01视频\04- ...
- android studio 清空缓存插件
http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2016/0308/4036.html 一个提高开发效率的ADB插件:ADB IDEA 泡在 ...
- 【windows核心编程】双机调试操作
1.1 中断与异常 计算机最重要的任务就是执行指令,只要你设置一个起始位置,他就会一条指令的执行下去.而中断和异常机制是为了防止计算机无休止地执行任意指令,出现错误时可以引导处理器转向正常控制流而诞生 ...
- AT91RM9200---定时器简介
1.前言 系统定时器模块集成了3个不同的定时器 一个周期性间隔的定时器,用来为操作系统设置时基 一个看门狗定时器,可用于软件死锁时进行系统复位 一个实时时钟计数器用来记录流逝的时间 系统定时器时钟 这 ...
- UML和模式应用4:初始阶段(3)--需求制品之用例模型
1. 前言 UP开发包括四个阶段:初始阶段.细化阶段.构建阶段.移交阶段: UP每个阶段包括 业务建模.需求.设计等科目: 其中需求科目对应的需求制品包括:设想.业务规则.用例模型.补充性规格说明.词 ...
- word文档里打不开公式 打开后都是方框
因为系统缺少一种字体,只要到网络上下载或到其他计算机中复制一种文件名为“symbol.ttf”的字体文件来安装上,就可以了.参考资料:Office之家 http://www.officejia.com ...
- git操作之冲突解决
应用场景,任哥,我两个人共同修改了git项目上的一个文件.zsh命令行模式 准备工作 简写命令解释 gl=git pullgp=git pushgst=git statusgcmsg=git comm ...
- Expm 4_1 多段图中的最短路径问题
[问题描述] 建立一个从源点S到终点T的多段图,设计一个动态规划算法求出从S到T的最短路径值,并输出相应的最短路径. 解 package org.xiu68.exp.exp4; public cl ...
- Go语言规格说明书 之 接口类型(Interface types)
go version go1.11 windows/amd64 本文为阅读Go语言中文官网的规则说明书(https://golang.google.cn/ref/spec)而做的笔记,介绍Go语言的 ...