結果

問題 No.348 カゴメカゴメ
ユーザー kzyKTkzyKT
提出日時 2016-02-27 00:07:37
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 167 ms / 2,000 ms
コード長 2,648 bytes
コンパイル時間 1,411 ms
コンパイル使用メモリ 164,728 KB
実行使用メモリ 125,364 KB
最終ジャッジ日時 2023-10-24 18:05:20
合計ジャッジ時間 5,739 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 104 ms
61,048 KB
testcase_01 AC 22 ms
43,340 KB
testcase_02 AC 121 ms
86,152 KB
testcase_03 AC 101 ms
51,616 KB
testcase_04 AC 20 ms
43,524 KB
testcase_05 AC 22 ms
46,036 KB
testcase_06 AC 21 ms
43,172 KB
testcase_07 AC 21 ms
43,164 KB
testcase_08 AC 19 ms
43,440 KB
testcase_09 AC 21 ms
43,216 KB
testcase_10 AC 21 ms
43,184 KB
testcase_11 AC 21 ms
43,196 KB
testcase_12 AC 25 ms
44,940 KB
testcase_13 AC 22 ms
43,620 KB
testcase_14 AC 22 ms
43,332 KB
testcase_15 AC 167 ms
125,364 KB
testcase_16 AC 21 ms
43,164 KB
testcase_17 AC 20 ms
43,164 KB
testcase_18 AC 21 ms
43,164 KB
testcase_19 AC 21 ms
43,164 KB
testcase_20 AC 21 ms
43,248 KB
testcase_21 AC 21 ms
43,244 KB
testcase_22 AC 21 ms
43,168 KB
testcase_23 AC 117 ms
66,896 KB
testcase_24 AC 114 ms
64,892 KB
testcase_25 AC 118 ms
66,752 KB
testcase_26 AC 117 ms
66,828 KB
testcase_27 AC 116 ms
64,964 KB
testcase_28 AC 116 ms
65,872 KB
testcase_29 AC 116 ms
67,464 KB
testcase_30 AC 114 ms
66,192 KB
testcase_31 AC 114 ms
63,000 KB
testcase_32 AC 117 ms
66,468 KB
testcase_33 AC 29 ms
45,824 KB
testcase_34 AC 28 ms
45,600 KB
testcase_35 AC 28 ms
45,444 KB
testcase_36 AC 28 ms
45,524 KB
testcase_37 AC 29 ms
45,784 KB
testcase_38 AC 29 ms
45,820 KB
testcase_39 AC 29 ms
45,472 KB
testcase_40 AC 29 ms
45,696 KB
testcase_41 AC 29 ms
45,632 KB
testcase_42 AC 28 ms
45,688 KB
testcase_43 AC 22 ms
43,680 KB
testcase_44 AC 22 ms
43,684 KB
testcase_45 AC 23 ms
43,688 KB
testcase_46 AC 22 ms
43,740 KB
testcase_47 AC 23 ms
43,796 KB
testcase_48 AC 22 ms
43,740 KB
testcase_49 AC 22 ms
43,744 KB
testcase_50 AC 22 ms
43,836 KB
testcase_51 AC 22 ms
43,656 KB
testcase_52 AC 23 ms
43,736 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define all(c) (c).begin(),(c).end()
#define rrep(i,n) for(int i=(int)(n)-1;i>=0;i--)
#define REP(i,m,n) for(int i=(int)(m);i<(int)(n);i++)
#define rep(i,n) REP(i,0,n)
#define iter(c) __typeof((c).begin())
#define tr(it,c) for(iter(c) it=(c).begin();it!=(c).end();it++)
#define mem(a) memset(a,0,sizeof(a))
#define pd(a) printf("%.10f\n",a)
#define pb(a) push_back(a)
#define in(a) insert(a)
#define pi M_PI
#define R cin>>
#define F first
#define S second
#define C class
#define ll long long
#define ln cout<<'\n'
#define _(_1,_2,_3,N,...)N
#define pr(...) _(__VA_ARGS__,pr3,pr2,pr1)(__VA_ARGS__)
template<C T>void pr1(T a){cout<<a;ln;}
template<C T,C T2>void pr2(T a,T2 b){cout<<a<<' '<<b;ln;}
template<C T,C T2,C T3>void pr3(T a,T2 b,T3 c){cout<<a<<' '<<b<<' '<<c;ln;}
template<C T>void PR(T a,int n){rep(i,n){if(i)cout<<' ';cout<<a[i];}ln;}
bool check(int n,int m,int x,int y){return x>=0&&x<n&&y>=0&&y<m;}
const ll MAX=1000000007,MAXL=1LL<<60,dx[8]={-1,-1,-1,0,0,1,1,1},dy[8]={-1,0,1,-1,1,-1,0,1},dxx[4]={-1,0,1,0},dyy[4]={0,1,0,-1};
typedef pair<int,int> P;

int n,m;
string s[1111];
int c[1111][1111],u[1111][1111],uu[1111][1111],cnt,d[1111111],M;
vector<int> v[1111111];

void dfs(int nx,int ny,int f,int z) {
  if(f&&s[nx][ny]!='x') return;
  if(f) u[nx][ny]=1,cnt++;
  uu[nx][ny]=1;
  c[nx][ny]=z;
  if(f) {
    rep(i,8) {
      int x=nx+dx[i],y=ny+dy[i];
      if(check(n,m,x,y)&&!u[x][y]) dfs(x,y,f,z);
    }
  } else {
    rep(i,4) {
      int x=nx+dxx[i],y=ny+dyy[i];
      if(!check(n,m,x,y)) M=0;
      if(u[x][y]&&M>c[x][y]) M=c[x][y];
      if(check(n,m,x,y)&&!u[x][y]&&c[x][y]!=z) dfs(x,y,f,z);
    }
  }
}

P solve(int x) {
  P p=P(0,0);
  rep(i,v[x].size()) {
    P q=solve(v[x][i]);
    p.F+=q.S;
    if(q.F>q.S) p.S+=q.F;
    else p.S+=q.S;
  }
  p.F+=d[x];
  return p;
}

void Main() {
  cin >> n >> m;
  rep(i,n) cin >> s[i];
  int k=1;
  rep(i,n) {
    rep(j,m) {
      if(s[i][j]=='x'&&!u[i][j]) {
        cnt=0;
        dfs(i,j,1,k);
        d[k]=cnt;
        k++;
      }
    }
  }
  mem(uu);
  rep(i,n) {
    rep(j,m) {
      if(s[i][j]!='x'&&uu[i][j]==0) {
        M=MAX;
        dfs(i,j,0,-1);
        dfs(i,j,0,M);
      }
    }
  }
  int e[1111111];
  fill(e,e+1111111,MAX);
  rep(i,n) {
    rep(j,m) {
      if(!c[i][j]) continue;
      rep(k,4) {
        int x=i+dxx[k],y=j+dyy[k];
        if(!check(n,m,x,y)) e[c[i][j]]=0;
        else if(e[c[i][j]]>c[x][y]) e[c[i][j]]=c[x][y];
      }
    }
  }
  rep(i,1111111) {
    if(e[i]!=MAX) v[e[i]].pb(i);
  }
  P p=solve(0);
  pr(max(p.F,p.S));
}

int main() {
  ios::sync_with_stdio(0);cin.tie(0);
  Main();return 0;
}
0