結果

問題 No.348 カゴメカゴメ
ユーザー kzyKTkzyKT
提出日時 2016-02-26 23:45:46
言語 C++11
(gcc 11.4.0)
結果
TLE  
実行時間 -
コード長 2,614 bytes
コンパイル時間 1,439 ms
コンパイル使用メモリ 164,792 KB
実行使用メモリ 59,416 KB
最終ジャッジ日時 2023-10-24 10:45:29
合計ジャッジ時間 8,228 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 TLE -
testcase_01 -- -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
testcase_45 -- -
testcase_46 -- -
testcase_47 -- -
testcase_48 -- -
testcase_49 -- -
testcase_50 -- -
testcase_51 -- -
testcase_52 -- -
権限があれば一括ダウンロードができます

ソースコード

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};
typedef pair<int,int> P;

int n,m;
string s[1111];
int c[1111][1111],u[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++;
  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 {
    for(int i=-1; i<2; i++) {
      for(int j=-1; j<2; j++) {
        if(i&&j) continue;
        int x=nx+i,y=ny+j;
        if(!check(n,m,x,y)) M=0;
        if(u[x][y]) M=min(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;
    p.S+=max(q.F,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++;
      }
    }
  }
  rep(i,n) {
    rep(j,m) {
      if(s[i][j]!='x'&&c[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,8) {
        int x=i+dx[k],y=j+dy[k];
        if(!check(n,m,x,y)) e[c[i][j]]=0;
        else e[c[i][j]]=min(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