結果

問題 No.157 2つの空洞
ユーザー かにかに
提出日時 2015-11-14 16:44:14
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 3 ms / 2,000 ms
コード長 3,551 bytes
コンパイル時間 795 ms
コンパイル使用メモリ 98,724 KB
実行使用メモリ 4,372 KB
最終ジャッジ日時 2023-10-11 17:43:27
合計ジャッジ時間 1,672 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 1 ms
4,356 KB
testcase_03 AC 2 ms
4,352 KB
testcase_04 AC 2 ms
4,356 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,352 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 2 ms
4,372 KB
testcase_09 AC 1 ms
4,352 KB
testcase_10 AC 2 ms
4,352 KB
testcase_11 AC 2 ms
4,356 KB
testcase_12 AC 1 ms
4,352 KB
testcase_13 AC 2 ms
4,352 KB
testcase_14 AC 3 ms
4,352 KB
testcase_15 AC 2 ms
4,352 KB
testcase_16 AC 3 ms
4,356 KB
testcase_17 AC 2 ms
4,352 KB
testcase_18 AC 2 ms
4,352 KB
testcase_19 AC 2 ms
4,352 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _CRT_SECURE_NO_WARNINGS
#include <set>
#include <map>
#include <list>
#include <queue>
#include <stack>
#include <cmath>
#include <ctype.h>
#include <ctime>
#include <cstdio>
#include <vector>
#include <string>
#include <bitset>
#include <cctype>
#include <cstdlib>
#include <cstring>
#include <utility>
#include <numeric>
#include <complex>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <cassert>
#include <iostream>
#include <iterator>
#include <algorithm>
#define aLL(g) (g).begin(),(g).end()
#define REP(i, x, n) for(int i = x; i < n; i++)
#define rep(i,n) REP(i,0,n)
#define F(i,j,k) fill(i[0],i[0]+j*j,k)
#define P(p) cout<<(p)<<endl;
#define EXISt(s,e) ((s).find(e)!=(s).end())
#define INF 1<<25
#define MAX 100000000
#define pb push_back
#define mp make_pair
using namespace std;
typedef vector<int> vi;
typedef vector<long long> vl;
typedef vector<double> vd;
typedef pair<int, int> pii;
typedef pair<long, long> pll;
typedef long long ll;
int dx[] = { 0, 1, 0, -1 };
int dy[] = { -1, 0, 1, 0 };

int sttoi(std::string str) {
	int ret;
	std::stringstream ss; ss << str;
	ss >> ret;
	return ret;
}

bool sort_greater(const pair<string, int> &a, const pair<string, int> &b) {
	return a.second > b.second;
}

void solve() {
	int n, m;
	int mini = 10000000;
	cin >> n >> m;
	string s[20];
	int f[20][20];
	rep(i, m)rep(j, n){
		f[i][j] = 0;
	}
	rep(i, m){
		cin >> s[i];
	}
	bool first = false;
	rep(i, m)rep(j, n){
		if (s[i][j] == '.' && !first){
			stack<pair<int,int>> st;
			st.push(make_pair(j,i));
			while (!st.empty()){
				int x, y;
				x = st.top().first;
				y = st.top().second;
				f[y][x] = 1;
				st.pop();
				rep(k, 4){
					if (s[y + dy[k]][x + dx[k]] != '#' && f[y + dy[k]][x + dx[k]] != 1){
						st.push(make_pair(x + dx[k], y + dy[k]));
					}
				}
			}
			first = true;
		}
	}
	rep(i, m)rep(j, n){
		if (s[i][j] == '.' && f[i][j] != 1){
			stack<pair<int, int>> st;
			st.push(make_pair(j, i));
			while (!st.empty()){
				int x, y;
				x = st.top().first;
				y = st.top().second;
				f[y][x] = 2;
				st.pop();
				rep(k, 4){
					if (s[y + dy[k]][x + dx[k]] != '#' && f[y + dy[k]][x + dx[k]] != 2){
						st.push(make_pair(x + dx[k], y + dy[k]));
					}
				}
			}
			break; break;
		}
	}
	int distance[20][20];
	rep(i, m)rep(j, n){
		distance[i][j] = 100000;
	}
	stack<pair<int, int> > dist;
	rep(i, m)rep(j, n){
		if (f[i][j] == 1){
			dist.push(mp(j, i));
			distance[i][j] = 0;
			while (!dist.empty()){
				int x = dist.top().first;
				int y = dist.top().second;
				int d = distance[y][x];
				dist.pop();
				rep(k, 4){
					if (y + dy[k] >= 0 && y + dy[k] < m && x + dx[k] >= 0 && x + dx[k] < n){
						if (f[y + dy[k]][x + dx[k]] == 0){
							if (distance[y + dy[k]][x + dx[k]] > d + 1){
								dist.push(mp(x + dx[k], y + dy[k]));
								distance[y + dy[k]][x + dx[k]] = min(distance[y + dy[k]][x + dx[k]], d + 1);
							}
						}
						else if (f[y + dy[k]][x + dx[k]] == 1){
							if (distance[y + dy[k]][x + dx[k]] > d){
								dist.push(mp(x + dx[k], y + dy[k]));
								distance[y + dy[k]][x + dx[k]] = min(distance[y + dy[k]][x + dx[k]], d);
							}
						}
						else if (f[y + dy[k]][x + dx[k]] == 2){
							if (distance[y + dy[k]][x + dx[k]] > d ){
								dist.push(mp(x + dx[k], y + dy[k]));
								distance[y + dy[k]][x + dx[k]] = min(distance[y + dy[k]][x + dx[k]], d);
							}
						}
					}
				}
			}
			break;
		}
	}
	rep(i, m)rep(j, n){
		if (f[i][j] == 2){
			mini = min(mini, distance[i][j]);
		}
	}
	P(mini);
}

int main() {
	solve();
	return 0;
}
0