过河卒
一共有三份代码,
分别是只会写迷宫时的、会写dfs时的、动态规划的,洛谷上对应的分数是40,60,100
迷宫写法
这份代码只是自己用来练习bfs的,运行的话只有40,数据过大会超时,还不是一般的超时,当m=n=20的时候,运行了一分钟还没结果。
用二维数组表示棋盘,与题目的棋盘相比,加上了外围的边界,所以坐标会有变化,比如:
原来坐标:
改后坐标:
思路与迷宫完全相同
代码如下:
#include <stdio.h> #include <malloc.h> #include <math.h> #define MAXSIZE 30 typedef enum abc { RIGHT, DOWN } DIR; //只有两个方向可走 typedef struct { int x, y; DIR dir; }Pos; typedef struct { Pos data[500]; int top; }Stack; int matrix[MAXSIZE][MAXSIZE]; int main() { int n, m, xhouse, yhouse, path_num = 0; Pos top_pos; scanf("%d %d %d %d", &n, &m, &xhouse, &yhouse); //这里参考的是迷宫的做法,将外围全部为1,题目中没有外围,所以中间可走的部分坐标会有些变化 for (int i = 0; i <= n + 2; i++) { matrix[i][0] = 1; matrix[i][m + 2] = 1; } for (int j = 0; j <= m + 2; j++) { matrix[0][j] = 1; matrix[n + 2][j] = 1; } //把马和可能走到的位置标记为不能走 for (int i = 1; i <= n + 1; i++) for (int j = 1; j <= m + 1; j++) if ( abs(i - (xhouse + 1)) == 1 && abs(j - (yhouse + 1)) == 2 || abs(i - (xhouse + 1)) == 2 && abs(j - (yhouse + 1)) == 1 || i == xhouse + 1 && j == yhouse + 1 ) matrix[i][j] = 1; Stack path; path.top = -1; path.data[++path.top].x = 1; path.data[path.top].y = 1; path.data[path.top].dir = RIGHT; while (path.top > -1) { top_pos.x = path.data[path.top].x; top_pos.y = path.data[path.top].y; top_pos.dir = path.data[path.top].dir; if (path.data[path.top].x == n + 1 && path.data[path.top].y == m + 1) { path_num++; if (--path.top > -1) path.data[path.top].dir++; } else switch (top_pos.dir) { case RIGHT: if (matrix[top_pos.x + 1][top_pos.y] == 0) { path.data[++path.top].x = top_pos.x + 1; path.data[path.top].y = top_pos.y; path.data[path.top].dir = RIGHT; } else path.data[path.top].dir++; break; case DOWN: if (matrix[top_pos.x][top_pos.y + 1] == 0) { path.data[++path.top].x = top_pos.x; path.data[path.top].y = top_pos.y + 1; path.data[path.top].dir = RIGHT; } else path.data[path.top].dir++; break; default: path.data[--path.top].dir++; } } printf("%d", path_num); return 0; }
dfs
更新一下,还没有学会动态规划,倒是dfs用熟练了,开O2有60分了,看来dfs是一定超时了o(╥﹏╥)o
#include <stdio.h> #include <math.h> #define MAXSIZE 25 int matrix[MAXSIZE][MAXSIZE]; int xhorse, yhorse, xterminal, yterminal; long long sum; void dfs(int xstart,int ystart) { if (xstart == xterminal && ystart == yterminal) { sum++; return; } if (matrix[xstart + 1][ystart] == 1) dfs(xstart + 1, ystart); if (matrix[xstart][ystart + 1] == 1) dfs(xstart, ystart + 1); } int main() { scanf("%d", &xterminal); scanf("%d", &yterminal); scanf("%d", &xhorse); scanf("%d", &yhorse); for (int i = 0; i <= xterminal; i++)//0表示非棋盘位置,1表示可以走的位置,2表示马的位置,不能走 for (int j = 0; j <= yterminal; j++) if (abs(i - xhorse) == 1 && abs(j - yhorse) == 2 || abs(i - xhorse) == 2 && abs(j - yhorse) == 1|| i == xhorse && j == yhorse) { matrix[i][j] = 2; } else matrix[i][j] = 1; dfs(0, 0); printf("%lld", sum); return 0; }
动态规划
最终还是放弃了dfs,改用动态规划了,100分过了,但还是对dfs不死心啊
#include <stdio.h> #include <math.h> #define MAXSIZE 25 long long matrix[MAXSIZE][MAXSIZE]; int xhorse, yhorse, xterminal, yterminal; int main() { scanf("%d", &xterminal); scanf("%d", &yterminal); scanf("%d", &xhorse); scanf("%d", &yhorse); for (int i = xterminal; i >= 0; i--) for (int j = yterminal; j >= 0; j--) { if (i == xterminal && j == yterminal) matrix[i][j] = 1; else if (abs(i - xhorse) == 1 && abs(j - yhorse) == 2 || abs(i - xhorse) == 2 && abs(j - yhorse) == 1 || i == xhorse && j == yhorse) matrix[i][j] = 0; else matrix[i][j] += matrix[i][j + 1] + matrix[i + 1][j]; } printf("%lld", matrix[0][0]); return 0; }