結果
| 問題 |
No.157 2つの空洞
|
| コンテスト | |
| ユーザー |
fiord
|
| 提出日時 | 2015-07-08 17:04:50 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 2 ms / 2,000 ms |
| コード長 | 1,253 bytes |
| コンパイル時間 | 661 ms |
| コンパイル使用メモリ | 70,764 KB |
| 実行使用メモリ | 6,944 KB |
| 最終ジャッジ日時 | 2024-07-08 01:43:26 |
| 合計ジャッジ時間 | 1,181 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 16 |
ソースコード
#include <string>
#include <iostream>
#include <vector>
#include <utility>
#include <queue>
std::vector<std::string> c;
int w,h;
int x[]={1,-1,0,0},y[]={0,0,1,-1};
std::queue<std::pair<int,int> > next;
bool check(int nx,int ny){
if(!(0<=nx&&nx<w)) return false;
if(!(0<=ny&&ny<h)) return false;
return true;
}
void groupdfs(int nh,int nw,char group){
c[nh][nw]=group;
if(group=='0') next.push(std::make_pair(nh,nw));
for(int i=0;i<4;i++){
if(!check(nw+x[i],nh+y[i])) continue;
if(c[nh+y[i]][nw+x[i]]=='.') groupdfs(nh+y[i],nw+x[i],group);
}
return;
}
int main(){
std::cin>>w>>h;
std::string in;
for(int i=0;i<h;i++){
std::cin>>in;
c.push_back(in);
}
char group='0';
for(int i=0;i<h;i++){
for(int j=0;j<w;j++){
if(c[i][j]=='.'){
groupdfs(i,j,group);
group='g';
}
}
}
while(!next.empty()){
int nx=next.front().second,ny=next.front().first;
next.pop();
for(int i=0;i<4;i++){
if(!check(nx+x[i],ny+y[i])) continue;
if(c[ny+y[i]][nx+x[i]]=='#'){
c[ny+y[i]][nx+x[i]]=c[ny][nx]+1;
next.push(std::make_pair(ny+y[i],nx+x[i]));
}
else if(c[ny+y[i]][nx+x[i]]=='g'){
int ans=0;
for(char a='0';a<c[ny][nx];a++) ans++;
std::cout<<ans<<std::endl;
return 0;
}
}
}
return 0;
}
fiord