#include <iostream>
#include <string.h>
using namespace std;
int dir[4][2] = {{1,0}, {0,1},{-1,0}, {0,-1}};
int counter;
int visited[5][5];
int a[6];
void dfs(int x, int y, int cou);
int inde;
int count;
int main()
{
for(int i1=1; i1<10; ++i1)
for(int i2=i1+1; i2<12; ++i2)
for(int i3=i2+1; i3<=12; ++i3)
for(int i4=i3+1; i4<=12; ++i4)
for(int i5=i4+1; i5<=12; ++i5)
{
a[1] = i1;
a[2] = i2;
a[3] = i3;
a[4] = i4;
a[5] = i5;
memset(visited, 0, sizeof(visited));
int x = (i1-1)/4;
int y = (i1-1)%4;
visited[x][y] = 1;
inde = 1;
dfs(x, y, 1);
// if(inde == 5)
// ++count;
}
cout << counter << '\n';
cout << count;
}
void dfs(int x, int y, int cou)
{
if(inde == 5) //116, 从这两个结果看出, 判断条件一定不能放在参数里
++count;
if(cou == 5)
++counter; //49, 从这两个结果看出, 判断条件一定不能放在参数里
else
{
for(int i=0; i<4; ++i)
{
int sx = x+dir[i][0];
int sy = y+dir[i][1];
if(sx < 0 || sx > 2 || sy <0 || sy >3)
continue;
for(int j=1; j<=5; ++j)
{
int xx = (a[j]-1)/4;
int yy = (a[j]-1)%4;
if(!visited[sx][sy] && (sx ==xx && sy == yy))
{
visited[sx][sy] = 1;
++inde;
dfs(sx, sy, cou+1);
// visited[sx][sy] = 0;
}
}
}
}
}
蓝桥杯—剪邮票,对初学算法自己启发挺大的一题
最新推荐文章于 2023-02-17 11:11:20 发布