HDU 2128 Tempter of the Bone II BFS
状压整张图包括每个点的炸弹有没有被拿,墙壁有没有被炸。用优先队列存一下状态。
还有就是注意整数数溢出的问题。
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <vector>
#include <string>
#include <queue>
#include <deque>
#include <bitset>
#include <list>
#include <cstdlib>
#include <climits>
#include <cmath>
#include <ctime>
#include <algorithm>
#include <stack>
#include <sstream>
#include <numeric>
#include <fstream>
#include <functional>using namespace std;#define MP make_pair
#define PB push_back
typedef long long LL;
typedef unsigned long long ULL;
typedef vector<int> VI;
typedef pair<int,int> pii;
const int INF = INT_MAX / 3;
const double eps = 1e-8;
const LL LINF = 1e17;
const double DINF = 1e60;
const int dx[] = {0,0,1,-1};
const int dy[] = {1,-1,0,0};
const int maxn = 8;
char mp[maxn][maxn];
int n,m,sx,sy,ex,ey;struct Node {int x,y,dis,bomb;ULL maze,vis;Node(int x,int y,ULL maze,int dis,int bomb,ULL vis): x(x),y(y),maze(maze),dis(dis),bomb(bomb),vis(vis) {}bool operator < (const Node &p) const {if(dis != p.dis) return dis > p.dis;int e_dis1 = abs(x - ex) + abs(y - ey);int e_dis2 = abs(p.x - ex) + abs(p.y - ey);if(e_dis1 != e_dis2) return e_dis1 > e_dis2;if(bomb != p.bomb) return bomb < p.bomb;return maze > p.maze;}
};struct st_Node {int x,y,bomb,dis;ULL maze,vis;st_Node(Node &p) {x = p.x; y = p.y; bomb = p.bomb;maze = p.maze; vis = p.vis; dis = p.dis;}bool operator < (const st_Node &p) const {if(x != p.x) return x < p.x;if(y != p.y) return y < p.y;if(bomb != p.bomb) return bomb < p.bomb;if(maze != p.maze) return maze < p.maze;return vis < p.vis;}
};set<st_Node> st;inline int U(int x,int y) {return x * m + y;
}inline bool in_bd(int x,int y) {return x >= 0 && x < n && y >= 0 && y < m;
}inline ULL maze_comp() {ULL ret = 0;for(int i = 0;i < n;i++) {for(int j = 0;j < m;j++) {if(mp[i][j] == 'X') ret |= (1LL << U(i,j));}}return ret;
}inline bool insert_st(Node &tmp) {st_Node now(tmp);set<st_Node>::iterator ret = st.find(now);if(ret == st.end()) {st.insert(now); return true;}return false;
}void printmap(ULL maze,int x,int y) {for(int i = 0;i < n;i++) {for(int j = 0;j < m;j++) {if(i == x && j == y) printf("@");else if(maze & (1LL << U(i,j))) printf("X");else printf(".");}puts("");}
}void solve() {st.clear();priority_queue<Node> q;q.push(Node(sx,sy,maze_comp(),0,0,0));int ans = INF;while(!q.empty()) {Node now = q.top(); q.pop();int x = now.x, y = now.y, dis = now.dis, bomb = now.bomb;ULL maze = now.maze, vis = now.vis;if(x == ex && y == ey) {ans = dis;break;}for(int i = 0;i < 4;i++) {int nx = x + dx[i], ny = y + dy[i];if(!in_bd(nx,ny)) continue;if(maze & (1LL << U(nx,ny))) {//如果是墙if(bomb == 0) continue;int nbomb = bomb - 1;ULL nmaze = maze ^ (1LL << U(nx,ny));Node nnode = Node(nx,ny,nmaze,dis + 2,nbomb,vis);if(insert_st(nnode)) q.push(nnode);}else {//空地if (mp[nx][ny] >= '1' && mp[nx][ny] <= '9' && !(vis & (1LL << U(nx,ny)))) {//炸弹储备ULL nvis = vis | (1LL << U(nx,ny));int nbomb = bomb + mp[nx][ny] - '0';Node nnode = Node(nx,ny,maze,dis + 1,nbomb,nvis);if(insert_st(nnode)) {q.push(nnode);}}else {Node nnode = Node(nx,ny,maze,dis + 1,bomb,vis);if(insert_st(nnode)) q.push(nnode);}}}}printf("%d\n",ans == INF ? -1 : ans);
}int main() {while(scanf("%d%d",&n,&m), n) {for(int i = 0;i < n;i++) {for(int j = 0;j < m;j++) {scanf(" %c",&mp[i][j]);if(mp[i][j] == 'S') {sx = i; sy = j; mp[i][j] = '.';}if(mp[i][j] == 'D') {ex = i; ey = j; mp[i][j] = '.';}}}solve();}return 0;
}
转载于:https://www.cnblogs.com/rolight/p/3939669.html
HDU 2128 Tempter of the Bone II BFS相关推荐
- HDU.1010 Tempter of the Bone
文章目录 一.题目解读 1.原题 2.分类 3.题意 4.输入输出格式 5.数据范围 二.题解参考 1.总体思路 2.思路① (1).分析 (2).AC代码 三.评价与后话 1.评价 2.奇偶剪枝0- ...
- HDOJ 1010 HDU 1010 Tempter of the Bone ACM 1010 IN HDU
MiYu原创, 转帖请注明 : 转载自 ______________白白の屋 题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=1010 题目描述: 代码 ...
- 回溯法+奇偶剪枝——Hdu 1010 Tempter of the Bone
1) 题目 Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (J ...
- 输入空格hdu - 1010 - Tempter of the Bone
时间紧张,先记一笔,后续优化与完善. 题意:一个N*M的地图,走过的点不能再走,X为墙弗成走,能否从点S到点D恰好用时T.(1 < N, M < 7; 0 < T < 50) ...
- HDU 1010 Tempter of the Bone heuristic 修剪
的问题是,在测试修剪. 应该说是更先进的应用. 由于使用的heuristic(经验)修剪.总结这方面的经验法则,别easy.我说,这也是由于先进的在线报告中的应用程序没有分析太多太好的解决这个问题,计 ...
- HDU 1010 Tempter of the Bone DFS(奇偶剪枝优化)
需要剪枝否则会超时,然后就是基本的深搜了 #include<cstdio> #include<stdio.h> #include<cstdlib> #include ...
- (step4.3.1) hdu 1010(Tempter of the Bone——DFS)
题目大意:输入三个整数N,M,T.在接下来的N行.M列会有一系列的字符.其中S表示起点,D表示终点. .表示路 . X表示墙...问狗能有在T秒时到达D.如果能输出YES, 否则输出NO 解题思路:D ...
- HDU 1010 Tempter of the Bone heuristic 剪枝法
本题就是考剪枝法了. 应该说是比较高级的应用了.因为要使用heuristic(经验)剪枝法.要总结出这个经验规律来,不容易.我说这是高级的应用也因为网上太多解题报告都没有分析好这题,给出的程序也很慢, ...
- Tempter of the Bone(DFS + 奇偶剪枝,好题)
Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
最新文章
- (灌水)如何限制一个WinForm应用程序只能在一个进程运行
- 网络工程师技能图谱,这些你都会吗?
- java gui 读取文件夹_java Swing GUI 入门-文件读写器
- 从 DevOps 到 Serverless:通过“不用做”的方式解决“如何更高效做”的问题
- ora-01658 :无法为表空间USERS 中的段创建INITIAL区
- Windows环境下Android Studio系列5—日志调试
- 【bfs】极其简单的最短路问题
- Java之接口的静态方法的定义和使用
- Spring Boot笔记-发送消息给RabbitMQ
- SpringBoot—JPA: javax.persistence.TransactionRequiredException
- 线程池----ThreadPoolExecutor
- SSM(Spring+springMVC+MyBatis)框架-springMVC实现图片上传
- [HTTP权威指南]2.HTTP连接管理
- VUE下载文件并修改文件名
- GitHub服务中断24小时11分钟事故分析报告
- c++实现压缩解压 zip文件
- SpringBooot:Redis:根据两地经纬度计算距离
- 21计算机考研国家线,来了!21考研国家线公布!附详细解读!
- python 找出其中不含有重复字符的最长子串的长度
- 2021潞河中学内高班高考成绩查询,潞河中学2018小升初入学攻略(含入学途径、中高考成绩)...