結果
| 問題 |
No.157 2つの空洞
|
| コンテスト | |
| ユーザー |
koyopro
|
| 提出日時 | 2015-09-21 19:31:48 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 3,209 bytes |
| コンパイル時間 | 761 ms |
| コンパイル使用メモリ | 80,432 KB |
| 実行使用メモリ | 11,248 KB |
| 最終ジャッジ日時 | 2024-07-19 08:27:03 |
| 合計ジャッジ時間 | 4,101 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | -- * 4 |
| other | AC * 1 TLE * 1 -- * 14 |
ソースコード
#include<iostream>
#include<math.h>
#include<vector>
#include <array>
#include <algorithm>
#include <queue>
#include <numeric>
#include <map>
using namespace std;
#define int long long
#define FOR(i, a, b) for(int i=(a);i<(b);i++)
#define RFOR(i, a, b) for(int i=(b-1);i>=(a);i--)
#define REP(i, n) for(int i=0; i<(n); i++)
#define RREP(i, n) for(int i=(n-1); i>=0; i--)
#define ALL(a) (a).begin(),(a).end()
#define UNIQUE_SORT(l) sort(ALL(l)); l.erase(unique(ALL(l)), l.end());
#define CONTAIN(a, b) find(ALL(a), (b)) != (a).end()
#define array2(type, x, y) array<array<type, y>, x>
#define vector2(type) vector<vector<type> >
#define out(...) //printf(__VA_ARGS__)
typedef pair<int, int> pos;
int pos::*x = &pos::first;
int pos::*y = &pos::second;
int dxy[] = {0, 1, 0, -1, 0};
/*================================*/
#define MAX 20
int W, H;
pos start;
static bool isCombine(bool map[MAX][MAX]) {
// マップ内の空洞数
int space = 0;
REP(h,H)REP(w,W) {
if (map[w][h]) space++;
}
// スタートから繋がってる場所カウント
queue<pos> que;
que.push(start);
vector<pos> checked;
checked.push_back(start);
int space2 = 1;
while (que.size()) {
pos p = que.front();
que.pop();
REP(i,4) {
pos np;
np.*x = p.first + dxy[i];
np.*y = p.second + dxy[i+1];
if (CONTAIN(checked, np)) continue;
if (map[np.*x][np.*y]) {
checked.push_back(np);
que.push(np);
space2++;
}
}
}
//cout << "space: " << space << "," << space2 << endl;
return (space == space2);
}
void expand(bool map[MAX][MAX]) {
// スタートから繋がってる場所
queue<pos> que;
que.push(start);
vector<pos> checked;
checked.push_back(start);
while (que.size()) {
pos p = que.front();
que.pop();
REP(i,4) {
pos np;
np.*x = p.first + dxy[i];
np.*y = p.second + dxy[i+1];
if (CONTAIN(checked, np)) continue;
if (map[np.*x][np.*y]) {
checked.push_back(np);
que.push(np);
}
}
}
// 空洞の一つ隣を全部空ける
REP(j,checked.size()) {
pos p = checked[j];
REP(i,4) {
pos np;
np.*x = p.first + dxy[i];
np.*y = p.second + dxy[i+1];
if (np.*x < 0 || W <= np.*x) continue;
if (np.*y < 0 || H <= np.*y) continue;
if (CONTAIN(checked, np)) continue;
map[np.*x][np.*y] = true; // 無条件で空洞に
}
}
}
signed main()
{
cin >> W >> H;
bool map[MAX][MAX];
REP(h,H) {
REP(w,W) {
char c;
cin >> c;
// 空洞はtrue
map[w][h] = (c == '.');
if (start.*x == 0 && map[w][h]) {
start.*x = w;
start.*y = h;
}
}
}
int ccnt = 1;
while (true) {
expand(map);
if (isCombine(map)) break;
ccnt++;
if (ccnt > 20) break;
}
cout << ccnt << endl;
return 0;
}
koyopro