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 }
  • 相关阅读:
    第一篇:spring+springMVC项目启动最终笔记(一web.xml)
    Mysql优化系列之查询性能优化前篇1
    Mysql优化系列之索引性能
    java设计模式系列1-- 概述
    操作系统-死锁(重要)
    Mysql优化系列之表设计规范和优化
    Mysql优化系列之数据类型优化
    git-常见问题解决
    使用 jenkins 搭建CI/CD流水线 (MAC)
    jenkins 异常
  • 原文地址:https://www.cnblogs.com/titicia/p/3917187.html
Copyright © 2011-2022 走看看