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

13 篇文章 0 订阅

题意: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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值