问题 L: 01 Matrix

mac2025-05-28  7

世上只有一种英雄主义,就是在认清生活真相之后依然热爱生活。

题目描述

We have a square grid with H rows and W columns. Snuke wants to write 0 or 1 in each of the squares. Here, all of the following conditions have to be satisfied: ·For every row, the smaller of the following is A: the number of 0s contained in the row, and the number of 1s contained in the row. (If these two numbers are equal, “the smaller” should be read as “either”.) ·For every column, the smaller of the following is B: the number of 0s contained in the column, and the number of 1s contained in the column. Determine if these conditions can be satisfied by writing 0 or 1 in each of the squares. If the answer is yes, show one way to fill the squares so that the conditions are satisfied. Constraints ·1≤H,W≤1000 ·0≤A ·2×A≤W ·0≤B ·2×B≤H ·All values in input are integers.

 

输入

Input is given from Standard Input in the following format: H W A B

输出

If the conditions cannot be satisfied by writing 0 or 1 in each of the squares, print −1. If the conditions can be satisfied, print one way to fill the squares so that the conditions are satisfied, in the following format: s11s12⋯s1W s21s22⋯s2W ⋮ sH1sH2⋯sHW Here sij is the digit written in the square at the i-th row from the top and the j-th column from the left in the grid. If multiple solutions exist, printing any of them will be accepted.

样例输入 Copy

3 3 1 1

样例输出 Copy

100 010 001

提示

Every row contains two 0s and one 1, so the first condition is satisfied. Also, every column contains two 0s and one 1, so the second condition is satisfied.

读懂题意很重要,这题题意看代码吧。。。。。。

#include <iostream> #include <cstdio> #include <algorithm> #include <string> #include <cstring> #include <cstdlib> #include <cmath> #include <stack> #include <queue> #include <set> #include <map> #include <vector> #include <ctime> #include <cctype> #include <bitset> #include <utility> #include <sstream> #include <complex> #include <iomanip> #define inf 0x3f3f3f3f typedef long long ll; using namespace std; int H,W,A,B,mp[2010][2010]; int main() { scanf("%d%d%d%d",&H,&W,&A,&B); for(int i=1; i<=B; i++) for(int j=1; j<=A; j++) mp[i][j]=1; for(int i=B+1; i<=H; i++) for(int j=A+1; j<=W; j++) mp[i][j]=1; for(int i=1; i<=H; i++) { for(int j=1; j<=W; j++) { printf("%d",mp[i][j]); } printf("\n"); } return 0; }

 

最新回复(0)