Metal Steak

Hard to eat

  C++博客 :: 首页 :: 联系 :: 聚合  :: 管理
  0 Posts :: 79 Stories :: 0 Comments :: 0 Trackbacks

公告

aaaaaaaaaaaa

常用链接

留言簿(1)

我参与的团队

搜索

  •  

最新评论

#include <iostream>
using namespace std;

int
n, g[
11][11], prime[201], gc, usednumbers[101];

void
__read__()
{
    cin 
>> n;
}

void
__init__()
{
    prime[
0= true;
    prime[
1= true;
    prime[
2= false;
    
forint i = 2; i <= 2 * n * n; i++ )
        
forint j = 2; i * j <= 2 * n * n; j++ )
            prime[i 
* j] = true;
}

void
__outp__()
{
    
forint i = 1; i <= n; i++ )
    {
        
forint j = 1; j < n; j++ )
            cout 
<< g[i][j] << " ";
        cout 
<< g[i][n] << endl;

    }
    cout 
<< endl;
}

bool
valid( 
int col, int row, int num )
{
    
if( col == 1 && row == 1 )
        
return true;
    
if( col > 1 && row == 1 )
        
if!prime[g[row][col - 1+ num] )
            
return true;
    
if( col == 1 && row > 1 )
        
if!prime[g[row - 1][col] + num] )
            
return true;
    
if( col > 1 && row > 1 )
        
if!prime[g[row][col - 1+ num]
         
&& !prime[g[row - 1][col] + num] )
            
return true;
    
return false;
}

void
__dfs__( 
int col, int row, int x )
{
    
if( x > n * n )
        
if( gc < 5 )
        {
            gc
++;
            __outp__();
        }
    
if( gc >= 5 )
        exit( 
0 );
    
if( x <= n * n )
        
forint i = 1; i <= n * n; i++ )
            
if!usednumbers[i] )
                
if( valid( col, row, i ) )
                {
                    usednumbers[i] 
= true;
                    g[row][col] 
= i;
                    
if!( x % n ) )
                        __dfs__( 
1, row + 1, x + 1 );
                    
else
                        __dfs__( col 
+ 1, row, x + 1 );
                    usednumbers[i] 
= false;
                }
}

int
main()
{
    __read__();
    __init__();
    __dfs__( 
111 );

    
if!gc )
        cout 
<< "0" << endl;

    
return 0;
}

posted on 2009-09-15 21:16 mad4alcohol 阅读(62) 评论(0)  编辑 收藏 引用

只有注册用户登录后才能发表评论。
网站导航:   博客园   博客园最新博文   博问   管理