日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

中石油训练赛 - 01 Matrix(构造)

發布時間:2024/4/11 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 中石油训练赛 - 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(构造)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。