题目
题解
这是一道比较经典的搜索题
可见ABCDEFGHI九种方案每种最多使用4次,那么九重for循环枚举出出组合情况即可
代码
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
int ch[10][10];
int n,tot[101],a[100],tmp[100],m[100];
void debug(int *A,int len)
{
for (int i=1; i<=len; i++) printf("%d ",A[i]); printf("\n");
}
void csh()
{
tot[1]=4; tot[2]=3; tot[3]=4; tot[4]=3; tot[5]=5;
tot[6]=3; tot[7]=4; tot[8]=3; tot[9]=4;
ch[1][1]=1; ch[1][2]=2; ch[1][3]=4; ch[1][4]=5;
ch[2][1]=1; ch[2][2]=2; ch[2][3]=3;
ch[3][1]=2; ch[3][2]=3; ch[3][3]=5; ch[3][4]=6;
ch[4][1]=1; ch[4][2]=4; ch[4][3]=7;
ch[5][1]=2; ch[5][2]=4; ch[5][3]=5; ch[5][4]=6; ch[5][5]=8;
ch[6][1]=3; ch[6][2]=6; ch[6][3]=9;
ch[7][1]=4; ch[7][2]=5; ch[7][3]=7; ch[7][4]=8;
ch[8][1]=7; ch[8][2]=8; ch[8][3]=9;
ch[9][1]=5; ch[9][2]=6; ch[9][3]=8; ch[9][4]=9;
}
bool judge()
{
for (int i=1; i<=9; i++)
if (tmp[i]!=0) return false;
return true;
}
void Move()
{
for (int i=1; i<=9; i++) tmp[i]=a[i];
for (int i=1; i<=9; i++)
{
if (m[i])
for (int j=1; j<=tot[i]; j++)
{
tmp[ch[i][j]]+=m[i]; tmp[ch[i][j]] %= 4;
}
}
}
void Input()
{
csh();
for (int i=1; i<=9; i++) scanf("%d",&a[i]);
}
void Output()
{
for (int i=1; i<=9; i++)
if (m[i])
for (int j=0; j<m[i]; j++)
printf("%d ",i);
}
int main()
{
Input();
Move();
for (m[1]=0; m[1]<=3; m[1]++)
for (m[2]=0; m[2]<=3; m[2]++)
for (m[3]=0; m[3]<=3; m[3]++)
for (m[4]=0; m[4]<=3; m[4]++)
for (m[5]=0; m[5]<=3; m[5]++)
for (m[6]=0; m[6]<=3; m[6]++)
for (m[7]=0; m[7]<=3; m[7]++)
for (m[8]=0; m[8]<=3; m[8]++)
for (m[9]=0; m[9]<=3; m[9]++)
{
Move();
if (judge()) {
Output(); return 0;}
}
return 0;
}