P11862 「o.OI R1」Easy ver. 题解

· · 题解

P11862 题目

解题思路

不难发现最小生成树的边权和理论最小为 2+3+\cdots+n\times m。

尝试改造,易知将数蛇形排列即可,如当 n=m=3 时,输出结果如下:

1 6 7
2 5 8
3 4 9

Code

#include<bits/stdc++.h>
using namespace std;
#define int long long
signed main()
{
    ios::sync_with_stdio(false);
    int n,m;
    cin>>n>>m;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            if(j%2==0)
                cout<<(j-1)*n+(n-i+1)<<' ';
            else
                cout<<(j-1)*n+i<<' ';
        }
        cout<<"\n";
    }
    return 0;
}