[暴力] Educational Codeforces Round 71 (Rated for Div. 2) B. Square Filling (1207B)
2024-10-08 22:14:01
B. Square Filling
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
You are given two matrices A
and B. Each matrix contains exactly n rows and m columns. Each element of A is either 0 or 1; each element of B is initially 0
.
You may perform some operations with matrix B
. During each operation, you choose any submatrix of B having size 2×2, and replace every element in the chosen submatrix with 1. In other words, you choose two integers x and y such that 1≤x<n and 1≤y<m, and then set Bx,y, Bx,y+1, Bx+1,y and Bx+1,y+1 to 1
.
Your goal is to make matrix B
equal to matrix A. Two matrices A and B are equal if and only if every element of matrix A is equal to the corresponding element of matrix B
.
Is it possible to make these matrices equal? If it is, you have to come up with a sequence of operations that makes B
equal to A
. Note that you don't have to minimize the number of operations.
Input
The first line contains two integers n
and m (2≤n,m≤50
).
Then n
lines follow, each containing m integers. The j-th integer in the i-th line is Ai,j. Each integer is either 0 or 1
.
Output
If it is impossible to make B
equal to A, print one integer −1
.
Otherwise, print any sequence of operations that transforms B
into A in the following format: the first line should contain one integer k — the number of operations, and then k lines should follow, each line containing two integers x and y for the corresponding operation (set Bx,y, Bx,y+1, Bx+1,y and Bx+1,y+1 to 1). The condition 0≤k≤2500
should hold.
Examples
Input
Copy
3 3
1 1 1
1 1 1
0 1 1
Output
Copy
3
1 1
1 2
2 2
Input
Copy
3 3
1 0 1
1 0 1
0 0 0
Output
Copy
-1
Input
Copy
3 2
0 0
0 0
0 0
Output
Copy
0
Note
The sequence of operations in the first example:
000000000→110110000→110110110→110111111
题意:
给两个n*m的01矩阵A和B,给出了A中的元素,B中元素全为0,现在有一个操作可以在B中选一个坐标,它和右边下面右下的元素都变为1,问能否通过一些操作(不要求最少)使得B等于A,若能则输出步数和坐标,否则输出-1
思路:
暴力并判断边界(当时忘记判断边界结果被Hack了 ~TAT~ )
#include<bits/stdc++.h>
using namespace std;
const int amn=;
int a[amn][amn],idx[amn][amn],ansx[],ansy[];
int main(){
int n,m,tp=,valid=;
cin>>n>>m;
for(int i=;i<=n;i++){
for(int j=;j<=m;j++){
cin>>a[i][j];
idx[i][j]=;
}
}
for(int i=;i<=n;i++){
for(int j=;j<=m;j++){
if(a[i][j]){
if(!idx[i][j]&&(i+>n||j+>m)){ ///这里判断边界条件被hack了...当时想着i<n&&j<m,判非法时忘记判边界了
valid=0;
break;
}
if(a[i][j+]&&a[i+][j]&&a[i+][j+]){
idx[i][j+]=idx[i+][j]=idx[i+][j+]=;
ansx[++tp]=i;ansy[tp]=j;
}
else if(!idx[i][j]&&(!idx[i][j+]||!idx[i+][j]||!idx[i+][j+])){
valid=;
break;
}
}
}
if(valid==)break;
}
if(valid){
cout<<tp<<endl;
for(int i=;i<=tp;i++)cout<<ansx[i]<<' '<<ansy[i]<<endl;
}
else cout<<-<<endl;
}
/**
给两个n*m的01矩阵A和B,给出了A中的元素,B中元素全为0,现在有一个操作可以在B中选一个坐标,它和右边下面右下的元素都变为1,问能否通过一些操作(不要求最少)使得B等于A,若能则输出步数和坐标,否则输出-1
暴力并判断边界
**/
最新文章
- Vue.js组件学习
- .NET Core的文件系统[3]:由PhysicalFileProvider构建的物理文件系统
- Java中的private protected public和default的区别
- #1000 A + B (hihoCoder)
- xfce4 启用回收站
- 三维场景中使用BillBoard技术
- 让 Web 站点崩溃最常见的七大原因
- 向SharePoint页面添加后台代码
- VMware Workstation虚拟机使用ISO映像文件
- 技术不牛如何才拿到国内IT巨头的Offer(转)
- eclipse安装cucumber插件
- Java并发编程之ThreadGroup
- Linux记录-linux系统常用监控指标
- python2和python3的主要区别
- mxnet设置动态学习率(learning rate)
- ubuntu18.04系统安装+基本环境配置【原创】
- Nginx出现504 Gateway Time-out的解决方案
- 微软的SQLHelper类(含完整中文注释)
- Preparing Olympiad---cf550B(DFS或者状态压缩模板)
- iOS开发小结 - 让你的APP后台运行