[2020蓝桥杯B组决赛] E-玩具蛇

本题没有题目,但来看题解的人都已知道题目意思了叭。ios

题解spa

  枚举每一点 $(i, j)$ ,总共 $16$ 种可能性,而后 $dfs$ 判断层数 $u$,若是 $u=16$ ,说明全部点都走过了,方案数加一便可。code

#include <iostream>
#include <cstring>
using namespace std;

const int N = 20;
int n = 4, ans;
int maze[N][N];
bool st[N][N];
int row[4] = {-1, 0, 1, 0}, col[4] = {0, 1, 0, -1};

int check(int x, int y)
{
    return x >= 0 && x < n && y >= 0 && y < n;
}

void dfs(int x, int y, int u)
{ 
    if (u == 15) {
        ++ans;
        return ;
    }
    for (int i = 0; i < 4; ++i) {
        int xx = x + row[i];
        int yy = y + col[i];
        if (check(xx, yy) && !st[xx][yy]) {
            st[xx][yy] = true;
            dfs(xx, yy, u + 1);
            st[xx][yy] = false;
        }
    }
}

int main()
{
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            st[i][j] = true;
            dfs(i, j, 0);
            memset(st, false, sizeof(st));
        }
    }
    cout << ans << endl;
    return 0;
}

   但是,我,呜呜呜~~,只判 $(0, 0)$ 这一个点,由于以前作了 2019 的一道相似的真题,就先入为主了,嘤嘤嘤。blog