zoukankan      html  css  js  c++  java
  • 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

    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

    思路:
    因为如果要求有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
    ");
    20         }
    21         
    22         
    23     }
    24     
    25     return 0;
    26 }
  • 相关阅读:
    重构27-Remove God Classes(去掉神类)
    重构25-Introduce Design By Contract checks(契约式设计)
    重构24-Remove Arrowhead Antipattern(去掉箭头反模式)
    重构23-Introduce Parameter Object(参数对象)
    重构22-Break Method(重构方法)
    重构19-Extract Factory Class(提取工厂类)
    重构20-Extract Subclass(提取父类)
    重构21-Collapse Hierarchy(去掉层级)
    重构15-Remove Duplication(删除重复)
    重构16-Encapsulate Conditional(封装条件)
  • 原文地址:https://www.cnblogs.com/titicia/p/3917187.html
Copyright © 2011-2022 走看看