HDOJ 1045 Fire Net (JAVA)

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 }

以上是 HDOJ 1045 Fire Net (JAVA) 的全部内容, 来源链接: utcz.com/z/392021.html

回到顶部