1. 程式人生 > 實用技巧 >[2020藍橋杯B組決賽] E-玩具蛇

[2020藍橋杯B組決賽] E-玩具蛇

本題沒有題目,但來看題解的人都已知道題目意思了叭。

題解

  列舉每一點 $(i, j)$ ,總共 $16$ 種可能性,然後 $dfs$ 判斷層數 $u$,如果 $u=16$ ,說明所有點都走過了,方案數加一即可。

#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 的一道類似的真題,就先入為主了,嚶嚶嚶。