第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;
}
相关推荐
薛慕昭15 小时前
嵌入式 C 语言猜大小游戏设计与实现
c语言·游戏
散峰而望15 小时前
C++数组(二)(算法竞赛)
开发语言·c++·算法·github
利刃大大16 小时前
【动态规划:背包问题】完全平方数
c++·算法·动态规划·背包问题·完全背包
月光技术杂谈16 小时前
实战:C驱动框架嵌入Rust模块的互操作机制与完整流程
c语言·开发语言·rust·ffi·跨语言·bindgen·互操作
笑非不退16 小时前
C# c++ 实现程序开机自启动
开发语言·c++·c#
合作小小程序员小小店17 小时前
游戏开发,桌面%小游戏,贪吃蛇%demo,基于vs2022,c语言,easyX,无数据库
c语言·开发语言
AA陈超17 小时前
从0开始学习 **Lyra Starter Game** 项目
c++·笔记·学习·游戏·ue5·lyra
q***T58317 小时前
C++在游戏中的Unreal Engine
c++·游戏·虚幻
保持低旋律节奏17 小时前
C++——C++11特性
开发语言·c++·windows
vir0218 小时前
P12155 [蓝桥杯 2025 省 Java B] 消失的蓝宝
java·职场和发展·蓝桥杯