codeforce 486B
2014-11-12 19:42
295 查看
题意:
Let's define logical OR as an operation on two logical values (i. e. values that belong to the set {0, 1}) that is
equal to 1 if either or both of the logical values is set to 1, otherwise it is 0.
We can define logical OR of three or more logical values in the same manner:
where
is
equal to 1 if some ai = 1, otherwise it is equal to 0.
Nam has a matrix A consisting of m rows and n columns.
The rows are numbered from 1 to m, columns are numbered from 1 to n.
Element at row i (1 ≤ i ≤ m) and column j (1 ≤ j ≤ n)
is denoted as Aij. All elements of A are either 0 or 1. From matrix A,
Nam creates another matrix B of the same size using formula:
.
(Bij is OR of all elements in row i and
column j of matrix A)
Nam gives you matrix B and challenges you to guess matrix A. Although Nam is smart, he could probably make
a mistake while calculating matrix B, since size of A can be large.
思路:先构造一个n*m的矩阵A,并初始化使得A中所有元素都为1,然后再看B矩阵,如果Bij==0,那么就将A矩阵中的i行所有元素置0,j列所有元素置0,然后再看A能否变成B;
代码如下:
Let's define logical OR as an operation on two logical values (i. e. values that belong to the set {0, 1}) that is
equal to 1 if either or both of the logical values is set to 1, otherwise it is 0.
We can define logical OR of three or more logical values in the same manner:
where
is
equal to 1 if some ai = 1, otherwise it is equal to 0.
Nam has a matrix A consisting of m rows and n columns.
The rows are numbered from 1 to m, columns are numbered from 1 to n.
Element at row i (1 ≤ i ≤ m) and column j (1 ≤ j ≤ n)
is denoted as Aij. All elements of A are either 0 or 1. From matrix A,
Nam creates another matrix B of the same size using formula:
.
(Bij is OR of all elements in row i and
column j of matrix A)
Nam gives you matrix B and challenges you to guess matrix A. Although Nam is smart, he could probably make
a mistake while calculating matrix B, since size of A can be large.
思路:先构造一个n*m的矩阵A,并初始化使得A中所有元素都为1,然后再看B矩阵,如果Bij==0,那么就将A矩阵中的i行所有元素置0,j列所有元素置0,然后再看A能否变成B;
代码如下:
#include <iostream> #include <string.h> #include <string> #include <cstdio> #include <cmath> #include <algorithm> using namespace std; int n,m; int a[110][110],b[110][110]; bool vis_x[110],vis_y[110]; void init() { for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) {a[i][j]=1; } memset(vis_x,false,sizeof(vis_x)); memset(vis_y,false,sizeof(vis_y)); } void check(int x,int y) { for(int i=1;i<=n;i++) a[i][y]=0; for(int i=1;i<=m;i++) a[x][i]=0; } void solve() { for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) { if(b[i][j]==0) check(i,j); } } int main() { while(scanf("%d%d",&n,&m)!=EOF) { init(); for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) scanf("%d",&b[i][j]); solve(); for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) { if(a[i][j]==1) { vis_x[i]=true; vis_y[j]=true; } } int flag=1; for(int i=1;i<=n;i++) {for(int j=1;j<=m;j++) if(b[i][j]==1) { if(vis_x[i]==false&&vis_y[j]==false) { flag=0; break; } } if(!flag)break; } if(flag==0) {printf("NO\n"); continue; } printf("YES\n"); for(int i=1;i<=n;i++) {for(int j=1;j<=m;j++) printf("%d ",a[i][j]); printf("\n"); } } return 0; }
相关文章推荐
- codeforce 721C Journey (DAG上的dp)
- Codeforce Round #220 Div2
- codeforce 492 C D
- CodeForce 505C Mr. Kitayuta(DP+DFS)
- Codeforce 630B Moore's Law
- codeforce Good Bye 2017 C. New Year and Curling 暴力
- codeforce 602C The Two Routes
- codeforce C. Ray Tracing(扩展欧几里得|模拟)
- Codeforce 632E(背包变形)
- 【codeforce Gym 100570B】【最短路SPFA】 ShortestPath Query 【询问单源最短路径,每条边有一个颜色,要求路径上相邻边的颜色不能相同】
- codeforce 761E - Dasha and Puzzle(思维)
- Code Force 21B Intersection
- codeforce 55 D. Beautiful numbers(数位dp,好题)
- codeforce 766D Mahmoud and a Dictionary 带权并查集
- Codeforce 839 -- B Game of the Rows 【思维 + 贪心】
- codeforce A. 2Char(水题,暴力)
- codeforce 597C-Subsequences(dp+树状数组)
- Codeforce 1A. Theatre Square
- codeforce 850A Five Dimensional Points(特殊判别)
- codeforce 298 B Sail