auto commit

This commit is contained in:
CyC2018 2018-02-27 16:04:39 +08:00
parent 599d26aa99
commit 5fa2a9f71a

View File

@ -429,33 +429,39 @@ public int minNumberInRotateArray(int[] array) {
```java
private int[][] next = {{0, -1}, {0, 1}, {-1, 0}, {1, 0}};
public boolean hasPath(char[] matrix, int rows, int cols, char[] str){
if(rows == 0 || cols == 0) return false;
char[][] m = new char[rows][cols];
for(int i = 0, idx = 0; i < rows; i++) {
for(int j = 0; j < cols; j++) {
m[i][j] = matrix[idx++];
private int rows;
private int cols;
public boolean hasPath(char[] matrix, int rows, int cols, char[] str) {
if (rows == 0 || cols == 0) return false;
this.rows = rows;
this.cols = cols;
// һάÊý×éÖؽ¨¶þά¾ØÕó
char[][] newMatrix = new char[rows][cols];
for (int i = 0, idx = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
newMatrix[i][j] = matrix[idx++];
}
}
for(int i = 0; i < rows; i++) {
for(int j = 0; j < cols; j ++) {
if(backtracking(m, rows, cols, str, new boolean[rows][cols], 0, i, j)) return true;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (backtracking(newMatrix, str, new boolean[rows][cols], 0, i, j)) return true;
}
}
return false;
}
private boolean backtracking(char[][] m, int rows, int cols, char[] str, boolean[][] used, int path, int r, int c) {
if(path == str.length) return true;
if(r < 0 || r >= rows || c < 0 || c >= cols) return false;
if(m[r][c] != str[path]) return false;
if(used[r][c]) return false;
used[r][c] = true;
for(int i = 0; i < next.length; i++) {
if(backtracking(m, rows, cols, str, used, path + 1, r + next[i][0], c + next[i][1])) return true;
private boolean backtracking(char[][] matrix, char[] str, boolean[][] used, int pathLen, int curR, int curC) {
if (pathLen == str.length) return true;
if (curR < 0 || curR >= rows || curC < 0 || curC >= cols) return false;
if (matrix[curR][curC] != str[pathLen]) return false;
if (used[curR][curC]) return false;
used[curR][curC] = true;
for (int i = 0; i < next.length; i++) {
if (backtracking(matrix, str, used, pathLen + 1, curR + next[i][0], curC + next[i][1]))
return true;
}
used[r][c] = false;
used[curR][curC] = false;
return false;
}
```