求助
查看原帖
求助
359430
江户川コナン楼主2021/10/2 07:24

不知道为什么WA了4个点,请大佬求助。 我的思路是n*m%k==0时是YES,否则NO。 YES时,先将其一行一行一行地染色,再在剩下列中一列一列染色,最后把留下的长方形染成好几个小长方形。 比如输入为10 14 4时,输出为:

1  1  1  1 11 11 11 11 21 21 21 21 31 32
2  2  2  2 12 12 12 12 22 22 22 22 31 32
3  3  3  3 13 13 13 13 23 23 23 23 31 32
4  4  4  4 14 14 14 14 24 24 24 24 31 32
5  5  5  5 15 15 15 15 25 25 25 25 33 34
6  6  6  6 16 16 16 16 26 26 26 26 33 34
7  7  7  7 17 17 17 17 27 27 27 27 33 34
8  8  8  8 18 18 18 18 28 28 28 28 33 34
9  9  9  9 19 19 19 19 29 29 29 29 35 35
10 10 10 10 20 20 20 20 30 30 30 30 35 35

输入为12 12 8时, 输出为:

  1  1  1  1  1  1  1  1 13 14 15 16
  2  2  2  2  2  2  2  2 13 14 15 16
  3  3  3  3  3  3  3  3 13 14 15 16
  4  4  4  4  4  4  4  4 13 14 15 16
  5  5  5  5  5  5  5  5 13 14 15 16
  6  6  6  6  6  6  6  6 13 14 15 16
  7  7  7  7  7  7  7  7 13 14 15 16
  8  8  8  8  8  8  8  8 13 14 15 16
  9  9  9  9  9  9  9  9 17 17 17 17
 10 10 10 10 10 10 10 10 17 17 17 17
 11 11 11 11 11 11 11 11 18 18 18 18
 12 12 12 12 12 12 12 12 18 18 18 18

代码:

#include<bits/stdc++.h>
using namespace std;
#define int long long
const int maxn=1e6+5;
int T,n,m,k;
int a[maxn];
int twotoone(int i,int j){//将二维数组下标转为一维数组
    return (i-1)*m+j;
}
void read(){
    scanf("%lld%lld%lld",&n,&m,&k);
    if(n*m%k!=0){
        //cout<<"NO"<<endl;
        printf("NO\n");
        return ;
    }
    if(n==1){
        printf("YES\n");
        for(int i=1;i<=m;i++){
            printf("%lld ",(i-1)/k+1);
        }
        cout<<endl;
        return ;
    }
    if(k==1){
        printf("YES\n");
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){ 
                cout<<(i-1)*m+j<<" ";
            }
            cout<<endl;
        }
        return ;
    }
    printf("YES\n");
    int h=m/k;
    int b=1;
    for(int i=1;i<=h;i++){//一行一行涂色
        for(int j=1;j<=n;j++){
            for(int len=1;len<=k;len++){
                a[twotoone(j,i*k-k+len)]=b;
            }
            b++;
        }
    }
    int mod=m%k;
    h=n/k;
    for(int i=1;i<=h;i++){//一列一列涂色
        for(int j=1;j<=mod;j++){
            for(int len=1;len<=k;len++){
                a[twotoone(i*k-k+len,m-mod+j)]=b;
            }
            b++;
        }
    }
    //cout<<n-n%k+1<<" "<<m-m%k+1<<endl;
    int x=0;
    for(int i=n-n%k+1;i<=n;i++){//最后的框涂色
        for(int j=m-m%k+1;j<=m;j++){  
            //cout<<i<<" "<<j<<endl;
            a[twotoone(i,j)]=b;
            x++;
            if(x==k) b++;
        }
    }
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){ 
            //cout<<setw(3)<<a[twotoone(i,j)];
            printf("%lld ",a[twotoone(i,j)]);
        }
        printf("\n");
    }
    return ;
}
signed main(){
    cin>>T;
    while(T--){
        read();
    }
    return 0;
}
2021/10/2 07:24
加载中...