标题:简单弄了个八皇后来玩玩~
只看楼主
九转星河
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:长长久久
等 级:贵宾
威 望:52
帖 子:5023
专家分:14003
注 册:2016-10-22
结帖率:99.25%
 问题点数:0 回复次数:1 
简单弄了个八皇后来玩玩~
弄了个八皇后,细节方面弄了很久,看看效果如何~

程序代码:
#include<stdio.h>
#include<string.h>
    
void fun ( void );

unsigned check(const unsigned short [],unsigned,unsigned );

void set( unsigned short [],unsigned*,unsigned,unsigned );

void print( unsigned,unsigned );

int main( void )
{
   fun();

    return 0;
}
void fun ( void )
{
    #define _NUM(x,s) (((x)>>(3*(s)))&7)
    
    unsigned short s[3];
    
    unsigned i=0;
    unsigned j=0;
    unsigned x=0;
    
    unsigned count=0;
    
    memset(s,0,sizeof (s));
    
    while (1)
    {
        for (;j!=8;++j)
        {
            if (!check(s,i,j))
                continue;
            
           set(s,&x,i++,j);
           break;         
        }

        if (!i)
            break;
        else if (i==8)
            print(x,++count);
        else if (j!=8)
        {
            j=0;
            continue;
        }
        
        --i;     
        
       j=_NUM(x,i);         
      set(s,&x,i,j++);
   }
   
   printf("八皇后问题共有%u种情况!\n",count);
    
    #undef _NUM
}

unsigned check(const unsigned short s[],unsigned i,unsigned j )
{
    if (s[0]&(1<<j))
        return 0;
     if (s[0]&(1<<8+i))
         return 0;   
    if (s[1]&(1<<8+j-i))
        return 0;
           
    if (s[2]&(1<<15-j-i))
       return 0;
       
    return 1;      
}

void set( unsigned short s[],unsigned* x,unsigned i,unsigned j)
{
    s[0]^=1<<j;
    s[0]^=1<<8+i;
    s[1]^=1<<8+j-i;
    s[2]^=1<<15-j-i;
    *x^=j<<3*i;
}

void print( unsigned x ,unsigned count )
{
    #define _NUM(x,s) (((x)>>(3*(s)))&7)
    
    unsigned i;
    
   printf("第%u种情况如下:\n\n",count);
   for (i=0;i!=8;++i)
   {
       unsigned j;
       for (j=0;j!=8;++j)
               putchar(j==_NUM(x,i)?'o':'*');
       puts("");
   }
   
   puts("");
   
   #undef _NUM
}


[此贴子已经被作者于2018-3-9 20:32编辑过]

搜索更多相关主题的帖子: 八皇后 void unsigned return count 
2018-03-09 17:56
九转星河
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:长长久久
等 级:贵宾
威 望:52
帖 子:5023
专家分:14003
注 册:2016-10-22
得分:0 
然后改了一下省了一个字节~

程序代码:
#include<stdio.h>
#include<string.h>
    
void fun ( void );

unsigned check(const unsigned char *,unsigned,unsigned );

void set( unsigned char *,unsigned,unsigned );

void print(const unsigned char* s ,unsigned count );

int main( void )
{
   fun();

    return 0;
}
void fun ( void )
{
    #define _NUM(x,s) (((*( unsigned* )(x+6))>>(3*(s)))&7)
    
    unsigned char s[9];
    
    unsigned i=0;
    unsigned j=0;
    
    unsigned count=0;
    
    memset(s,0,sizeof (s));
    
    while (1)
    {
        for (;j!=8;++j)
        {
            if (!check(s,i,j))
                continue;
            
           set(s,i++,j);
           break;         
        }

        if (!i)
            break;
        else if (i==8)
            print(s,++count);
        else if (j!=8)
        {
            j=0;
            continue;
        }
        
        --i;     
        
       j=_NUM(s,i);         
      set(s,i,j++);
   }
   
   printf("八皇后问题共有%u种情况!\n",count);
    
    #undef _NUM
}

unsigned check(const unsigned char* s,unsigned i,unsigned j )
{
    if ((*( unsigned* )(s+0))&(1<<j))
        return 0;
    if ((*( unsigned* )(s+1))&(1<<i))
         return 0;   
    if ((*( unsigned* )(s+2))&(1<<8+j-i))
        return 0;
    if ((*( unsigned* )(s+4))&(1<<15-j-i))
       return 0;
       
    return 1;      
}

void set( unsigned char* s,unsigned i,unsigned j)
{
    (*( unsigned* )(s+0))^=1<<j;
    (*( unsigned* )(s+1))^=1<<i;
    (*( unsigned* )(s+2))^=1<<8+j-i;
    (*( unsigned* )(s+4))^=1<<15-j-i;
    (*( unsigned* )(s+6))^=j<<3*i;
}

void print(const unsigned char* s ,unsigned count )
{
    #define _NUM(x,s) (((*( unsigned* )(x+6))>>(3*(s)))&7)
    
    unsigned i;
    
   printf("第%u种情况如下:\n\n",count);
   for (i=0;i!=8;++i)
   {
       unsigned j;
       for (j=0;j!=8;++j)
               putchar(j==_NUM(s,i)?'o':'*');
       puts("");
   }
   
   puts("");
   
   #undef _NUM
}


[此贴子已经被作者于2018-3-9 19:45编辑过]


[code]/*~个性签名:bug是什么意思?bug是看上去没有可能的东西实际上是有可能做到的 就是这样~2018-08-08更~*/[/code]
2018-03-09 19:38



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-485125-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.077365 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved