XVIII Open Cup named after E.V. Pankratiev Stage 6, Grand Prix of Ukraine Problem D. Cut the Cake

It is Marichka’s k
2
-th birthday today! Zenyk bought a big cake for this occasion and now he wants to
cut it.
For the sake of simplicity, consider the cake as rectangular matrix with n rows and m columns. There
are exactly k
2
candles on it, each of them located in a unique cell of the matrix. Zenyk wants to cut the
cake with k − 1 horizontal and k − 1 vertical cuts. (Note that he’s only allowed to cut between cells.)
After the cutting, each of k
2 parts must contain a single candle.
You task is to find and output any valid cutting, or indicate that it’s impossible to achive the goal.
Input
The first line contains three integers n, m and k (2 ≤ k ≤ n, m ≤ 200). The following n lines contain a
string of m characters each. Character ‘1’ represents a cell with a candle on it, while ‘0’ respresents a
cell without candle.
It’s guaranteed that there are exactly k
2
candles on the cake.
Output
In the first line print “YES” if it’s possible to cut the cake the way Zenyk wants, otherwise print “NO”.
In case of positive answer the second line must contain k − 1 unique valid indices of the horizontal cuts,
and the third line must contain k − 1 unique valid indices of the vertical cuts. A cut between rows (or
columns) i and i + 1 has index i (1-based).
Examples
standard input standard output
4 4 2
1000
0001
0010
0001
YES
2
3
3 4 2
1110
0000
0100
NO

给一个蛋糕(矩形);现在有 k^2 个蜡烛在上面;
现在需要在行之间切 k-1刀,列之间切 k-1刀;
使得每一块的蛋糕有且仅有一个蜡烛;
如果不能,输出-1;
否则输出每一刀的位置;

思路:对于每一列(行),考虑这种一维的情况,我们需要k-1刀,将其分成 k 分,那么自然每一份应该是 k 分;同理对于行(列);

队友的代码:

#include<cstdio>
using namespace std;
const int maxn = 200 + 5;
int g[maxn][maxn];
int row[maxn], col[maxn];
int ansr[maxn], ansc[maxn];

int main() {
    int n, m, k; scanf("%d%d%d", &n, &m, &k);
    for (int i = 0; i < n; i++) 
        for (int j = 0; j < m; j++) 
            scanf("%1d", &g[i][j]);

    //处理每一列
    for (int j = 0; j < m; j++) 
        for (int i = 0; i < n; i++) 
            row[j] += g[i][j];

    int now = 0, cnt = 0, flag = 1;
    for (int j = 0; j < m; j++) {
        now += row[j];
        if (now < k)continue;
        else if (now == k) { now = 0, ansr[cnt++] = j; }
        else if (now > k) { flag = false; break; }
    }
    if (!flag) { printf("NO\n"); return 0; }

    //处理每一行
    for (int i = 0; i < n; i++) 
        for (int j = 0; j < m; j++) 
            col[i] += g[i][j];

    now = 0, cnt = 0, flag = 1;
    for (int i = 0; i < n; i++) {
        now += col[i];
        if (now < k)continue;
        else if (now == k) { now = 0, ansc[cnt++] = i; }
        else if (now > k) { flag = false; break; }
    }
    if (!flag) { printf("NO\n"); return 0; }

    //如果可以的话
    printf("YES\n");
    printf("%d", ansc[0]+1);
    for (int i = 1; i < k-1; i++)printf(" %d", ansc[i]+1);
    printf("\n%d", ansr[0]+1);
    for (int i = 1; i < k-1; i++)printf(" %d", ansr[i]+1);
    printf("\n");

    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_40273481/article/details/81812043
今日推荐