結果
| 問題 |
No.460 裏表ちわーわ
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2016-12-12 02:02:49 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 98 ms / 2,000 ms |
| コード長 | 2,369 bytes |
| コンパイル時間 | 721 ms |
| コンパイル使用メモリ | 75,968 KB |
| 実行使用メモリ | 5,248 KB |
| 最終ジャッジ日時 | 2024-11-29 12:24:02 |
| 合計ジャッジ時間 | 3,376 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 28 |
ソースコード
#include <iostream>
#include <vector>
#define repeat(i,n) for (int i = 0; (i) < int(n); ++(i))
#define repeat_from(i,m,n) for (int i = (m); (i) < int(n); ++(i))
typedef long long ll;
using namespace std;
template <class T> void setmin(T & a, T const & b) { if (b < a) a = b; }
template <typename X, typename T> auto vectors(X x, T a) { return vector<T>(x, a); }
template <typename X, typename Y, typename Z, typename... Zs> auto vectors(X x, Y y, Z z, Zs... zs) { auto cont = vectors(y, z, zs...); return vector<decltype(cont)>(x, cont); }
ll bit(int i) { return 1ll << i; }
bool is_on_field(int y, int x, int h, int w) { return 0 <= y and y < h and 0 <= x and x < w; }
const int inf = 1e9+7;
int main() {
int h, w; cin >> h >> w;
vector<vector<bool> > a = vectors(h, w, bool());
repeat (y,h) repeat (x,w) {
bool it; cin >> it;
a[y][x] = it;
}
int ans = inf;
repeat (s, bit(w)) {
repeat (t, bit(h)) {
int cnt = 0;
vector<vector<bool> > b = a;
auto flip = [&](int y, int x) {
cnt += 1;
repeat_from (dy,-1,1+1) repeat_from (dx,-1,1+1) {
int ny = y + dy;
int nx = x + dx;
if (is_on_field(ny, nx, h, w)) {
b[ny][nx] = not b[ny][nx];
}
}
};
repeat (y,h) {
repeat (x,w) {
if (y == 0) {
if (s & bit(x)) {
flip(y, x);
}
} else if (x == 0) {
if (t & bit(y-1)) {
flip(y, x);
}
} else {
if (b[y-1][x-1]) {
flip(y, x);
}
}
}
}
bool succeeded = true;
repeat (y,h) {
repeat (x,w) {
if (b[y][x]) {
succeeded = false;
}
}
}
if (succeeded) {
setmin(ans, cnt);
}
}
}
if (ans == inf) {
cout << "Impossible" << endl;
} else {
cout << ans << endl;
}
return 0;
}