結果
| 問題 |
No.460 裏表ちわーわ
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2017-01-07 21:53:03 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 53 ms / 2,000 ms |
| コード長 | 2,119 bytes |
| コンパイル時間 | 1,787 ms |
| コンパイル使用メモリ | 173,876 KB |
| 実行使用メモリ | 6,820 KB |
| 最終ジャッジ日時 | 2024-12-17 17:11:28 |
| 合計ジャッジ時間 | 3,849 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 28 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector< int > vi;
typedef vector< vi > vvi;
typedef vector< ll > vl;
typedef vector< vl > vvl;
typedef pair< int, int > pii;
typedef vector< pii > vp;
typedef vector< double > vd;
typedef vector< vd > vvd;
typedef vector< string > vs;
template< class T1, class T2 >
int upmin( T1 &x, T2 v ){
if( x > v ){
x = v;
return 1;
}
return 0;
}
template< class T1, class T2 >
int upmax( T1 &x, T2 v ){
if( x < v ){
x = v;
return 1;
}
return 0;
}
const int INF = 0x3f3f3f3f;
int N, M;
vvi G;
void init(){
cin >> N >> M;
G = vvi( N, vi( M ) );
for( int i = 0; i < N; ++i ){
for( int j = 0; j < M; ++j ){
cin >> G[ i ][ j ];
}
}
}
const int dx[] = { 0, 0, 1, 0, -1, 1, 1, -1, -1 };
const int dy[] = { 0, 1, 0, -1, 0, 1, -1, 1, -1 };
void flip( vvi &t, int x, int y ){
for( int di = 0; di < 9; ++di ){
int nx = x + dx[ di ];
int ny = y + dy[ di ];
if( not ( 0 <= nx and nx < N and 0 <= ny and ny < M ) ) continue;
t[ nx ][ ny ] ^= 1;
}
}
int get_cost( vvi t ){
int res = 0;
for( int i = 1; i < N; ++i ){
for( int j = 1; j < M; ++j ){
if( t[ i - 1 ][ j - 1 ] ){
++res;
flip( t, i, j );
}
}
}
for( int i = 0; i < N; ++i ){
for( int j = 0; j < M; ++j ){
if( t[ i ][ j ] ){
return INF;
}
}
}
return res;
}
void preprocess(){
int ans = INF;
for( int i = 0; i < 1 << M; ++i ){
for( int j = 0; j < 1 << N - 1; ++j ){
int cost = __builtin_popcount( i ) + __builtin_popcount( j );
vvi t = G;
for( int k = 0; k < M; ++k ){
if( i & 1 << k ){
flip( t, 0, k );
}
}
for( int k = 0; k < N - 1; ++k ){
if( j & 1 << k ){
flip( t, k + 1, 0 );
}
}
cost += get_cost( t );
upmin( ans, cost );
}
}
if( ans == INF ){
cout << "Impossible" << endl;
} else{
cout << ans << endl;
}
}
void solve(){
}
signed main(){
ios::sync_with_stdio( 0 );
init();
preprocess();
solve();
return 0;
}