第15届蓝桥杯省赛B组c/c++ 0数字接龙

cpp 复制代码
#include<vector>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<string>
using namespace std;
bool st[15][15];
int n, k;
int g[15][15];
int t[15][15][15][15];
int dx[] = { -1,-1,0,1,1,1,0,-1 };
int dy[] = { 0,1,1,1,0,-1,-1,-1 };
void dfs(int x, int y, vector<int>temp, int num){
    if (x == n && y == n && temp.size() == n * n - 1){
        for (auto x : temp){
            cout << x;
        }
        exit(0);
    }
    for (int i = 0; i < 8; i++){
        int xx = x + dx[i];
        int yy = y + dy[i];
        if (xx <= 0 || yy <= 0 || xx > n || yy > n || st[xx][yy]) continue;
        if(i%2==1)
        {
          if(t[xx][y][x][yy]||t[x][yy][xx][y])
          continue;
        }
        if ((num + 1) % k == g[xx][yy])
        {
            t[x][y][xx][yy]=1;
            st[xx][yy] = 1;
            temp.push_back(i);
            dfs(xx, yy, temp, (num + 1)%k);
            t[x][y][xx][yy]=0;
            st[xx][yy] = 0;
            temp.pop_back();
        }

    }

}
int main()
{
    cin >> n >> k;
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= n; j++)
        {
            cin >> g[i][j];
        }
    }
    vector<int>ans;
    st[1][1] = true;
    dfs(1, 1, ans, 0);//坐标,答案,数
    cout << -1 << '\n';
    return 0;
}
相关推荐
hPw0eKIqD4 小时前
C++ 模板参数推导问题小记(非推导上下文)
开发语言·c++
程序员爱德华4 小时前
Python与C++:异同点对比
c++·python
Nebula嵌入式5 小时前
【C语言】09-深入解析main函数
linux·c语言·开发语言·嵌入式
天空'之城7 小时前
C 语言工业级通用组件手写 23:卡尔曼滤波(简易版)
c语言·卡尔曼滤波·嵌入式算法·工业级组件
888CC++7 小时前
C语言与C++的区别:从面向过程到面向对象
java·c语言·c++
Darkwanderor8 小时前
Linux系统编程实战项目:模拟实现shell
linux·c++
himobrinehacken9 小时前
揭秘Windows程序启动的神秘之旅
c++·安全
库玛西9 小时前
C++ 运行时多态 :核心总结与原理图解
c语言·c++·笔记
Byron Loong10 小时前
【C++】重定向是什么
开发语言·c++
hansang_IR11 小时前
【题解】LC:Z 算法(Z Algorithm)
c++·算法·字符串