前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >loj#6030. 「雅礼集训 2017 Day1」矩阵(贪心 构造)

loj#6030. 「雅礼集训 2017 Day1」矩阵(贪心 构造)

作者头像
attack
发布2019-03-19 17:10:42
3840
发布2019-03-19 17:10:42
举报
文章被收录于专栏:数据结构与算法

题意

链接

Sol

自己都不知道自己怎么做出来的系列

不难观察出几个性质:

  1. 最优策略一定是先把某一行弄黑,然后再用这一行去覆盖不是全黑的列
  2. 无解当且仅当无黑色。否则第一个黑色所在的行i可以先把第i列弄出一个黑色,接下来第i列的黑色可以把第i行全部弄成黑色。

然后直接算出把每一行弄黑的步数取个min就行了。

代码里面有注释。

代码语言:javascript
复制
#include<bits/stdc++.h> 
#define Pair pair<int, int>
#define MP(x, y) make_pair(x, y)
#define fi first
#define se second
#define LL long long 
#define ull unsigned long long 
#define Fin(x) {freopen(#x".in","r",stdin);}
#define Fout(x) {freopen(#x".out","w",stdout);}
using namespace std;
const int MAXN = 1001, INF = 1e9 + 1, mod = 1e9 + 7;
const double eps = 1e-9, pi = acos(-1);
template <typename A, typename B> inline bool chmin(A &a, B b){if(a > b) {a = b; return 1;} return 0;}
template <typename A, typename B> inline bool chmax(A &a, B b){if(a < b) {a = b; return 1;} return 0;}
template <typename A, typename B> inline LL add(A x, B y) {if(x + y < 0) return x + y + mod; return x + y >= mod ? x + y - mod : x + y;}
template <typename A, typename B> inline void add2(A &x, B y) {if(x + y < 0) x = x + y + mod; else x = (x + y >= mod ? x + y - mod : x + y);}
template <typename A, typename B> inline LL mul(A x, B y) {return 1ll * x * y % mod;}
template <typename A, typename B> inline void mul2(A &x, B y) {x = (1ll * x * y % mod + mod) % mod;}
inline int read() {
    char c = getchar(); int x = 0, f = 1;
    while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
    while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
    return x * f;
}
int N;
char s[MAXN][MAXN];
int FullRow[MAXN], HaveRow[MAXN], FullCol[MAXN], HaveCol[MAXN];//full:每行每列是否全为1 / have:是否至少有一个1 
signed main() {
    N = read();
    for(int i = 1; i <= N; i++) scanf("%s", s[i] + 1);
    bool flag = 0;
    fill(FullRow + 1, FullRow + N + 1, 1); fill(FullCol + 1, FullCol + N + 1, 1);
    for(int i = 1; i <= N; i++) 
        for(int j = 1; j <= N; j++) 
            if(s[i][j] == '#') flag = 1, HaveRow[i] = 1, HaveCol[j] = 1;
            else FullRow[i] = 0, FullCol[j] = 0;
    if(!flag) return puts("-1"), 0;
    int ans = INF;
    for(int i = 1; i <= N; i++) {
        int now = 0;
        if(FullRow[i]) {
            for(int j = 1; j <= N; j++) now += (!FullCol[j]);//如果第i行全为黑,答案就加上不为黑的列数
            chmin(ans, now); 
            continue;
        }
        if(!HaveCol[i]) {
            if(!HaveRow[i]) continue;
            else now++;//用该行的一个黑去染黑对应的列
        }
        for(int j = 1; j <= N; j++) {
            if(FullCol[j]) continue;
            if(s[i][j] == '.') now += 2;
            else now++;
        }
        chmin(ans, now);
    }
    cout << ans;
    return 0;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019-03-14 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 题意
  • Sol
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档