結果

問題 No.460 裏表ちわーわ
ユーザー beetbeet
提出日時 2019-03-30 17:09:21
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,903 bytes
コンパイル時間 1,934 ms
コンパイル使用メモリ 209,436 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-27 16:36:07
合計ジャッジ時間 3,003 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 3 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 AC 19 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 19 ms
5,376 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 AC 20 ms
5,376 KB
testcase_14 AC 19 ms
5,376 KB
testcase_15 AC 20 ms
5,376 KB
testcase_16 WA -
testcase_17 AC 19 ms
5,376 KB
testcase_18 AC 20 ms
5,376 KB
testcase_19 AC 20 ms
5,376 KB
testcase_20 WA -
testcase_21 AC 20 ms
5,376 KB
testcase_22 AC 20 ms
5,376 KB
testcase_23 AC 19 ms
5,376 KB
testcase_24 AC 20 ms
5,376 KB
testcase_25 AC 2 ms
5,376 KB
testcase_26 AC 1 ms
5,376 KB
testcase_27 AC 1 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
using Int = long long;
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}


template<typename T>
vector<T> make_v(size_t a){return vector<T>(a);}

template<typename T,typename... Ts>
auto make_v(size_t a,Ts... ts){
  return vector<decltype(make_v<T>(ts...))>(a,make_v<T>(ts...));
}

template<typename T,typename U,typename... V>
typename enable_if<is_same<T, U>::value!=0>::type
fill_v(U &u,const V... v){u=U(v...);}

template<typename T,typename U,typename... V>
typename enable_if<is_same<T, U>::value==0>::type
fill_v(U &u,const V... v){
  for(auto &e:u) fill_v<T>(e,v...);
}


//INSERT ABOVE HERE
signed main(){
  Int h,w;
  cin>>h>>w;
  auto a=make_v<Int>(h,w);
  for(Int i=0;i<h;i++)
    for(Int j=0;j<w;j++)
      cin>>a[i][j];

  const Int INF = 1e9;
  Int ans=INF,cnt=0;

  auto in=[&](Int y,Int x){
            return 0<=y&&y<h&&0<=x&&x<w;
          };
  auto flip=[&](Int y,Int x){
              for(Int i=-1;i<=1;i++)
                for(Int j=-1;j<=1;j++)
                  if(in(y+i,x+j)) a[y+i][x+j]^=1;              
            };
  
  function<void(Int, Int)> dfs=
    [&](Int y,Int x)->void{
      if(y==h){
        for(Int i=0;i<h;i++)
          for(Int j=0;j<w;j++)
            if(a[i][j]) return;
        chmin(ans,cnt);
        return;
      }
      
      Int ny=y+(x+1)/w,nx=(x+1)%w;      
      if(y==0||x==0){
        dfs(ny,nx);
        
        flip(y,x);
        cnt++;
        
        dfs(ny,nx);

        flip(y,x);
        cnt--;
        return;
      }
      
      if(a[y-1][x-1]){
        flip(y,x);
        cnt++;
        
        dfs(ny,nx);

        flip(y,x);
        cnt--;
      }else{
        dfs(ny,nx);
      }
    };
  dfs(0,0);
  
  if(ans>=INF) cout<<"Impossible"<<endl;
  cout<<ans<<endl;
  return 0;
}
0