zoukankan      html  css  js  c++  java
  • Java实现 计蒜客 拯救行动

    拯救行动

    公主被恶人抓走,被关押在牢房的某个地方。牢房用 N imes M (N, M le 200)N×M(N,M≤200) 的矩阵来表示。矩阵中的每项可以代表道路(@)、墙壁(#)、和守卫(x)。

    英勇的骑士(r)决定孤身一人去拯救公主(a)。我们假设拯救成功的表示是 “骑士到达了公主所在的位置”。由于在通往公主所在位置的道路中可能遇到守卫,骑士一旦遇到守卫,必须杀死守卫才能继续前进。

    现假设骑士可以向上、下、左、右四个方向移动,每移动一个位置需要 11 个单位时间,杀死一个守卫需要花费额外的 11 个单位时间。同时假设骑士足够强壮,有能力杀死所有的守卫。

    给定牢房矩阵,公主、骑士和守卫在矩阵中的位置,请你计算拯救行动成功需要花费最短时间。

    输入格式
    1、两个整数代表 NN 和 M, (N, M le 200)M,(N,M≤200).
    2、随后 NN 行,每行有 MM 个字符。"@" 代表道路,“a” 代表公主,“r” 代表骑士,“x” 代表守卫, “#” 代表墙壁。

    输出格式
    如果拯救行动成功,输出一个整数,表示行动的最短时间。
    如果不可能成功,输出 “Impossible”。

    输出时每行末尾的多余空格,不影响答案正确性

    样例输入1

    7 8
    #@#####@
    #@a#@@r@
    #@@#x@@@
    @@#@@#@#
    #@@@##@@
    @#@@@@@@
    @@@@@@@@
    

    样例输出1
    13
    样例输入2

    13 40
    @x@@##x@#x@x#xxxx##@#x@x@@#x#@#x#@@x@#@x
    xx###x@x#@@##xx@@@#@x@@#x@xxx@@#x@#x@@x@
    #@x#@x#x#@@##@@x#@xx#xxx@@x##@@@#@x@@x@x
    @##x@@@x#xx#@@#xxxx#@@x@x@#@x@@@x@#@#x@#
    @#xxxxx##@@x##x@xxx@@#x@x####@@@x#x##@#@
    #xxx#@#x##xxxx@@#xx@@@x@xxx#@#xxx@x#####
    #x@xxxx#@x@@@@##@x#xx#xxx@#xx#@#####x#@x
    xx##@#@x##x##x#@x#@a#xx@##@#@##xx@#@@x@x
    x#x#@x@#x#@##@xrx@x#xxxx@##x##xx#@#x@xx@
    #x@@#@###x##x@x#@@#@@x@x@@xx@@@@##@@x@@x
    x#xx@x###@xxx#@#x#@@###@#@##@x#@x@#@@#@@
    #@#x@x#x#x###@x@@xxx####x@x##@x####xx#@x
    #x#@x#x######@@#x@#xxxx#xx@@@#xx#x#####@
    

    样例输出2
    7

    PS:还差一个样例,还望大佬指点

     
    
     
    
    import java.util.LinkedList;
    import java.util.Scanner;
    
    public class Main {
    	 private static class Node {
    	         int x;
    	         int y;
    	         int step;
    
    	        public Node() {
    	        }
    
    	        public Node(int x, int y, int step) {
    	            this.x = x;
    	            this.y = y;
    	            this.step = step;
    	        }
    	    }
    
    	    private static int N = 220;
    	    private static LinkedList<Node> queue = new LinkedList<>();
    	    private static int r, c;
    	    private static char map[][] = new char[N][N];
    	    private static boolean vis[][] = new boolean[N][N];
    	    private static int dir[][] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
    
    	    private static void bfs(int sx, int sy, int ex, int ey) {
    	        boolean flag = true;
    
    	        Node n = new Node(sx, sy, 0);
    	        queue.add(n);
    	     //   vis[sx][sy] = true;
    	        while (!queue.isEmpty()) {
    	            Node nowP = queue.peek();
    //	            int dis = nowP.step;
    	            if (nowP.x == ex && nowP.y == ey) {
    	                flag = false;
    	                if(nowP.step==0) flag=true;
    	                else	System.out.println(nowP.step);
    	                break;
    	            } else {
    	                if (map[nowP.x][nowP.y] == 'x') {
    	                    map[nowP.x][nowP.y] = '@';
    	                   // vis[nowP.x][nowP.y] = false;
    	                    queue.add(new Node(nowP.x, nowP.y, nowP.step + 1));
    	                } else {
    	                    for (int i = 0; i < 4; i++) {
    	                        int nx = nowP.x + dir[i][0];
    	                        int ny = nowP.y + dir[i][1];
    	                        if (nx >= 0 && nx < r && ny >= 0 && ny < c && !vis[nx][ny] && map[nx][ny] != '#') {
    	                            vis[nx][ny] = true;
    	                            queue.add(new Node(nx,ny,nowP.step+1));
    	                        }
    	                    }
    	                }
    	            }
    	            queue.pop();
    	        }
    	        if (flag) {
    	            System.out.println("Impossible");
    	        }
    	    }
    
    	    public static void main(String[] args) {
    	        int sx = 0, sy = 0, ex = 0, ey = 0;
    	        boolean bool1 = false ,bool2 = false; 
    	        Scanner sc = new Scanner(System.in);
    	        r = sc.nextInt();
    	        c = sc.nextInt();
    
    	        //sc.nextLine();
    	        for (int i = 0; i < r; i++) {
    	            String s = sc.next();
    	            map[i] = s.toCharArray();
    	        }
    	        for (int i = 0; i < r; i++) {
    	            for (int j = 0; j < c; j++) {
    	                if (map[i][j] == 'r') {
    	                    sx = i;
    	                    sy = j;
    	                    bool1=true;
    	                }
    	                if (map[i][j] == 'a') {
    	                    ex = i;
    	                    ey = j;
    	                    bool2=true;
    	                 //   map[i][j] = '@';
    	                }
    	            }
    	        } 
    	        if(bool1 && bool2)
    	        bfs(sx, sy, ex, ey);
    	        else	System.out.println("Impossible");
    
    	    }
    
    
    }
    
    
  • 相关阅读:
    _ 下划线 Underscores __init__
    Page not found (404) 不被Django的exception中间件捕捉 中间件
    从装修儿童房时的门锁说起
    欧拉定理 费马小定理的推广
    线性运算 非线性运算
    Optimistic concurrency control 死锁 悲观锁 乐观锁 自旋锁
    Avoiding Full Table Scans
    批量的单向的ssh 认证
    批量的单向的ssh 认证
    Corrupted MAC on input at /usr/local/perl/lib/site_perl/5.22.1/x86_64-linux/Net/SSH/Perl/Packet.pm l
  • 原文地址:https://www.cnblogs.com/a1439775520/p/13075446.html
Copyright © 2011-2022 走看看