hdu1072 记忆化bfs(java)
来源:互联网 发布:航天科工31所待遇知乎 编辑:程序博客网 时间:2024/06/05 14:51
题目链接:打开链接
题目大意:
在n×m的地图上,0表示墙,1表示空地,2表示人,3表示目的地,
4表示有定时炸弹重启器。定时炸弹的时间是6,人走一步所需要的时间是1。每次可以上、下、左、右移动一格。当人走到4时如果炸弹的时间不是0,
可以重新设定炸弹的时间为6。如果人走到3而炸弹的时间不为0时,成功走出。求人从2走到3的最短时间
解题思路:不要被炸弹重置误导了,第一次搜索到炸弹时 吧当前时间重置过后 吧到这个炸弹时间标记为已经访问过。经过可以重复走 但是相同时间走到炸弹位置是没有任何意义的。
java代码:
import java.util.LinkedList; import java.util.Queue; import java.util.Scanner;public class hdu1072{ /** * @param args * 在n×m的地图上,0表示墙,1表示空地,2表示人,3表示目的地, * 4表示有定时炸弹重启器。定时炸弹的时间是6,人走一步所需要的时间是1。每次可以上、下、左、右移动一格。当人走到4时如果炸弹的时间不是0, * 可以重新设定炸弹的时间为6。如果人走到3而炸弹的时间不为0时,成功走出。求人从2走到3的最短时间 */ static int dx[] = {-1,0,1,0 }; static int dy[] = { 0,-1,0,1 }; static int n,m,f_x,f_y; static int[][] map=new int [9][9]; static int vis[][] =new int[9][9]; class Node{ int x; int y; int to_time; int now_time; public Node(){ } public Node(int x,int y,int to_time,int now_time){ this.x=x; this.y=y; this.to_time=to_time; this.now_time=now_time; } } public static void main(String[] args) { // TODO Auto-generated method stub Scanner scan=new Scanner(System.in); int t; int s_x = 0,s_y=0; t=scan.nextInt(); while(t>0){ for(int i=0;i<9;i++){ for(int j=0;j<9;j++){ vis[i][j]=0; } } n=scan.nextInt(); m=scan.nextInt(); for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ map[i][j]=scan.nextInt(); if(map[i][j]==2){ s_x=i; s_y=j; } if(map[i][j]==3){ f_x=i; f_y=j; } } } System.out.println(bfs(s_x,s_y)); t--; } } private static int bfs(int x, int y) { // TODO Auto-generated method stub Queue<Node> queue=new LinkedList<Node>(); hdu1072 hdu=new hdu1072(); Node node1=hdu.new Node(); vis[x][y]=6; node1.x=x; node1.y=y; node1.to_time=0; node1.now_time=6; queue.offer(node1); while(queue.size()!=0){ Node b=queue.poll(); if(b.x==f_x&&b.y==f_y){ return b.to_time; } for(int i=0;i<4;i++){ int ssx=b.x+dx[i]; int ssy=b.y+dy[i]; if(judge(ssx,ssy)){ if(map[ssx][ssy]==4){ if(b.now_time==1) continue; int to_time=b.to_time+1; int nowtime=6; if(nowtime==0||nowtime<=vis[ssx][ssy]){ continue; } vis[ssx][ssy]=nowtime; Node c=hdu.new Node(ssx,ssy,to_time,nowtime); queue.offer(c); }else{ int to_time=b.to_time+1; int nowtime=b.now_time-1; if(nowtime==0||nowtime<=vis[ssx][ssy]){ continue; } vis[ssx][ssy]=nowtime; Node c=hdu.new Node(ssx,ssy,to_time,nowtime); queue.offer(c); } } } } return -1; } private static boolean judge(int x, int y) { // TODO Auto-generated method stub if(x<0||y<0||x>=n||y>=m){ return false; } if(map[x][y]==0){ return false; } return true; }}
阅读全文
0 0
- hdu1072 记忆化bfs(java)
- hdu1072 Nightmare bfs(java版本)
- hdu1072 BFS
- HDU1072(BFS)
- hdu1072(bfs)
- hdu1072 bfs
- HDU1072 BFS
- Acm hdu1072 BFS
- hdu1072 Nightmare (BFS)
- hdu1072 Nightmare (BFS)
- hdu1072 BFS初体验
- hdu1072(bfs+优先级队列)
- HDU1072 Nightmare 【BFS】
- hdu1072 Nightmare(BFS)
- hdu1072 Nightmare(bfs)
- hdu1072 Nightmare--DFS/BFS
- hdu1072 Nightmare(bfs)
- HDU1072 Nightmare(BFS)
- 守护者观察|色情诱导诈骗:20秒小电影撑起6亿黑产
- 工信部:明日起手机预装软件必须可卸载
- 24.2亿欧元!欧盟对谷歌处以“创纪录”的反垄断罚款
- 专访科大讯飞执行总裁胡郁:关于硬件合作方选择、砍掉伪需求AI产品线,我想谈几点
- 简单学习 JDBC的DAO模式
- hdu1072 记忆化bfs(java)
- 判断质数的一种高效方法——基于裁剪策略
- VirtualBox实现虚拟机与主机之间的文件拖放
- Amazon Prime会员日首登中国 如此能打开国内电商缺口吗?
- Qt: C++中的单例
- 福布斯:人工智能下一代算法研发是当务之急
- 【首发】Petya勒索病毒爆发,腾讯安全反病毒实验室首发技术分析
- 算术表达式计算(栈实现)
- qnx学习笔记-QNX系统下载graphic镜-卡在Load QNX image from SDMMC...卡住不动