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 <= ...
随机推荐
- 堆叠窗口QStackedWidget
经常将QStackedWidget和QListWidget或者QListView搭配使用 import sys from PyQt5.QtWidgets import QApplication, QW ...
- 为小程序开发创建本地mock数据服务器
开发时使用easy-mock的服务,不是大厂就不是大厂,实在恶心,每天都会有卡的这么一段时间 于是,自己建个本地mock服务算了,想使用express 但是必须把json数据里面的不同对象,分配到不同 ...
- try 、catch 、finally 、throw 测试js错误
try语句允许我们定义在执行时进行错误测试的代码块. catch 语句允许我们定义当 try 代码块发生错误时,所执行的代码块. finally 语句在 try 和 catch 之后无论有无异常都会执 ...
- Struts的JSON机制
需要加入jar包 Struts的JSON帮助我们自动将对象解析为JSON对象,不用我门借助第三方进行JSON的解析 .具体的使用机制如下: 1.Action类 package StrutsJSON; ...
- SpringMVC使用Redis作为缓存提供者
(1)pom添加依赖项 <dependency> <groupId>org.springframework.data</groupId> <artifactI ...
- 编码器AE & VAE
学习总结于国立台湾大学 :李宏毅老师 自编码器 AE (Auto-encoder) & 变分自动编码器VAE(Variational Auto-encoder) ...
- python日志和异常
“日志”转载:http://www.cnblogs.com/dkblog/archive/2011/08/26/2155018.html "异常"转载:http://www.cnb ...
- vim常用命令总结 (转)【转】
转自:https://www.cnblogs.com/yangjig/p/6014198.html 在命令状态下对当前行用== (连按=两次), 或对多行用n==(n是自然数)表示自动缩进从当前行起的 ...
- 注解图Annotation
该图来源于 竹子-博客(.NET/Java/Linux/架构/管理/敏捷) http://www.cnblogs.com/peida/archive/2013/04/26/3038503.html,感 ...
- 通过使用CSS字体阴影效果解决hover图片时显示文字看不清的问题
1.前言 最近需要加入一个小功能,在鼠标越过图片时,提示其大小和分辨率,而不想用增加属性title来提醒,不够好看.然而发现如果文字是一种颜色,然后总有概率碰到那张图上浮一层的文字会看不到,所以加入文 ...