思路:dfs深搜答案就行,我考试的时候以为是全排列,结果程序半天没出结果,考试的时候多思考,换不同的思路去做题!!
答案:552
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
const int N = 5;
int g[N][N];
//上右下左
int dx[4] = { -1, 0, 1, 0 };
int dy[4] = { 0, 1, 0, -1 };
int ans;
void dfs(int r, int l,int cnt)
{
if (cnt == 16)
{
ans++;
return;
}
g[r][l] = cnt;
for (int i = 0; i < 4; i++)
{
int x = dx[i] + r, y = dy[i] + l;
if (x < 4 && x >= 0 && y < 4 && y >= 0 && g[x][y] == 0)
dfs(x, y, cnt + 1);
}
g[r][l] = 0;//恢复现场
}
int main()
{
for (int i = 0; i < 4; i++)
for (int j = 0; j < 4; j++)
dfs(i, j, 1); //每个位置都可能放1去搜有多少中方案
cout << ans << endl;
system("pause");
return 0;
}