zoukankan      html  css  js  c++  java
  • N皇后

    //main.c
    //
    1.cpp : Defines the entry point for the console application. // #include "stdio.h" #include "queen.h" int main(int argc, char* argv[]) { printf("\nnum = %d\n",queen(12)); return 0; }

    dbg.h

    1 #ifndef        _MRCHEN_DBG_H_
    2 #define        _MRCHEN_DBG_H_
    3     //#define    MY_DEBUG
    4     void    dbgprint(char *sz_msg);
    5 #endif

    dbg.c

    1 #include    "stdio.h"
    2 #include    "dbg.h"
    3 
    4 void    dbgprint(char *sz_msg)
    5 {
    6     #ifdef    MY_DEBUG
    7         printf("FILE : %s  LINE : %d\n\tMSG : %s\n",__FILE__,__LINE__,sz_msg);
    8     #endif
    9 }

    queen.h

     1 #ifndef    _MRCHEN_2_H_
     2 #define    _MRCHEN_2_H_
     3 #ifndef    BOOL
     4 #define    BOOL    char    
     5 #define    FALSE    0      
     6 #define    TRUE    1      
     7 #endif
     8 
     9 typedef    struct    _tagPanel{
    10     char        *panel_p;//排列
    11     int         count;//找到的排列的种数
    12     int         x,y;//
    13     int         n;//棋盘的size
    14 }PANEL,*PPANEL;
    15 
    16 //查看(x_i,y_i)位置是否合法
    17 static    BOOL    init_panel(int    n);
    18 
    19 static    void    clean_panel();
    20 
    21 static    BOOL    check();
    22 
    23 static    BOOL    set(int    i);
    24 
    25 static    BOOL    next();
    26 
    27 static    int    next_line();
    28 static    int    prev_line();
    29 
    30 BOOL      print();
    31 
    32 int    queen(int    n);
    33         
    34 #endif

    queen.c

      1 #include    "malloc.h"
      2 #include    "string.h"
      3 #include    "queen.h"
      4 #include    "dbg.h"
      5 #define        MY_DEBUG
      6 
      7 static    PANEL        pan_st;
      8 
      9 int    queen(int    n)
     10 {    
     11     int    i = 0;
     12     int    ln;
     13     //init    
     14     if(FALSE == init_panel(n)){
     15         dbgprint("init failed");
     16         return    -1;
     17     }
     18     //loop
     19     for(i = 0;i < (n>>1);i++){
     20         set(i);
     21         next_line();
     22         do{
     23             if(!check()){
     24                 if(next()){
     25                     continue;    
     26                 }
     27                 if(prev_line()==0){
     28                     break;
     29                 }
     30                 continue;
     31             }else{
     32                 if(pan_st.n != next_line()){
     33                     continue;
     34                 }else{
     35                 //    print();
     36                     pan_st.count++;
     37                     if(next()){
     38                         continue;
     39                     }
     40                     if(prev_line()==0){
     41                         break;
     42                     }
     43                 }
     44             }
     45 
     46         }while(1);
     47     }
     48     //clean
     49     clean_panel();
     50     return    pan_st.count<<=1;
     51 }
     52 
     53 
     54 //init panel ,ok
     55 static    BOOL    init_panel(int    n)
     56 {
     57     dbgprint("init_panel");
     58     pan_st.panel_p = (char*)malloc(n*sizeof(char));
     59     if(pan_st.panel_p == NULL){
     60         return    FALSE;
     61     }
     62     pan_st.count = 0;
     63     pan_st.n = n;
     64     pan_st.x = pan_st.y = 0;
     65     return    TRUE;
     66 }
     67 
     68 //destroy panel,ok
     69 static    void    clean_panel()
     70 {
     71     dbgprint("clean_panel");
     72     if(pan_st.panel_p != NULL){
     73         free(pan_st.panel_p);
     74     }
     75 }
     76 
     77 
     78 //check
     79 static    BOOL    check()
     80 {
     81     int    i = 0;
     82 
     83     dbgprint("check");
     84     for(i=0;i < pan_st.y ;i++){
     85         if(pan_st.panel_p[i] == pan_st.x){
     86         //    printf("i = %d  (y,x) = (%d,%d)  pan_st.panel_p[%d] = %d\n",
     87         //            i,pan_st.y,pan_st.x,i,pan_st.panel_p[i]);
     88             return    FALSE;
     89         }
     90         if((pan_st.y-i) == (pan_st.x-pan_st.panel_p[i])
     91             ||(pan_st.y-i)==-(pan_st.x-pan_st.panel_p[i])){
     92             
     93         //        printf("i = %d  (y,x) = (%d,%d)  pan_st.panel_p[%d] = %d\n",
     94         //                i,pan_st.y,pan_st.x,i,pan_st.panel_p[i]);
     95                     
     96             return    FALSE;
     97         }
     98     }
     99     //printf("%d---%d\n",pan_st.y,pan_st.x);
    100     return    TRUE;
    101 }
    102 
    103 
    104 //set first
    105 static    BOOL    set(int    x)
    106 {
    107     dbgprint("set");
    108     if(x >= pan_st.n || x<0){
    109         return    FALSE;
    110     }
    111     pan_st.x = x;
    112     pan_st.y = 0;
    113     pan_st.panel_p[0] = x;
    114     return    TRUE;
    115 }
    116 
    117 
    118 //get next
    119 static    BOOL    next()
    120 {
    121     dbgprint("next");
    122     do{
    123         if(pan_st.x+1 == pan_st.n){
    124             return    FALSE;
    125         }
    126         pan_st.panel_p[pan_st.y] = ++pan_st.x;
    127     }while(!check());
    128     return    TRUE;
    129 }
    130 
    131 
    132 //return    next line
    133 static    int    next_line()
    134 {
    135     int    i = pan_st.y+1;
    136     dbgprint("next_line");
    137     if(i < pan_st.n ){
    138         //printf("\n\ty = %d  x = %d\n",pan_st.y,pan_st.x);
    139         pan_st.panel_p[pan_st.y] = pan_st.x;        
    140         pan_st.y++;
    141         pan_st.x = 0;
    142     }
    143     return    i;
    144 }
    145 
    146 //return    previous line
    147 static    int    prev_line()
    148 {
    149     int    i = 0;
    150     dbgprint("prev_line");
    151     while(0!=(i=--pan_st.y)){
    152         pan_st.panel_p[pan_st.y] += 1;
    153         if(pan_st.panel_p[pan_st.y] < pan_st.n){
    154             pan_st.x = pan_st.panel_p[pan_st.y];
    155             break;
    156         }else{
    157             continue;
    158         }
    159     }
    160     return    i;
    161 }
    162 
    163 
    164 //print the panel,ok
    165 BOOL      print()
    166 {
    167     int    i = 0,j = 0;
    168     dbgprint("print");
    169     for(; i < pan_st.n ;i++){
    170         for(j=0;j < pan_st.n; j++){
    171             if(pan_st.panel_p[i] != j){
    172                 printf("O ");
    173             }else{
    174                 printf("# ");
    175             }
    176         }
    177         printf("\n");
    178     }
    179     puts("=========================");
    180     return    TRUE;
    181 }

    makefile

    cc	=	cc
    obj	=	main.o\
    		queen.o\
    		dbg.o
    targets	:	$(obj)
    	$(cc)	$(obj)	-o	main
    main.o	:	main.c
    queue.o	:	queen.h
    dbg.o	:	dbg.h
    clean:
    	rm	*.o
    	rm	main
    
  • 相关阅读:
    openssl自签发证书
    安装tomcat8 env
    路由信息相关 route 网卡
    安装jdk env
    sublime使用与配置
    docker仓库登录 配置insecure-registries
    harobor私有docker镜像仓库
    git版本回退的两种方式
    git diff命令的使用
    Kali Linux中的自带字典&crunch自建字典
  • 原文地址:https://www.cnblogs.com/wowk/p/2979831.html
Copyright © 2011-2022 走看看