思路

数据并不大,直接暴力dfs回溯就行,用一个标记数组b来表示哪个格子没被走,这个蛇得90°转向和向前就是说不能走已经走过的地方,细节见代码。

Code

#include<iostream>

using namespace std;
typedef long long ll;

const int Max = 1e6 + 5;
int b[100][100];
int dp[100][100];
int sec[8][2] = {
    {
   1,0},{
   -1,0},{
   0,1},{
   0,-1} };
ll sum = 0;
void dfs(int x, int y,int n)
{
   
	if (n == 16) {
   
		sum++;return;
	}
	for (int i = 0;i <= 3;i++)
	{
   
		int tx = x + sec[i][0];
		int ty = y + sec[i][1];
		if (tx <= 4 && tx >= 1 && ty <= 4 && ty >= 1 && b[tx][ty]==0)
		{
   
			b[tx][ty] = 1;
			dfs(tx, ty, n + 1);
			b[tx][ty] = 0;
		}
	}
	
}

int main()
{
   
	for (int i = 1;i <= 4;i++)
	{
   
		for (int j = 1;j <= 4;j++)
		{
   
			b[i][j] = 1;
			dfs(i, j, 1);
			b[i][j] = 0;
		}
	}
	cout << sum;
	
}