第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;
}
相关推荐
Y4090011 小时前
C语言转Java语言,相同与相异之处
java·c语言·开发语言·笔记
古月-一个C++方向的小白6 小时前
C++11之lambda表达式与包装器
开发语言·c++
tanyongxi668 小时前
C++ AVL树实现详解:平衡二叉搜索树的原理与代码实现
开发语言·c++
棐木8 小时前
【C语言】动态内存管理
c语言·free·malloc·realloc·calloc·动态内存
斯是 陋室10 小时前
在CentOS7.9服务器上安装.NET 8.0 SDK
运维·服务器·开发语言·c++·c#·云计算·.net
tju新生代魔迷11 小时前
C++:list
开发语言·c++
HHRL-yx11 小时前
C++网络编程 5.TCP套接字(socket)通信进阶-基于多线程的TCP多客户端通信
网络·c++·tcp/ip
tomato0911 小时前
河南萌新联赛2025第(一)场:河南工业大学(补题)
c++·算法
屁股割了还要学13 小时前
【C语言进阶】内存函数
c语言·开发语言·学习·算法·青少年编程
耳总是一颗苹果13 小时前
C语言---自定义类型(上)(结构体类型)
c语言·开发语言