第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;
}
相关推荐
霍霍的袁1 小时前
【初识C语言】预处理笔记(预处理指令、宏定义等)
c语言·笔记·visualstudio
yuanyuan2o22 小时前
从最小项目开始的 CMake 教程
c语言·开发语言·arm开发·c++·makefile·make·cmake
xh didida3 小时前
算法 -- 位运算
开发语言·c++·算法
hele_two3 小时前
VS Code + CMake 调用 SDL2 & SDL2_image 完整编译教程(Windows 平台)
c++·windows·vscode·图形渲染
谙弆悕博士3 小时前
快速学C语言——第2章:编程规范与代码风格
服务器·c语言·开发语言·经验分享·程序人生·学习方法·业界资讯
量子炒饭大师4 小时前
【优化算法】双指针算法的「义体化」重构 ——【双指针】双指针算法中的指针是如何定义的?如何使用它进行一些简单的算法?
c++·算法·重构·优化算法·双指针
小辉同志4 小时前
62. 不同路径
c++·力扣·多维动态规划
arron88995 小时前
DebugView未打印日志原因
c++
hele_two5 小时前
SDL2高效画实心圆的算法(一)
c++·算法·图形渲染
炸膛坦客6 小时前
嵌入式 - 数据结构与算法:(1-9)数据结构 - 队列(Queue)
c语言·数据结构