| 比赛 | 
    20100927 | 
    评测结果 | 
    AAAAAAAAAA | 
    | 题目名称 | 
    魔术数字游戏 | 
    最终得分 | 
    100 | 
    | 用户昵称 | 
    Pom | 
    运行时间 | 
    0.000 s  | 
    | 代码语言 | 
    C++ | 
    内存使用 | 
    0.00 MiB  | 
    | 提交时间 | 
    2010-09-27 22:33:43 | 
显示代码纯文本
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
using namespace std;
int a[5][5],i,j,k;
bool b[20];
void pd()
{
	if (a[1][3]+a[1][4]+a[2][3]+a[2][4]!=34) return;
	if (a[3][1]+a[3][2]+a[4][1]+a[4][2]!=34) return;
	if (a[3][3]+a[3][4]+a[4][3]+a[4][4]!=34) return;
	if (a[1][1]+a[2][2]+a[3][3]+a[4][4]!=34) return;
	if (a[1][4]+a[2][3]+a[3][2]+a[4][1]!=34) return;
	for (i=1;i<=4;i++)
	{
		for (j=1;j<4;j++)
			printf("%d ",a[i][j]);
		printf("%d\n",a[i][4]);
	}
	printf("\n");
}
void dfs(int x,int y)
{
	int i,j,k,t;
	if (x==5) pd();
	for (i=2;i<=16;i++)
		if (!b[i])
		{
		b[i]=true;
		if (a[x][y]==1)
			b[i]=false;
		else
		a[x][y]=i;
		
		
	if (x==2 && y==2)
		if (a[1][1]+a[1][2]+a[2][1]+a[2][2]!=34) 
		{
			b[a[x][y]]=false;
			continue;
		}
	if (x==3 && y==3)
		if (a[2][2]+a[2][3]+a[3][2]+a[3][3]!=34)
		{
			b[a[x][y]]=false;
			continue;
		}
	if (x==4 && y==1)
		if (a[1][4]+a[2][3]+a[3][2]+a[4][1]!=34)
		{
			b[a[x][y]]=false;
			continue;
		}
	if (y==4)
	{
		t=0;
		for (k=1;k<=4;k++)
			t+=a[x][k];
		if (t!=34) 
		{
			b[a[x][y]]=false;
			continue;
		}
	}
	if (x==4)
	{
		t=0;
		for (k=1;k<=4;k++)
			t+=a[k][y];
		if (t!=34) 
		{
			b[a[x][y]]=false;
			continue;
		}
	}
	
	if (y==4) dfs(x+1,1);
	else dfs(x,y+1);
	if (a[x][y]==1) return;
	b[a[x][y]]=false;
	}
}
int main()
{
	freopen("magic.in","r",stdin);
	freopen("magic.out","w",stdout);
	scanf("%d%d",&i,&j);
	memset(b,false,sizeof(b));
	a[i][j]=1;
	dfs(1,1);
}