杭电 1026 Ignatius and the Princess I BFS 搜索 JAVA

题意: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 ();
        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 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;        }    }}
     
            


你可能感兴趣的:(BFS,杭电)