题目大意
给一个$n imes n$的棋盘,上面有黑白,每次任意取一行一列,把列染成行的颜色,问最少多少次让棋盘全黑。
简要题解
全黑列不要动,那么非全黑列在构造出全黑行之前显然是不增不减的(除非你主动去染全黑的列),然后构造出一个全黑行,然后去染就好。
比赛时就是这么想的,然而构造全黑行时脑子进了点水。。
1 #include <bits/stdc++.h> 2 using namespace std; 3 namespace my_header { 4 #define pb push_back 5 #define mp make_pair 6 #define pir pair<int, int> 7 #define vec vector<int> 8 #define pc putchar 9 #define clr(t) memset(t, 0, sizeof t) 10 #define pse(t, v) memset(t, v, sizeof t) 11 #define bl puts("") 12 #define wn(x) wr(x), bl 13 #define ws(x) wr(x), pc(' ') 14 const int INF = 0x3f3f3f3f; 15 typedef long long LL; 16 typedef double DB; 17 inline char gchar() { 18 char ret = getchar(); 19 for(; (ret == ' ' || ret == ' ' || ret == ' ') && ret != EOF; ret = getchar()); 20 return ret; } 21 template<class T> inline void fr(T &ret, char c = ' ', int flg = 1) { 22 for(c = getchar(); (c < '0' || '9' < c) && c != '-'; c = getchar()); 23 if (c == '-') { flg = -1; c = getchar(); } 24 for(ret = 0; '0' <= c && c <= '9'; c = getchar()) 25 ret = ret * 10 + c - '0'; 26 ret = ret * flg; } 27 inline int fr() { int t; fr(t); return t; } 28 template<class T> inline void fr(T&a, T&b) { fr(a), fr(b); } 29 template<class T> inline void fr(T&a, T&b, T&c) { fr(a), fr(b), fr(c); } 30 template<class T> inline char wr(T a, int b = 10, bool p = 1) { 31 return a < 0 ? pc('-'), wr(-a, b, 0) : (a == 0 ? (p ? pc('0') : p) : 32 (wr(a/b, b, 0), pc('0' + a % b))); 33 } 34 template<class T> inline void wt(T a) { wn(a); } 35 template<class T> inline void wt(T a, T b) { ws(a), wn(b); } 36 template<class T> inline void wt(T a, T b, T c) { ws(a), ws(b), wn(c); } 37 template<class T> inline void wt(T a, T b, T c, T d) { ws(a), ws(b), ws(c), wn(d); } 38 template<class T> inline T gcd(T a, T b) { 39 return b == 0 ? a : gcd(b, a % b); } 40 template<class T> inline T fpw(T b, T i, T _m, T r = 1) { 41 for(; i; i >>= 1, b = b * b % _m) 42 if(i & 1) r = r * b % _m; 43 return r; } 44 }; 45 using namespace my_header; 46 47 int a[555][555]; 48 49 int main() { 50 #ifdef lol 51 freopen("b.in", "r", stdin); 52 freopen("b.out", "w", stdout); 53 #endif 54 55 int n = fr(), c = n, t = 0; 56 for (int i = 1; i <= n; ++i) 57 for (int j = 1; j <= n; ++j) { 58 a[i][j] = gchar(); 59 if (a[i][j] == '#') 60 t = 1; 61 } 62 if (!t) { 63 puts("-1"); 64 return 0; 65 } 66 for (int i = 1; i <= n; ++i) { 67 int flg = true; 68 for (int j = 1; j <= n; ++j) { 69 if (a[j][i] == '.') 70 flg = false; 71 } 72 c -= flg; 73 } 74 int ans = 2 * n; 75 for (int i = 1; i <= n; ++i) { 76 int x = 0; 77 for (int j = 1; j <= n; ++j) 78 if (a[i][j] == '.') 79 ++x; 80 int flg = 1; 81 for (int j = 1; j <= n; ++j) 82 if (a[j][i] == '#') 83 flg = 0; 84 x += flg; 85 ans = min(ans, x + c); 86 } 87 wt(ans); 88 89 return 0; 90 }