week8(第二波搜索进阶) A -hdu 2553 N皇后问题

A - N皇后问题
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

Description

在N*N的方格棋盘放置了N个皇后,使得它们不相互攻击(即任意2个皇后不允许处在同一排,同一列,也不允许处在与棋盘边框成45角的斜线上。 
你的任务是,对于给定的N,求出有多少种合法的放置方法。 

Input

共有若干行,每行一个正整数N≤10,表示棋盘和皇后的数量;如果N=0,表示结束。

Output

共有若干行,每行一个正整数,表示对应输入行的皇后的不同放置数量。

Sample Input

1
8
5
0

Sample Output

1
92
10

dfs水题,这里可以开三个标记数组,列标记,左斜标记,右斜标记,坐标(i,j),bookj [j] =0; bookleft [j-i+n]=1; bookright [ i+j]=1;


      judgex[i]=1;
judge1[j+i]=1;
judge2[j-i+n]=1;
dfs(j+1);
judgex[i]=0;
judge1[j+i]=0;
judge2[j-i+n]=0;


注意不要直接搜,先到搜索表。



/*#include<stdio.h>


int ans[11]={0,1,0,0,2,10,4,40,92,352,724};
int main(){



int n;
while(~(scanf("%d",&n)),n)
printf("%d\n",ans[n]); 
return 0;

*/
#include<stdio.h>
#include<string.h>
#define CL(a) memset(a,0,sizeof(a))
int judgex[11];//列 
int judge1[25];//斜1 
int judge2[25];//斜2 
int ans=0,n;
int a[11];
void dfs(int j){


if(n+1==j){
ans++; //printf("%d \n",ans);
return;
}
int i;
for(i=1;i<=n;i++){

if(judgex[i]==0&&judge1[j+i]==0&&judge2[j-i+n]==0){
// printf("x=%d j=%d\n",j,i);
judgex[i]=1;
judge1[j+i]=1;
judge2[j-i+n]=1;
dfs(j+1);
judgex[i]=0;
judge1[j+i]=0;
judge2[j-i+n]=0;


}



}





}
int main(){



int i;
for(n=1;n<=10;n++){
ans=0;
CL(judgex);
CL(judge1);
CL(judge2);
for(i=1;i<=n;i++){
judgex[i]=1;
judge1[1+i]=1;
judge2[1-i+n]=1;
  dfs(2);
  judgex[i]=0;
judge1[1+i]=0;
judge2[1-i+n]=0;
}
a[n]=ans;


}


while(~scanf("%d",&n),n){
printf("%d\n",a[n]);
}



return 0;

}

Rebuild started: Project: Project *** Using Compiler &#39;V6.22&#39;, folder: &#39;E:\Keil_v5\ARM\ARMCLANG\Bin&#39; Rebuild target &#39;Target 1&#39; assembling startup_stm32f10x_md.s... Start/core_cm3.c(445): error: non-ASM statement in naked function is not supported 445 | uint32_t result=0; | ^ Start/core_cm3.c(442): note: attribute is here 442 | uint32_t __get_PSP(void) __attribute__( ( naked ) ); | ^ Start/core_cm3.c(465): error: parameter references not allowed in naked functions 465 | "BX lr \n\t" : : "r" (topOfProcStack) ); | ^ Start/core_cm3.c(461): note: attribute is here 461 | void __set_PSP(uint32_t topOfProcStack) __attribute__( ( naked ) ); | ^ Start/core_cm3.c(479): error: non-ASM statement in naked function is not supported 479 | uint32_t result=0; | ^ Start/core_cm3.c(476): note: attribute is here 476 | uint32_t __get_MSP(void) __attribute__( ( naked ) ); | ^ Start/core_cm3.c(499): error: parameter references not allowed in naked functions 499 | "BX lr \n\t" : : "r" (topOfMainStack) ); | ^ Start/core_cm3.c(495): note: attribute is here 495 | void __set_MSP(uint32_t topOfMainStack) __attribute__( ( naked ) ); | ^ 4 errors generated. compiling core_cm3.c... compiling misc.c... compiling system_stm32f10x.c... compiling stm32f10x_adc.c... compiling stm32f10x_dac.c... compiling stm32f10x_exti.c... compiling stm32f10x_dbgmcu.c... compiling stm32f10x_dma.c... compiling stm32f10x_crc.c... compiling stm32f10x_cec.c... compiling stm32f10x_bkp.c... compiling stm32f10x_can.c... compiling stm32f10x_flash.c... compiling stm32f10x_pwr.c... compiling stm32f10x_fsmc.c... compiling stm32f10x_
03-31
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值