这篇blog算是自己记录一下,没什么很新的东西。共勉~~
因为我原来并没有研究过算法,所以一看到过河卒这个题目直接想都没想 ,深搜遍历被。
public class guohezu {
static int[][] grips;
static int[] back = new int[40];
static int cur_x = 1, cur_y = 1;
static Integer number = 0;
static Integer retricted_x = 0;
static Integer retricted_y = 0;
/**
* Braylon
* 20191124
* */
//@Test
public static void main(String args[])/*guohezu1124() */{
//获取数据
Scanner sc = new Scanner(System.in);
String input = sc.nextLine();
String array[] = input.split(" ");
int m = Integer.parseInt(array[0]) + 3;
int n = Integer.parseInt(array[1]) + 3;
int h_x = Integer.parseInt(array[2]) + 1;
int h_y = Integer.parseInt(array[3]) + 1;
grips = new int[m][n];
for (int i = 0; i < m ; i++) {
for (int j = 0; j < n ; j++) {
if (i == 0 || j == 0 || i == m -1 || j == n -1 ||
(i == h_x && j == h_y) ||
(i != h_x && j != h_y && ((Math.abs(i - h_x) + Math.abs(j - h_y)) == 3))) {
grips[i][j] = 1;
} else {
grips[i][j] = 0;
}
}
}
for (int i = 0; i < 40; i++) {
back[i] = -1;
}
//以上初始化结束
//遍历
while (cur_x != 0) {
if (grips[cur_x][cur_y + 1] == 0 && retricted_x != cur_x && retricted_y != cur_y) {
if (grips[cur_x + 1][cur_y] == 0) {
push(cur_x, cur_y);
walk(cur_x, cur_y + 1);//先走右边
} else {
walk(cur_x, cur_y + 1);
}
} else {
if (retricted_x == cur_x && retricted_y == cur_y) {
retricted_x = 0;
retricted_y = 0;
}
if (grips[cur_x + 1][cur_y] == 0) {
walk(cur_x + 1, cur_y);
} else {
gotosavepoint();
}
}
if (cur_y == n - 2 && cur_x == m - 2) {
number++;
gotosavepoint();
}
}
System.out.println(number);
}
public static int walk(int i, int j) {
//grips[i][j] = 1;
cur_x = i;
cur_y = j;
return 0;
}
public static void gotosavepoint() {
int get_x = 0;
int get_y = 0;
for (int i = 39; i >= 1; i=i-2) {
if (back[i] != -1) {
get_y = back[i];
get_x = back[i - 1];
back[i] = -1;
back[i - 1] = -1;
break;
}
}
retricted_y = get_y;
retricted_x = get_x;
cur_y = get_y;
cur_x = get_x;
}
public static int push(int x, int y) {
for (int i = 0; i <= 38; i = i + 2) {
if (back[i] == -1) {
back[i] = x;
back[i + 1] = y;
return 0;
}
}
return 1;
}
}
这样写肯定就基本上超时了,后来仔细研究,再加上参考一些资料和方法,确实dp才是王道。
思路很简单
大家可以参考这个文章看具体算法思路
点击跳转
这里我放一个java实现
/**
* Braylon
* 20191125
* */
public class guohezu_DP {
@Test
public void guohezu_dp() {
// public static void main(String args[]){
//声明全局变量
long[][] dp_map;//表示走到当前空格
int[][] restricted_matric;//1:禁止通过,0:正常
long number = 0;//最终数量
int[] h_arr_x = {1, 1, -1, -1, 2, 2, -2, -2};
int[] h_arr_y = {2, -2, 2, -2, -1, 1, 1, -1};
//获取输入
Scanner scanner = new Scanner(System.in);
int m = scanner.nextInt() + 1;//输入6但是有7挑路径
int n = scanner.nextInt() + 1;
int h_x = scanner.nextInt();
int h_y = scanner.nextInt();
// String input = scanner.nextLine();
// String input="20 3 19 2";
// String[] arr = input.split(" ");
// int m = Integer.parseInt(arr[0]) + 1;//输入6但是有7挑路径
// int n = Integer.parseInt(arr[1]) + 1;
// int h_x = Integer.parseInt(arr[2]);
// int h_y = Integer.parseInt(arr[3]);
//初始化全局变量
dp_map = new long[m][n];
restricted_matric = new int[m][n];
restricted_matric[h_x][h_y] = -1;
for (int i = 0; i < 8; i++) {
if (((h_x + h_arr_x[i]) < m) && ((h_x + h_arr_x[i]) >= 0) && ((h_y + h_arr_y[i]) >= 0) && ((h_y + h_arr_y[i]) < n)) {
restricted_matric[h_x + h_arr_x[i]][h_y + h_arr_y[i]] = -1;
}
}
for (int i = 0; i < n; i++) {
if (restricted_matric[0][i] == -1) {
dp_map[0][i] = 0;
continue;
}
dp_map[0][i] = 1;
}
for (int i = 0; i < m; i++) {
if (restricted_matric[i][0] == -1) {
dp_map[i][0] = 0;
continue;
}
dp_map[i][0] = 1;
}
for (int i = 1; i < m; i++) {
for (int j = 1; j < n; j++) {
if (restricted_matric[i][j] == -1) {
dp_map[i][j] = 0;
} else {
dp_map[i][j] = dp_map[i - 1][j] + dp_map[i][j - 1];
}
}
}
number = dp_map[m - 1][n - 1];
System.out.println(number);
}
}