結果
| 問題 |
No.157 2つの空洞
|
| コンテスト | |
| ユーザー |
latte0119
|
| 提出日時 | 2015-02-27 00:17:00 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 8 ms / 2,000 ms |
| コード長 | 1,395 bytes |
| コンパイル時間 | 631 ms |
| コンパイル使用メモリ | 51,072 KB |
| 実行使用メモリ | 6,944 KB |
| 最終ジャッジ日時 | 2024-06-23 21:53:58 |
| 合計ジャッジ時間 | 1,383 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 16 |
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:22:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
22 | scanf("%d%d",&w,&h);
| ~~~~~^~~~~~~~~~~~~~
main.cpp:24:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
24 | scanf("%s",fld[i]);
| ~~~~~^~~~~~~~~~~~~
ソースコード
#include<cstdio>
#include<vector>
#include<algorithm>
#include<queue>
using namespace std;
const int dx[]={0,-1,0,1},dy[]={-1,0,1,0};
vector<pair<int,int> >s;
char fld[32][32];
int w,h;
const int INF=1e9;
struct data{
int y,x,cost;
};
void dfs(int y,int x){
if(y<0||y>=h||x<0||x>=w)return;
if(fld[y][x]=='#')return;
fld[y][x]='#';
for(int i=0;i<4;i++)dfs(y+dy[i],x+dx[i]);
s.push_back(make_pair(y,x));
}
int main(){
scanf("%d%d",&w,&h);
for(int i=0;i<h;i++)
scanf("%s",fld[i]);
for(int i=0;i<h;i++){
bool flag=false;
for(int j=0;j<w;j++){
if(fld[i][j]=='.'){
flag=true;
dfs(i,j);
break;
}
}
if(flag)break;
}
int d[32][32],ans=INF;
for(int i=0;i<s.size();i++){
int sx=s[i].second,sy=s[i].first;
fill_n(*d,32*32,INF);
queue<data>Q;
Q.push((data){sy,sx,0});
while(Q.size()){
data a=Q.front();Q.pop();
if(a.y<0||a.y>=h||a.x<0||a.x>=w)continue;
if(d[a.y][a.x]!=INF)continue;
d[a.y][a.x]=a.cost;
if(fld[a.y][a.x]=='.'){
ans=min(ans,a.cost-1);
}
for(int i=0;i<4;i++){
Q.push((data){a.y+dy[i],a.x+dx[i],a.cost+1});
}
}
}
printf("%d\n",ans);
return 0;
}
latte0119