Codeforces 486B - OR in Matrix

矩阵的 OR ,也是醉了。

题目意思很简单,就是问你有没有这么一个矩阵,可以变化,得到输入的矩阵。

要求是这个矩阵每行都可以上下任意移动,每列都可以左右任意移动。

解题方法:

  1.也是导致我WA 的原因,首先要判断是否是一个零矩阵,如果是一个零矩阵,那么YES输出

  2.判断输入矩阵存在1的那个位置,在输出矩阵的同一行同一列是否存在1,如果没有则NO输出

  3.开辟数组X[MAXN],Y[MAXN]记录这一行或者这一列是否有0这个元素

//#pragma comment(linker, "/STACK:16777216") //for c++ Compiler
#include <stdio.h>
#include <iostream>
#include <cstring>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <algorithm>
#define ll long long
#define Max(a,b) (((a) > (b)) ? (a) : (b))
#define Min(a,b) (((a) < (b)) ? (a) : (b))
#define Abs(x) (((x) > 0) ? (x) : (-(x)))
using namespace std;

const int INF = 0x3f3f3f3f;

int a[111][111], b[111][111], x[111], y[111];// Martix a as output Martix, Martix b as input Martix
int n, m;

int main(){
    int i, j, t, k;
    while(EOF != scanf("%d%d",&n,&m)){
        for(i = 1; i <= n; ++i){
            for(j = 1; j <= m; ++j){
                scanf("%d",&a[i][j]);
                b[i][j] = a[i][j];
                if(!a[i][j]){
                    x[i] = 1;
                    y[j] = 1;
                }
            }
        }
        bool ff = true;
        for(i = 1; i <= n; ++i){
            for(j = 1; j <= m; ++j){
                if(x[i] || y[j]){   // nice cood
                    a[i][j] = 0;
                } else{
                    ff = false;
                }
            }
        }
        bool falg = false;
        for(i = 1; i <= n; ++i){
            for(j = 1; j <= m; ++j){
                if(a[i][j]) falg = true;
            }
        }
        if(ff)  falg = true;
        for(i = 1; i <= n; ++i){
            for(j = 1; j <= m; ++j){
                bool kk = false;
                if(b[i][j]){
                    for(int ii = 1; ii <= n; ++ii){
                        if(a[ii][j])    kk = true;
                    }
                    for(int jj = 1; jj <= m; ++jj){
                        if(a[i][jj])    kk = true;
                    }
                    if(!kk){
                        falg = false;
                    }
                }
            }
        }

        if(falg){
            printf("YES\n");
            for(i = 1; i <= n; ++i){
                for(j = 1; j < m; ++j){
                    printf("%d ",a[i][j]);
                }
                printf("%d\n",a[i][m]);
            }
        }
        else{
            printf("NO\n");
        }
    }
    return 0;
}
时间: 2025-01-02 02:47:49

Codeforces 486B - OR in Matrix的相关文章

Educational Codeforces Round 40 C. Matrix Walk( 思维)

Educational Codeforces Round 40 (Rated for Div. 2) C. Matrix Walk time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output There is a matrix A of size x?×?y filled with integers. For every , *A**i,?

(最小生成树)Codeforces Educational Codeforces Round 9 Magic Matrix

You're given a matrix A of size n?×?n. Let's call the matrix with nonnegative elements magic if it is symmetric (so aij?=?aji), aii?=?0 and aij?≤?max(aik,?ajk) for all triples i,?j,?k. Note that i,?j,?k do not need to be distinct. Determine if the ma

CodeForces 486B

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 va

Codeforces 903F Clear The Matrix(状态压缩DP)

题目链接 Clear The Matrix 题意 给定一个$4 * n$的矩形,里面的元素为'.'或'*'.现在有4种正方形可以覆盖掉'*',正方形的边长分别为$1,2,3,4$. 求把整个矩形变成全'.'的最小代价. 考虑状压DP 设$f[i][j]$为前$i$列已经全部变成'.',第$i + 1$到第$i + 4$列的这$16$个格子状态为$j$的最小花费. 这$16$个格子标号如下 0   4   8   12 1   5   9   13 2   6  10  14 3   7  11 

codeforces 402E - Strictly Positive Matrix【tarjan】

首先认识一下01邻接矩阵k次幂的意义:经过k条边(x,y)之间的路径条数 所以可以把矩阵当成邻接矩阵,全是>0的话意味着两两之间都能相连,也就是整个都要在一个强连通分量里,所以直接tarjan染色,如果只有一个色块的话就是YES否则都是NO(其实应该能更简单一些,不过tarjan比较顺手) 还有就是我没仔细看题判了对角巷为0就NO结果WA-- #include<iostream> #include<cstdio> using namespace std; const int

【codeforces】Codeforces Round #277 (Div. 2) 解读

门户:Codeforces Round #277 (Div. 2) 486A. Calculating Function 裸公式= = #include <cstdio> #include <cstring> #include <algorithm> using namespace std ; typedef long long LL ; LL n ; int main () { while ( ~scanf ( "%I64d" , &n )

codeforces 289B - Polo the Penguin and Matrix 二分+dp

题意:给你一个序列,每一次可以对序列里面任意数+d 或者 -d 问你最少多少步能够使得数列里面所有的数相等 解题思路:从 1 - 10000 枚举这个数,二分找数列中小于等于它的最大的那个数,然后求前缀和以后刻意快速求出差值和的绝对值,差值和/d 就是我们所求数. 解题代码: 1 // File Name: 289b.cpp 2 // Author: darkdream 3 // Created Time: 2014年07月29日 星期二 22时33分11秒 4 5 #include<vecto

二分查找/暴力 Codeforces Round #166 (Div. 2) B. Prime Matrix

题目传送门 1 /* 2 二分查找/暴力:先埃氏筛选预处理,然后暴力对于每一行每一列的不是素数的二分查找最近的素数,更新最小值 3 */ 4 #include <cstdio> 5 #include <cstring> 6 #include <algorithm> 7 using namespace std; 8 9 const int MAXN = 5e2 + 10; 10 const int MAXM = 1e6 + 10; 11 const int INF = 0

Codeforces Round #531 (Div. 3) F. Elongated Matrix(状压DP)

F. Elongated Matrix 题目链接:https://codeforces.com/contest/1102/problem/F 题意: 给出一个n*m的矩阵,现在可以随意交换任意的两行,最后从上到下,从左到右形成一个序列s1,s2.....snm,满足对于任意相邻的两个数,它们差的绝对值的最大值为k. 现在问怎么交换行与行,可以使得最后的这个k最大. 题解: 人生中第一道状压dp~其实还是参考了这篇博客:https://blog.csdn.net/CSDNjiangshan/art