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.
3 3 1 1
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.
题目大意:给定一个n*m的01矩阵,规定a为每行中0或1的数目较小的一个,b为每列中0或1数目较小的一个,现在给出n,m,a,b,问是否存在一个矩阵可以满足上述规定,若不满足输出-1
题目分析:一道很好的构造题,简单却又不失难度,简单是因为zx学长做了一会就做出来了,难是因为我不会。。
实际上构造一个由四个部分组成的矩阵即可,比如这样:n=3,m=5,a=2,b=1
11000
00111
00111
大概就是这样的一个形式,就能满足所有的条件了
代码:
#include<iostream> #include<cstdlib> #include<string> #include<cstring> #include<cstdio> #include<algorithm> #include<climits> #include<cmath> #include<cctype> #include<stack> #include<queue> #include<list> #include<vector> #include<set> #include<map> #include<sstream> #include<deque> using namespace std; typedef long long LL; const int inf=0x3f3f3f3f; const int N=1e3+100; int maze[N][N]; int main() { // freopen("input.txt","r",stdin); int n,m,a,b; scanf("%d%d%d%d",&n,&m,&a,&b); for(int i=1;i<=b;i++) for(int j=1;j<=a;j++) maze[i][j]=1; for(int i=b+1;i<=n;i++) for(int j=a+1;j<=m;j++) maze[i][j]=1; for(int i=1;i<=n;i++) { for(int j=1;j<=m;j++) printf("%d",maze[i][j]); printf("\n"); } return 0; }