結果

問題 No.157 2つの空洞
ユーザー kongarishisyamokongarishisyamo
提出日時 2016-06-02 17:47:19
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,928 bytes
コンパイル時間 1,134 ms
コンパイル使用メモリ 69,472 KB
実行使用メモリ 4,388 KB
最終ジャッジ日時 2023-07-29 17:47:16
合計ジャッジ時間 2,404 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include<iostream>
#include<queue>
#include<string>
#include<cstdlib>
#include<algorithm>

using namespace std;

#define HMAX 20
#define WMAX 20

string map[HMAX];
int W,H;
int dx[]={0,0,1,-1};
int dy[]={1,-1,0,0};

void fill_char(int x,int y,char c){
  char b=map[y][x];
  queue<int> qy,qx;
  qx.push(x);
  qy.push(y);
  map[y][x]=c;
  while(!qx.empty()){
    int nx=qx.front();qx.pop();
    int ny=qy.front();qy.pop();
    for(int i=0;i<4;i++){
      int fy=ny+dy[i];
      int fx=nx+dx[i];
      if(fx>=0&&fx<W&&fy>=0&&fy<H&&map[fy][fx]==b){
	qy.push(fy);
	qx.push(fx);
	map[fy][fx]=c;
      }
    }
  }
}

int length(int x,int y,char g){
  int ret=WMAX*HMAX+1;
  int cmap[HMAX][WMAX];
  queue<int> qy,qx,qc;
  qx.push(x);
  qy.push(y);
  qc.push(1);

  for(int i=0;i<H;i++){
    for(int j=0;j<W;j++){
      cmap[i][j]=HMAX*WMAX+1;
    }
  }

  cmap[y][x]=0;
  while(!qx.empty()){
    //cout<<qx.size()<<endl;
    int nx=qx.front();qx.pop();
    int ny=qy.front();qy.pop();
    int nc=qc.front();qc.pop();
    if(map[ny][nx]==g){
      //cout<<ny<<","<<nx<<","<<ret<<","<<nc<<","<<g<<","<<map[ny][nx]<<endl;
      ret=min(ret,cmap[ny][nx]);
    }
    for(int i=0;i<4;i++){
      int fy=ny+dy[i];
      int fx=nx+dx[i];
      if(fx>=0&&fx<W&&fy>=0&&fy<H){
        if(map[fy][fx]=='#'&&cmap[fy][fx]>cmap[ny][nx]+1){
	  qy.push(fy);
	  qx.push(fx);
          qc.push(nc+1);
	  cmap[fy][fx]=cmap[ny][nx]+1;
	}
	else if(map[fy][fx]!='#'&&cmap[fy][fx]>cmap[ny][nx]){
	  qy.push(fy);
	  qx.push(fx);
          qc.push(nc);
	  cmap[fy][fx]=cmap[ny][nx];
	}
      }
    }
  }

  //for(int i=0;i<H;i++){
  //for(int j=0;j<W;j++){
  //cout<<cmap[i][j]<<" ";
  // }
  //cout<<endl;
  //}
    return ret;
}

int main(){

  cin>>W>>H;
  for(int i=0;i<H;i++){
    cin>>map[i];
  }

  for(int i=0;i<H;i++){
    for(int j=0;j<W;j++){
      if(map[i][j]=='.'){
	fill_char(j,i,'x');
	cout<<length(j,i,'.')<<endl;
	return 0;
      }
    }
  } 
}
0