HDOJ 1045 Fire Net (JAVA)

参考连接:http://www.cnblogs.com/AdaByron/archive/2011/10/10/2205525.html

在网上看了很多二分图和匈牙利算法,但是最后还是用了DFS的方法来完成了。

奉上java代码:

 1 import java.io.BufferedInputStream;
 2 import java.util.Scanner;
 3 
 4 public class Main {
 5 
 6     static char maze[][];
 7     static int max, result, N;
 8 
 9     public static boolean placeable(int i, int j) {
10         if (maze[i][j] == 'X')
11             return false;
12         int k=j;
13         while (k >= 0 && maze[i][k] != 'X') {
14             if (maze[i][k--] == '*')
15                 return false;
16         }
17         k=i;
18         while (k >= 0 && maze[k][j] != 'X') {
19             if (maze[k--][j] == '*')
20                 return false;
21         }
22         return true;
23     }
24 
25     public static void DFS(int i, int j, int max) {
26         if ((i==N-1&&j==N)||(i==N&&j==N-1)) {
27             result = max > result ? max : result;
28             return;
29         } else if (i < N && j < N && placeable(i, j)) {
30             maze[i][j] = '*';
31             if (j == N - 1) {
32                 DFS(i + 1, 0, max + 1);
33             } else {
34                 DFS(i, j + 1, max + 1);
35             }
36             maze[i][j] = '.';
37         }
38         if (j == N - 1) {
39             DFS(i + 1, 0, max);
40         } else {
41             DFS(i, j + 1, max);
42         }
43     }
44 
45     public static void main(String[] args) {
46         // TODO Auto-generated method stub
47         Scanner cin = new Scanner(new BufferedInputStream(System.in));
48 
49         while (cin.hasNext()) {
50             N = cin.nextInt();
51             if (N == 0)  return;
52             max = 0;
53             result = 0;
54             maze = new char[N][N];
55             String str;
56             for (int i = 0; i < N; i++) {
57                 str = cin.next();
58                 for (int j = 0; j < N; j++) {
59                     maze[i][j] = str.charAt(j);
60                 }
61             }
62 
63             DFS(0, 0, 0);
64             System.out.println(result);
65 
66         }
67 
68     }
69 }

 

转载于:https://www.cnblogs.com/rorchach/archive/2013/01/23/2873733.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值