中石油训练赛 - 01 Matrix(构造)
題目描述
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
樣例輸出
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.
題目大意:給定一個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; }?
總結
以上是生活随笔為你收集整理的中石油训练赛 - 01 Matrix(构造)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: PAT (Advanced Level)
- 下一篇: POJ - 3764 The xor-l