http://www.1point3acres.com/bbs/thread-145576-1-1.html
就是给一个矩阵的size: n row, m column可选路径是每个格子周围的8个格 但不可以重复,
求生成一个从(0,0) 走到(n-1, m-1)的random 路径....
public class Solution {
public static void main(String args[]){
//int a[] = {7,2,4};
Solution s = new Solution();
int [][] result = s.getRandomPath(10, 10);
for (int i = 0; i < result.length; i++){
for (int j = 0; j < result[0].length; j++){
System.out.print(result[i][j]+" ");
}
System.out.println();
}
}
int [][]result;
int m,n;
int[][] getRandomPath(int n, int m){
//check = new boolean[n][m];
result = new int[n][m]; //all 0
this.n = n;
this.m = m;
dfs(0,0,1);
return result;
}
boolean dfs(int x, int y, int level){
if (x < 0 || x >= n || y < 0 || y >= m || result[x][y] != 0){
return false;
}
if (x == n-1 && y == m-1){
result[x][y] = level;
return true;
}
result[x][y] = level;
boolean found = false;
boolean[] directions = new boolean[8];
Random randomGenerator = new Random();
int totalCheck = 0;
while (!found && totalCheck != 8){
int random = randomGenerator.nextInt(8);
if (directions[random]){ //skip checked direction
continue;
}
totalCheck++;
directions[random] = true;
if (random == 0){
found = dfs(x-1, y, level+1);
}else if (random == 1){
found = dfs(x+1, y, level+1);
}else if (random == 2){
found = dfs(x, y+1, level+1);
}else if (random == 3){
found = dfs(x, y-1, level+1);
}else if (random == 4){
found = dfs(x-1, y-1, level+1);
}else if (random == 5){
found = dfs(x+1, y+1, level+1);
}else if (random == 6){
found = dfs(x-1, y+1, level+1);
}else{
found = dfs(x+1, y-1, level+1);
}
}
if (found){
return true;
}
result[x][y] = 0;
return false;
}
}
public class Leetcode {
public static void main(String[] args){
int[][] matrix = new int[10][10];
List<Integer> result = new ArrayList<Integer>();
if(fill(result, matrix, 0, 0))
System.out.print(result);
}
static boolean fill(List<Integer> result, int[][] matrix, int i, int j){
int [] dx = {0,1,1,1,0,-1,-1,-1};
int [] dy = {1,1,0,-1,-1,-1,0,1};
if(i<0 || i>=matrix.length || j<0 || j>=matrix[0].length || matrix[i][j] != 0)
return false;
matrix[i][j] = 1;
result.add(matrix[0].length*i+j);
if(i == matrix.length-1 && j == matrix[0].length-1)
return true;
int [] directions = new int [8];
Random randomGenerator = new Random();
int count = 0;
while(count != 8){
int random = randomGenerator.nextInt(8);
if(directions[random] == 1)
continue;
else{
directions[random] = 1;
count++;
if(fill(result, matrix, i+dx[random], j+dy[random]))
return true;
}
}
result.remove(result.size()-1);
matrix[i][j] = 0;
return false;
}
}
就是给一个矩阵的size: n row, m column可选路径是每个格子周围的8个格 但不可以重复,
求生成一个从(0,0) 走到(n-1, m-1)的random 路径....
public class Solution {
public static void main(String args[]){
//int a[] = {7,2,4};
Solution s = new Solution();
int [][] result = s.getRandomPath(10, 10);
for (int i = 0; i < result.length; i++){
for (int j = 0; j < result[0].length; j++){
System.out.print(result[i][j]+" ");
}
System.out.println();
}
}
int [][]result;
int m,n;
int[][] getRandomPath(int n, int m){
//check = new boolean[n][m];
result = new int[n][m]; //all 0
this.n = n;
this.m = m;
dfs(0,0,1);
return result;
}
boolean dfs(int x, int y, int level){
if (x < 0 || x >= n || y < 0 || y >= m || result[x][y] != 0){
return false;
}
if (x == n-1 && y == m-1){
result[x][y] = level;
return true;
}
result[x][y] = level;
boolean found = false;
boolean[] directions = new boolean[8];
Random randomGenerator = new Random();
int totalCheck = 0;
while (!found && totalCheck != 8){
int random = randomGenerator.nextInt(8);
if (directions[random]){ //skip checked direction
continue;
}
totalCheck++;
directions[random] = true;
if (random == 0){
found = dfs(x-1, y, level+1);
}else if (random == 1){
found = dfs(x+1, y, level+1);
}else if (random == 2){
found = dfs(x, y+1, level+1);
}else if (random == 3){
found = dfs(x, y-1, level+1);
}else if (random == 4){
found = dfs(x-1, y-1, level+1);
}else if (random == 5){
found = dfs(x+1, y+1, level+1);
}else if (random == 6){
found = dfs(x-1, y+1, level+1);
}else{
found = dfs(x+1, y-1, level+1);
}
}
if (found){
return true;
}
result[x][y] = 0;
return false;
}
}
public class Leetcode {
public static void main(String[] args){
int[][] matrix = new int[10][10];
List<Integer> result = new ArrayList<Integer>();
if(fill(result, matrix, 0, 0))
System.out.print(result);
}
static boolean fill(List<Integer> result, int[][] matrix, int i, int j){
int [] dx = {0,1,1,1,0,-1,-1,-1};
int [] dy = {1,1,0,-1,-1,-1,0,1};
if(i<0 || i>=matrix.length || j<0 || j>=matrix[0].length || matrix[i][j] != 0)
return false;
matrix[i][j] = 1;
result.add(matrix[0].length*i+j);
if(i == matrix.length-1 && j == matrix[0].length-1)
return true;
int [] directions = new int [8];
Random randomGenerator = new Random();
int count = 0;
while(count != 8){
int random = randomGenerator.nextInt(8);
if(directions[random] == 1)
continue;
else{
directions[random] = 1;
count++;
if(fill(result, matrix, i+dx[random], j+dy[random]))
return true;
}
}
result.remove(result.size()-1);
matrix[i][j] = 0;
return false;
}
}