2020蓝桥杯B国赛_玩具蛇_裸的dfs
作者:
nxdxml
,
2021-02-24 11:18:21
,
所有人可见
,
阅读 489
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 4;
int sx, sy, ex, ey, ans;
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, 1, 0, -1};
bool st[N][N];
void dfs(int x, int y, int cnt){
if(x == ex && y == ey && cnt == N * N) ans ++;
st[x][y] = true;
for(int i = 0; i < 4; i ++ ){
int a = dx[i] + x, b = dy[i] + y;
if(a < 0 || a >= N || b < 0 || b >= N) continue;
if(!st[a][b]) dfs(a, b, cnt + 1);
}
st[x][y] = false;
}
int main(){
for(int i = 0; i < N; i ++ )
for(int j = 0; j < N; j ++ )
for(ex = 0; ex < N; ex ++ )
for(ey = 0; ey < N; ey ++ ){
dfs(i, j, 1);
}
cout << ans;
return 0;
}