-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution_130.java
More file actions
90 lines (82 loc) · 1.89 KB
/
Solution_130.java
File metadata and controls
90 lines (82 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package com.hilbert25.leetcode;
import java.util.ArrayList;
import java.util.List;
/**
* @author : hilbert25
* @version 创建时间:2017年5月19日 上午12:16:53 LeetCode com.hilbert25.leetcode
* Solution_130
*/
public class Solution_130 {
public static void main(String[] args) {
// TODO Auto-generated method stub
}
/**
*
* @ClassName: Loc
*
* @Description: TODO
*
* @author: hilbert25
*
* @date: 2017年5月19日 上午12:39:48
*
*
*/
public class Loc {
int x;
int y;
public Loc(int x, int y) {
super();
this.x = x;
this.y = y;
}
}
/**
* @param board
*/
public void solve(char[][] board) {
for (int i = 1; i < board.length - 1; i++) {
for (int j = 1; j < board[0].length - 1; j++) {
if (board[i][j] == 'O') {
List<Loc> locationList = new ArrayList<>();
boolean[][] visited = new boolean[board.length][board[0].length];
if (dfs(locationList, board, visited, i, j, true)) {
for (Loc loc : locationList) {
board[loc.x][loc.y] = 'X';
}
}
}
}
}
}
/**
* @param locationList
* @param board
* @param x
* @param y
* @return
*/
public boolean dfs(List<Loc> locationList, char[][] board, boolean[][] visited, int x, int y, boolean legal) {
if (visited[x][y] == true || board[x][y] == 'X')
return true;
if (x == 0 || y == 0 || x == board.length - 1 || y == board[0].length - 1) {
legal = false;
} else {
locationList.add(new Loc(x, y));
}
visited[x][y] = true;
if (x > 0) {// up
legal &= dfs(locationList, board, visited, x - 1, y, legal);
}
if (x < board.length - 1) {// down
legal &= dfs(locationList, board, visited, x + 1, y, legal);
}
if (y > 0) {// left
legal &= dfs(locationList, board, visited, x, y - 1, legal);
}
if (y < board[0].length - 1) {// right
legal &= dfs(locationList, board, visited, x, y + 1, legal);
}
return legal;
}
}