您的位置:首页 > 其它

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;

代码如下:
#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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: