zoukankan      html  css  js  c++  java
  • 1520C

    (1 sim n^2)放置在(n imes n)的棋盘当中,要求当前格子中的数字与其四连通的格子中的数字不相邻。

    考虑将当前格子染成黑色,与其四连通的格子染成白色,即下标((i,j))的格子,当(i+j)为偶数时染成黑色,(i+j)为奇数时染成白色,则颜色不同的格子中的数字不能相邻。

    按从左至右,从上至下的顺序,优先安排黑色格子中的数字,黑色格子安排完毕后再安排白色格子中的数字,数字的安排顺序为(1sim n^2)

    (n=3)的构造情况如下:

    [egin{pmatrix} 1 & 6 & 2\ 7 & 3 & 8\ 4 & 9 & 5\ end{pmatrix} ]

    注意点

    (n)(2)的情况无解。

    const int N=110;
    int g[N][N];
    int n;
     
    int main()
    {
        int T;
        cin>>T;
        while(T--)
        {
            cin>>n;
     
            int idx=0;
            for(int i=0;i<n;i++)
                for(int j=0;j<n;j++)
                    if((i+j) % 2 == 0)
                        g[i][j]=++idx;
     
            for(int i=0;i<n;i++)
                for(int j=0;j<n;j++)
                    if((i+j) % 2)
                        g[i][j]=++idx;
     
            if(n == 2) puts("-1");
            else
            {
                for(int i=0;i<n;i++)
                    for(int j=0;j<n;j++)
                        if(j == n-1) cout<<g[i][j]<<endl;
                        else cout<<g[i][j]<<' ';
            }
        }
        //system("pause");
        return 0;
    }
    
  • 相关阅读:
    ajax
    异步加载js的方法
    node的特点,优缺点及应用场景
    ajax面试题
    jQuery实现手风琴效果
    jQuery简介
    原型
    string 对象属性和方法
    函数声明和函数表达式
    JavaScript 基本语法
  • 原文地址:https://www.cnblogs.com/fxh0707/p/14803898.html
Copyright © 2011-2022 走看看