ZOJ-3212-K-Nice【6th浙江省赛】【构造】

ZOJ-3212-K-Nice

                Time Limit: 1 Second      Memory Limit: 32768 KB      Special Judge

This is a super simple problem. The description is simple, the solution is simple. If you believe so, just read it on. Or if you don’t, just pretend that you can’t see this one.

We say an element is inside a matrix if it has four neighboring elements in the matrix (Those at the corner have two and on the edge have three). An element inside a matrix is called “nice” when its value equals the sum of its four neighbors. A matrix is called “k-nice” if and only if k of the elements inside the matrix are “nice”.

Now given the size of the matrix and the value of k, you are to output any one of the “k-nice” matrix of the given size. It is guaranteed that there is always a solution to every test case.

Input

The first line of the input contains an integer T (1 <= T <= 8500) followed by T test cases. Each case contains three integers n, m, k (2 <= n, m <= 15, 0 <= k <= (n - 2) * (m - 2)) indicating the matrix size n * m and it the “nice”-degree k.

Output

For each test case, output a matrix with n lines each containing m elements separated by a space (no extra space at the end of the line). The absolute value of the elements in the matrix should not be greater than 10000.

Sample Input
2
4 5 3
5 5 3

Sample Output
2 1 3 1 1
4 8 2 6 1
1 1 9 2 9
2 2 4 4 3
0 1 2 3 0
0 4 5 6 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0

题目链接:ZOJ-3212

题目大意:构造出一个含k个nice的n*m的矩阵。nice:周围4个数字之和等于该数字(边缘的数字不属于,因为周围没有4个数字)

题目思路:我的构造方法是,0的四周4个为0.其余地方用1覆盖

以下是代码:

#include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <string>
#include <cstring>
using namespace std;
int g[20][20];
int main(){
    int t;
    cin >> t;
    while(t--)
    {
        int n,m,k;
        cin >> n >> m >> k;
        memset(g,-1,sizeof(g));
        for (int i = 1; i < n - 1; i++)
        {
            for (int j = 1; j < m - 1; j++)
            {
                if (k <= 0) break;
                if (g[i][j] <= 0)
                {
                    g[i][j] = 0;
                    g[i - 1][j] = 0;
                    g[i + 1][j] = 0;
                    g[i][j - 1] = 0;
                    g[i][j + 1] = 0;
                    k--;
                }
            }
            if (k <= 0) break;
        }
        for(int i = 0; i < n; i++)
        {
            for (int j = 0; j < m; j++)
            {
                if (g[i][j] < 0) g[i][j] = 1;
            }
        }
        for (int i = 0; i < n; i++)
        {
            cout << g[i][0];
            for (int j = 1; j < m; j++)
            {
                cout << " " << g[i][j];
            }
            cout << endl;
        }
    }
    return 0;
}

你可能感兴趣的:(ZOJ,构造,3212)