結果
| 問題 |
No.157 2つの空洞
|
| コンテスト | |
| ユーザー |
oxyshower
|
| 提出日時 | 2020-01-12 14:06:01 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 3 ms / 2,000 ms |
| コード長 | 2,544 bytes |
| コンパイル時間 | 2,361 ms |
| コンパイル使用メモリ | 178,452 KB |
| 実行使用メモリ | 5,248 KB |
| 最終ジャッジ日時 | 2024-11-29 00:56:20 |
| 合計ジャッジ時間 | 2,776 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 16 |
ソースコード
#include<bits/stdc++.h>
using namespace std;
#ifdef LOCAL_DEBUG
#include "LOCAL_DEBUG.hpp"
#endif
#define int long long
template<class T> vector<T> make_vec(size_t a) { return vector<T>(a); }
template<class T, class... Ts> auto make_vec(size_t a, Ts... ts) {
return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}
template<class T, class V>
typename enable_if<is_class<T>::value == 0>::type fill(T &t, const V &v) {
t = v;
}
template<class T, class V>
typename enable_if<is_class<T>::value != 0>::type fill(T &t, const V &v){
for (auto &e : t) fill(e, v);
}
// auto v = make_vec<int>(h, w);
// fill(v, 0);
const int INF = 1LL << 60;
signed main(){
int h, w; cin >> w >> h;
auto c = make_vec<char>(h, w);
for(int i = 0; i < h; i++){
for(int j = 0; j < w; j++){
cin >> c[i][j];
}
}
auto group = make_vec<int>(h, w);
fill(group, -1);
int id = 1;
function< void(int,int) > dfs =
[&](int y, int x){
int dx[4] = {1,0,-1,0}, dy[4] = {0,-1,0,1};
for(int i = 0; i < 4; i++){
int ny = y + dy[i];
int nx = x + dx[i];
if(ny < 0 || h <= ny || nx < 0 || w <= nx) continue;
if(c[ny][nx] == '.' && group[ny][nx] == -1){
group[ny][nx] = id;
dfs(ny, nx);
}
}
};
for(int i = 0; i < h; i++){
for(int j = 0; j < w; j++){
if(c[i][j] == '.' && group[i][j] == -1){
group[i][j] = id;
dfs(i, j);
id++;
}
}
}
auto dp = make_vec<int>(h, w);
fill(dp, INF);
function< void(int,int) > dfs2 =
[&](int y, int x){
int dx[4] = {1,0,-1,0}, dy[4] = {0,-1,0,1};
for(int i = 0; i < 4; i++){
int ny = y + dy[i];
int nx = x + dx[i];
if(ny < 0 || h <= ny || nx < 0 || w <= nx) continue;
if(c[ny][nx] == '.'){
if(group[ny][nx] == 1){
if(dp[ny][nx] != 0){
dp[ny][nx] = 0;
dfs2(ny, nx);
}
}else{
if(dp[ny][nx] > dp[y][x] + 1){
dp[ny][nx] = dp[y][x] + 1;
dfs2(ny, nx);
}
}
}else{
if(dp[ny][nx] > dp[y][x] + 1){
dp[ny][nx] = dp[y][x] + 1;
dfs2(ny, nx);
}
}
}
};
for(int i = 0; i < h; i++){
for(int j = 0; j < w; j++){
if(group[i][j] == 1){
dp[i][j] = 0;
dfs2(i, j);
}
}
}
int ans = INF;
for(int i = 0; i < h; i++){
for(int j = 0; j < w; j++){
if(group[i][j] == 2){
ans = min(ans, dp[i][j]);
}
}
}
cout << ans-1 << endl;
return 0;
}
oxyshower