結果

問題 No.157 2つの空洞
ユーザー ttakanottakano
提出日時 2017-11-23 15:51:29
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,609 bytes
コンパイル時間 1,570 ms
コンパイル使用メモリ 150,096 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-18 03:09:33
合計ジャッジ時間 2,397 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 1 ms
4,376 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"
using namespace std;
#define print(x) cout<<x<<endl;
#define rep(i,a,b) for(int i=a;i<b;i++)
#define REP(i,a) for(int i=0;i<a;i++)
typedef long long ll;
typedef pair<int, int> PI;
typedef pair<int, PI> V;
typedef vector<int> VE;
const ll mod = 1000000007;

int w,h;
char c[20][20];
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
int check[20][20];

int dfs(int y,int x,char h){
  c[y][x]=h;
  REP(i,4){
    int nx=x+dx[i];
    int ny=y+dy[i];
    if(0<=nx&&nx<w&&0<=ny&&ny<h&&c[ny][nx]=='.')dfs(ny,nx,h);
  }
  return 0;
}

int hole_diveide(){
  int res=0;
  REP(i,h)REP(j,w){
    if(c[i][j]=='.'){
      char h=res?'b':'a';
      dfs(i,j,h);
      res++;
    }
  }
  return 0;
}

int graph(){
  REP(i,h){
    REP(j,w){
      cout<<c[i][j];
    }
    cout<<endl;
  }
  return 0;
}

int ok(int y, int x){
  return 0<=x&&x<w&&y<h&&0<=y&&c[y][x]!='a'&&check[y][x]==50;
}

int main(){
  queue<PI> que;
  cin>>w>>h;
  REP(i,h)REP(j,w)cin>>c[i][j];
  REP(i,h)REP(j,w)check[i][j]=50;
  hole_diveide();
  REP(i,h)REP(j,w){
    if(c[i][j]=='a'){
      REP(k,4){
        int nx=j+dx[k];
        int ny=i+dy[k];
        if(ok(ny,nx)) {
          check[ny][nx]=0;
          que.push(make_pair(ny,nx));
        }
      }
    }
  }
  while(!que.empty()){
    int y=que.front().first;
    int x=que.front().second;
    if(c[y][x]=='b'){
      print(check[y][x]);
      return 0;
    }
    que.pop();
    REP(k,4){
      int nx=x+dx[k];
      int ny=y+dy[k];
      if(ok(ny,nx)) {
        check[ny][nx]=check[y][x]+1;
        que.push(make_pair(ny,nx));
      }
    }
  }
  print("error");
  return 0;
}
0