第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;
}
相关推荐
2301_781143564 分钟前
C语言笔记(四)
c语言·笔记·算法
C羊驼11 分钟前
C语言学习笔记(十四):编译与链接
c语言·开发语言·经验分享·笔记·学习
Irissgwe20 分钟前
线程概念与控制
linux·开发语言·c++·线程
ShineWinsu25 分钟前
对于Linux:git版本控制器和cgdb调试器的解析
linux·c语言·git·gitee·github·调试·cgdb
m0_7167652341 分钟前
C++提高编程--STL初识、string容器详解
java·开发语言·c++·经验分享·学习·青少年编程·visual studio
楼田莉子44 分钟前
高并发内存池项目:内存池性能分析及其优化
开发语言·c++·后端·学习
wapicn991 小时前
智能识别技术在生活服务领域的落地应用与前景展望
java·c++·人工智能·python·php
是翔仔呐1 小时前
第6章 UART串口通信!掌握单片机与外界的双向数据通道,实现跨设备交互
c语言·开发语言·单片机·嵌入式硬件·gitee
2201_758642641 小时前
自定义内存检测工具
开发语言·c++·算法
fpcc1 小时前
C++编程实践—操作系统调优和内核旁支
开发语言·c++