首页 > 代码库 > ZOJ 3212 K-Nice (K)

ZOJ 3212 K-Nice (K)

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 nmk (2 <= nm <= 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

 

24 5 35 5 3

 

Sample Output

 

2 1 3 1 14 8 2 6 11 1 9 2 92 2 4 4 30 1 2 3 00 4 5 6 00 0 0 0 00 0 0 0 00 0 0 0 0

思路:
因为如果要求有K个满足,那么别的都是0,只要填入(n-2)*(m-2)-k个别的数即可满足。
 1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 #include <string> 5 using namespace std; 6 int T, n, m, k; 7 #define maxn 20   8 int a[maxn][maxn]; 9 int main(){10     scanf("%d", &T);11     while(T--){12         scanf("%d%d%d", &n, &m, &k); 13         k = (n-2)*(m-2)-k;14         for(int i = 1; i <= n; i++){15             printf("0");16             for(int j = 2; j <= m-1; j++){17                 if(k == 0) printf(" 0");18                 else printf(" %d", k--);19             }printf(" 0\n");20         }21         22         23     }24     25     return 0;26 }