lanqiao 19712 数字接龙

This commit is contained in:
2025-04-11 09:28:12 +08:00
parent 35658f092a
commit 01e55f3cf7

View File

@@ -6,8 +6,8 @@ int a[N][N]; // 迷宫数组
int vis[N][N]; // 标记数组 int vis[N][N]; // 标记数组
int n, k; int n, k;
// 方向数组 0 1 2 3 4 5 6 7 // 方向数组 0 1 2 3 4 5 6 7
int dx[] = {-1, -1, 0, 1, 1, 1, 0, -1}; int dx[] = {-1, -1, 0, 1, 1, 1, 0, -1};
int dy[] = { 0, 1, 1, 1, 0, -1, -1, -1}; int dy[] = { 0, 1, 1, 1, 0, -1, -1, -1};
string res; // 结果字符串 string res; // 结果字符串
// x, y为搜索的位置坐标, pre 前一个访问位置的数字, s 当前路径的访问序列, dep 搜索的深度 // x, y为搜索的位置坐标, pre 前一个访问位置的数字, s 当前路径的访问序列, dep 搜索的深度
@@ -29,6 +29,7 @@ void dfs(int x, int y, int pre, string s, int dep){
if((a[bx][by] < k && a[bx][by] == pre + 1) || (pre + 1 == k && a[bx][by] == 0)){ if((a[bx][by] < k && a[bx][by] == pre + 1) || (pre + 1 == k && a[bx][by] == 0)){
vis[bx][by] = 1; vis[bx][by] = 1;
dfs(bx, by, a[bx][by], s + to_string(i), dep + 1); dfs(bx, by, a[bx][by], s + to_string(i), dep + 1);
// 最优性剪枝 // 最优性剪枝
if(!res.empty()) return; if(!res.empty()) return;
vis[bx][by] = 0; //回溯 vis[bx][by] = 0; //回溯
@@ -51,7 +52,7 @@ int main() {
return 0; return 0;
} }
/* test samples /* test samples -> 41255214
3 3 3 3
0 2 0 0 2 0
1 1 1 1 1 1