航电1728 逃离迷宫 题解

 1 package DFS;
 2 //http://acm.hdu.edu.cn/showproblem.php?pid=1728
 3 import java.util.Scanner;
 4 
 5 class Tao_li_mi_gong {
 6     static int x1, y1, x2, y2;
 7     static int x_n, y_n;
 8     static int K; // 追多能转的弯数
 9     static boolean[][] dp;
10     static boolean thisdp[];
11     static int[][] a;
12     static int ii = 0;
13     static int ol[];
14     static boolean flag;
15     static int next[][] = { { -1, 0 }, { 0, -1 }, { 1, 0 }, { 0, 1 } };//方向数组 
16 
17     //DFS
18     static void dfs(int x1, int y1, int s) {
19         int this_x, this_y;
20         if (x1 == x2 && y1 == y2 && s <= K) {
21             flag = true;
22         }
23 
24         for (int i = 0; i <= 3; i++) {
25             this_x = x1 + next[i][0];
26             this_y = y1 + next[i][1];
27 
28             if (this_x < 1 || this_y < 1 || this_x > x_n || this_y > y_n || s > K) {
29                 continue;
30             }
31             if (a[this_x][this_y] == 0 && dp[this_x][this_y]) {
32                 dp[this_x][this_y] = false;
33                 dfs(this_x, this_y, s + 1);
34                 dp[this_x][this_y] = true;
35             }
36         }
37     }
38 
39     public static void main(String[] args) {
40         Scanner in = new Scanner(System.in);
41         int con = in.nextInt();
42         while (con-- > 0) {
43             x_n = in.nextInt();
44             y_n = in.nextInt();
45             a = new int[x_n + 1][y_n + 1];
46             dp = new boolean[x_n + 1][y_n + 1];
47 
48             for (int i = 1; i <= x_n; i++)
49                 for (int j = 1; j <= x_n; j++) {
50                     dp[i][j] = true;
51                 }
52 
53             dp[1][1] = false;
54 
55             for (int i = 1; i <= x_n; i++) {
56                 for (int j = 1; j <= y_n; j++) {
57                     if (in.next().charAt(0) == '.')
58                         a[i][j] = 0;
59                     else
60                         a[i][j] = 1;
61                 }
62             }
63             K = in.nextInt();
64             x1 = in.nextInt();
65             y1 = in.nextInt();
66             x2 = in.nextInt();
67             y2 = in.nextInt();
68             flag = false;
69             dfs(x1, y1, -1);
70             if (flag) {
71                 System.out.println("yes");
72             } else {
73                 System.out.println("no");
74             }
75         }
76     }
77 }

猜你喜欢

转载自www.cnblogs.com/naiyoucong/p/9475735.html
今日推荐