杭电 1026 Ignatius and the Princess I BFS 搜索 JAVA
来源:互联网 发布:美国种族歧视华人 知乎 编辑:程序博客网 时间:2024/06/08 13:34
题意:n*m迷宫,求从(0,0)到(n-1,m-1)的最少时间。'X'是墙,'.'是空地,'1'-'9'表示有怪物,消灭之需要数字对应的时间。
package DFS;import java.util.PriorityQueue;import java.util.Scanner;import java.util.Stack;public class HD1026 { int n, m, mins; char[][] maze; int[][] vis; int[] dr = { - 1, 0, 1, 0 }; int[] dc = { 0, -1, 0, 1 }; Point[][] roads; int success; public static void main(String[] args) { new HD1026().run(); } public void run() { Scanner scanner = new Scanner(System. in ); while (scanner.hasNextInt()) { n = scanner.nextInt(); m = scanner.nextInt(); maze = new char[n][m]; vis = new int[n][m]; roads = new Point[n][m]; String line; scanner.nextLine(); for (int i = 0; i < n; i++) { line = scanner.nextLine(); for (int j = 0; j < m; j++) { maze[i][j] = line.charAt(j); vis[i][j] = 0; } } success = 0; bfs(); if (success == 1) { System.out.printf("It takes %d seconds to reach the target position, let me show you the way.\n", mins); print(); } else { System.out.printf("God please help our poor hero.\nFINISH\n"); } } scanner.close(); } public void bfs() { Point point = new Point(); PriorityQueue < Point > queue = new PriorityQueue <Point >(); queue.offer(point); vis[0][0] = 1; while (!queue.isEmpty()) { point = queue.poll(); if (point.x == n - 1 && point.y == m - 1) { mins = point.cnt; success = 1; return; } for (int d = 0; d < 4; d++) { int rtemp = point.x + dr[d]; int ctemp = point.y + dc[d]; if (rtemp >= 0 && rtemp < n && ctemp >= 0 && ctemp < m && vis[rtemp][ctemp] != 1 && maze[rtemp][ctemp] != 'X') { vis[rtemp][ctemp] = 1; roads[rtemp][ctemp] = new Point(point.x, point.y, point.cnt); if (maze[rtemp][ctemp] == '.') { queue.offer(new Point(rtemp, ctemp, point.cnt + 1)); } else { queue.offer(new Point(rtemp, ctemp, point.cnt + (maze[rtemp][ctemp] - '0' + 1))); } } } } } public void print() { Stack < Point > stack = new Stack <Point >(); Point temp = roads[n - 1][m - 1]; stack.push(new Point(n - 1, m - 1, mins)); while (temp.x != 0 || temp.y != 0) { stack.push(temp); temp = roads[temp.x][temp.y]; } int t = 1; while (!stack.empty()) { temp = stack.peek(); stack.pop(); if (maze[temp.x][temp.y] == '.') System.out.printf("%ds:(%d,%d)->(%d,%d)\n", t++, roads[temp.x][temp.y].x, roads[temp.x][temp.y].y, temp.x, temp.y); else { System.out.printf("%ds:(%d,%d)->(%d,%d)\n", t++, roads[temp.x][temp.y].x, roads[temp.x][temp.y].y, temp.x, temp.y); int k = maze[temp.x][temp.y] - '0'; while (k--!=0) System.out.printf("%ds:FIGHT AT (%d,%d)\n", t++, temp.x, temp.y); } } System.out.printf("FINISH\n"); } class Point implements Comparable < Point > { int x, y, cnt; public Point() { x = 0; y = 0; cnt = 0; } public Point(int x, int y, int cnt) { this.x = x; this.y = y; this.cnt = cnt; } public int compareTo(Point o) { return this.cnt > o.cnt ? 1 : -1; } }}
0 0
- 杭电 1026 Ignatius and the Princess I BFS 搜索 JAVA
- 【搜索之BFS + 优先队列】杭电 hdu 1026 Ignatius and the Princess I
- 杭电1026 Ignatius and the Princess I(BFS+路径记录)
- 杭电-1026Ignatius and the Princess I(BFS+记录路径)
- 杭电1026——Ignatius and the Princess I(BFS)
- HDU杭电1026 Ignatius and the Princess I(迷宫问题bfs)
- 杭电ACM OJ 1026 Ignatius and the Princess I DFS+BFS
- 杭电hdu 1026 Ignatius and the Princess I 广度优先搜索
- 杭电OJ 1026:Ignatius and the Princess I
- 杭电 hdu 1026 Ignatius and the Princess I(BFS+优先队列+墨迹人的输出)
- HDU1026:Ignatius and the Princess I(BFS)
- hdu1026 Ignatius and the Princess I BFS
- HDU1026:Ignatius and the Princess I(BFS)
- HDU1026:Ignatius and the Princess I(BFS)
- HDU1026 Ignatius and the Princess I(BFS)
- HDU 1026 Ignatius and the Princess I 广度优先搜索(BFS)
- HDU 1026 Ignatius and the Princess I (优先队列+BFS(广度优先搜索))
- hdu 1026 Ignatius and the Princess I(bfs搜索+输出路径)
- 获取当前系统时间,设置系统时间
- ListView
- OBIEE 12c Weblogic中部署自定义文件夹
- String/StringBuffer/StringBuilder
- 读程序员的怒吼
- 杭电 1026 Ignatius and the Princess I BFS 搜索 JAVA
- linux command
- java
- 使用System.arraycopy()实现数组之间的复制
- 接口
- TCP,IP,HTTP,SOCKET区别和联系
- C语言双向链表
- ubuntu系统怎么自适应虚拟机窗口界面大小
- ubutun 12.04开机