結果
| 問題 |
No.157 2つの空洞
|
| コンテスト | |
| ユーザー |
peroon
|
| 提出日時 | 2019-05-31 09:16:05 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 2 ms / 2,000 ms |
| コード長 | 3,041 bytes |
| コンパイル時間 | 1,765 ms |
| コンパイル使用メモリ | 180,744 KB |
| 実行使用メモリ | 5,376 KB |
| 最終ジャッジ日時 | 2024-09-17 16:10:54 |
| 合計ジャッジ時間 | 2,568 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 16 |
ソースコード
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
#define FOR(i,a,b) for(ll i=(a);i<(b);++i)
#define ALL(v) (v).begin(), (v).end()
#define p(s) cout<<(s)<<endl
#define p2(s, t) cout << (s) << " " << (t) << endl
#define br() p("")
#define pn(s) cout << (#s) << " " << (s) << endl
#define p_yes() p("Yes")
#define p_no() p("No")
const ll mod = 1e9 + 7;
const ll inf = 1e18;
template < typename T >
void vprint(T &V){
for(auto v : V){
cout << v << " ";
}
cout << endl;
}
struct Point{
ll x;
ll y;
Point(ll x, ll y): x(x), y(y) {}
Point(){}
bool operator<(const Point &another) const
{
if(x == another.x){
return y < another.y;
}else{
return x < another.x;
}
}
};
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, 1, 0, -1};
vector<string> S;
ll W, H;
void bfs(ll x, ll y, char c){
queue<Point> que;
que.push(Point(x, y));
S[y][x] = c;
while(!que.empty()){
auto p = que.front();
que.pop();
FOR(i, 0, 4){
ll tx = p.x + dx[i];
ll ty = p.y + dy[i];
if(0<=tx && tx<W && 0<=ty && ty<H && S[ty][tx]=='.'){
S[ty][tx] = c;
que.push(Point(tx, ty));
}
}
}
}
ll manhattan_distance(Point a, Point b){
return abs(a.x - b.x) + abs(a.y - b.y);
}
int main(){
cin.tie(0);
ios::sync_with_stdio(false);
// input
cin >> W >> H;
S.resize(H);
FOR(i, 0, H){
cin >> S[i];
}
// 空洞を見つけてRに塗る
FOR(y, 0, H){
FOR(x, 0, W){
if(S[y][x]=='.'){
bfs(x, y, 'R');
goto HERE;
}
}
}
HERE:
// 空洞を見つけてGに塗る
FOR(y, 0, H){
FOR(x, 0, W){
if(S[y][x]=='.'){
bfs(x, y, 'G');
goto THERE;
}
}
}
THERE:
set<Point> se_R;
set<Point> se_G;
// Rに隣接する壁をse_Rに入れる
// Gも同様
// それは掘る始点となる
FOR(y, 0, H){
FOR(x, 0, W){
FOR(i, 0, 4){
ll tx = x + dx[i];
ll ty = y + dy[i];
if(0<=tx && tx<W && 0<=ty && ty<H && S[ty][tx]=='R'){
se_R.insert(Point(x, y));
}
if(0<=tx && tx<W && 0<=ty && ty<H && S[ty][tx]=='G'){
se_G.insert(Point(x, y));
}
}
}
}
// Rの始点、Gの始点の各組み合わせのマンハッタン距離を求め、それらの内の最短距離を求める
ll min_md = inf;
for(Point p0 : se_R){
for(Point p1 : se_G){
ll md = manhattan_distance(p0, p1);
chmin(min_md, md);
}
}
p(min_md + 1);
return 0;
}
peroon