记录编号 23562 评测结果 AAAAA
题目名称 [HAOI 2004模拟]数列问题 最终得分 100
用户昵称 Gravatar苏轼 是否通过 通过
代码语言 C 运行时间 0.063 s
提交时间 2011-03-15 13:57:13 内存使用 0.24 MiB
显示代码纯文本
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

const unsigned char gx[16][7]=
{{99, 0, 0, 0, 0, 0, 0},
 { 2, 4, 6,10,12,99, 0},
 { 1, 3, 5, 9,11,15,99},
 { 2, 4, 8,10,14,99, 0},
 { 1, 3, 7, 9,13,15,99},
 { 2, 6, 8,12,14,99, 0},
 { 1, 5, 7,11,13,99, 0},
 { 4, 6,10,12,99, 0, 0},
 { 3, 5, 9,11,15,99, 0},
 { 2, 4, 8,10,14,99, 0},
 { 1, 3, 7, 9,13,99, 0},
 { 2, 6, 8,12,99, 0, 0},
 { 1, 5, 7,11,99, 0, 0},
 { 4, 6,10,99, 0, 0, 0},
 { 3, 5, 9,15,99, 0, 0},
 { 2, 4, 8,14,99, 0, 0}};

long ans=0;
unsigned char n;
unsigned char *prn, *boo;
char *buf;
FILE *fin, *fout;

void go (const unsigned char deep)
{
	if (deep<n)
	{
		unsigned char i=0,last=prn[deep-1];

		while (gx[last][i]<=n)
		{
			if (!boo[gx[last][i]])
			{
				prn[deep]=gx[last][i];
				boo[gx[last][i]]=1;
				go(deep+1);
				boo[gx[last][i]]=0;
			}

			i++;
		}
	}
	else
	{
		unsigned char i;
		ans++;
		for (i=0; i<n; i++)
			fprintf(fout, "%hhu ", prn[i]);
	}
}

int main (void)
{
	unsigned char i;

	fin=fopen("dfs3.in", "r");
	fout=fopen("dfs3.out", "w");

	fscanf(fin,"%hhu",&n);
	prn = malloc(n*sizeof(unsigned char));
	boo = malloc((n+1)*sizeof(unsigned char));
	memset(boo, 0, (n+1)*sizeof(unsigned char));

	for (i=1; i<=n; i++)
	{
		prn[0]=i;
		boo[i]=1;
		go(1);
		boo[i]=0;
	}

	fprintf(fout,"%ld",ans);

	fclose(fin);
	fclose(fout);

	return 0;
}