第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;
}
相关推荐
Lehjy31 分钟前
【C++】继承
c++·学习
Trouvaille ~1 小时前
【C++篇】继承之巅:超越法则束缚,领略面向对象的至臻智慧
开发语言·c++·面试·stl·代码规范·菱形继承·虚拟继承
baiyu331 小时前
C++语言学习(10):《C++程序设计原理与实践》第五章笔记
c++
ya888g1 小时前
蓝桥等级考试C++组18级真题-2023-06-18
c++·蓝桥杯·蓝桥等考
921正在学习编程2 小时前
模拟实现strstr 找到母串中是否有与子串相同的部分(代码)
c语言·笔记·c#
hjxxlsx2 小时前
介绍C++
开发语言·c++
l13372244932 小时前
C++ 3D冒险游戏开发案例
c++·c
Narnat2 小时前
判断链表是否有回环 C
c语言·数据结构·链表
筱姌3 小时前
牛客:Holding Two,Inverse Pair,Counting Triangles
c++·算法