UVa 861 :: Little Bishops

我做的题果然很杂= =,这题写了三个程序,提交九次。。

 

大意类似N皇后,而且还少个判断条件。。即使如此在棋盘规模最大为8、主教个数在20以下时我仍然不能直接提交AC(用的对角线判断,详见第一个程序的注释),所以我选择了打表,“这题写了三个程序”就是这么来的= =

 

 

会超时的搜索
#include <stdio.h>
#include
<string.h>

/*

0 1 2
-1 0 1
-2 -1 0

JUDGE1

1 2 3
2 3 4
3 4 5

JUDGE2

*/

long k, n, ans, judge1[40], judge2[40];

void solve ( long dep, long x, long y ){
if ( dep == k )
{ ans
++; return; }
long i;

if ( x >= n )
return;

for ( i = y; i < n; i ++ )
if ( ! judge1[x-i+n] && ! judge2[x+i+1] ){
judge1[x
-i+n] = judge2[x+i+1] = 1;
solve ( dep
+1, x, i );
judge1[x
-i+n] = judge2[x+i+1] = 0;
}
solve ( dep, x
+1, 0 );
}

int main(){
while ( scanf ( "%ld %ld", &n, &k ) == 2 ){

solve (
0, 0, 0 );
printf (
"%ld\n", ans );

ans
= 0;
memset ( judge1,
0, sizeof(judge1) );
memset ( judge1,
0, sizeof(judge1) );
}

getchar(), getchar();
return 0;
}

 

 

 

在第一个程序基础上略做改动,为打表而写,注意可以在ans为0的时候就认为规模为n棋盘不会再有解并跳出
#include <stdio.h>
#include
<string.h>

/*

0 1 2
-1 0 1
-2 -1 0

JUDGE1

1 2 3
2 3 4
3 4 5

JUDGE2

*/

long k, n, ans, judge1[40], judge2[40];

void solve ( long dep, long x, long y ){
if ( dep == k )
{ ans
++; return; }
long i;

if ( x >= n )
return;

for ( i = y; i < n; i ++ )
if ( ! judge1[x-i+n] && ! judge2[x+i+1] ){
judge1[x
-i+n] = judge2[x+i+1] = 1;
solve ( dep
+1, x, i );
judge1[x
-i+n] = judge2[x+i+1] = 0;
}
solve ( dep, x
+1, 0 );
}

int main(){

for ( n = 1; n <= 8; n ++ ){
for ( k = 0; k <= n*n; k ++ ){
solve (
0, 0, 0 );


if ( !ans )
break;

printf (
"%ld\n", ans );

ans
= 0;
memset ( judge1,
0, sizeof(judge1) );
memset ( judge1,
0, sizeof(judge1) );
}
putchar (
'\n' );
}

getchar(), getchar();
return 0;
}

 

 

 

伟大的表
#include <stdio.h>

const long table[9][65] = { {0},
{
1, 1}, {1, 4, 4}, {1, 9, 26, 26, 8}, {1, 16, 92, 232, 260, 112, 16},
{
1, 25, 240, 1124, 2728, 3368, 1960, 440, 32},
{
1, 36, 520, 3896, 16428, 39680, 53744, 38368, 12944, 1600, 64},
{
1, 49, 994, 10894, 70792, 282248, 692320, 1022320, 867328, 389312,
81184, 5792, 128},
{
1, 64, 1736, 26192, 242856, 1444928, 5599888, 14082528, 22522960, 22057472,
12448832, 3672448, 489536, 20224, 256}
};


int main(){
long n, k;
while ( scanf ( "%ld %ld", &n, &k ) == 2 ){
if ( n+k == 0 )
return 0;
printf (
"%ld\n", table[n][k] );
}
return 0;
}

 

 

提交九次是因为我数组开小了。。并且在我机子上运行得很好= =,但是在UVa那不行了,这件事情告诉我们要用新的Mingw里的gcc赶快换掉DevCpp自带的gcc。。。

posted on 2010-11-13 00:05  Jasper Ho  阅读(741)  评论(0编辑  收藏  举报

导航