結果

問題 No.157 2つの空洞
ユーザー ramia777ramia777
提出日時 2022-06-02 14:25:55
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 3 ms / 2,000 ms
コード長 5,064 bytes
コンパイル時間 1,614 ms
コンパイル使用メモリ 99,736 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-21 01:06:56
合計ジャッジ時間 2,018 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

// yukicodr
//No.157



//二次元可変配列
//vector <vector <int>> mass;
//vector <vector <int>> memo;
//vector <int> memo;


//#include "stdafx.h"
#include <iostream>
#include <vector>
#include <list>//list
#include <queue> //queue
#include <set> //tree
#include <map> //連想配列
#include <unordered_set> //hash
#include <unordered_map> //hash
#include <algorithm>
#include <iomanip>
#include <cstring>
//#include <bits/stdc++.h>

using namespace std;

#define FOR(x,to) for(x=0;x<to;x++)
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
#define FMAX(a,b)  ((a)>(b)?(a):(b))
#define FMIN(a,b)  ((a)<(b)?(a):(b))
#define FCEIL(a,b)  ((a)+(b)-1)/(b)

typedef unsigned long long ULL;
typedef signed long long SLL;





//昇順
int compare_up(const int * a, const int *b)
{
	if (*a > *b)
		return (1);
	else if (*a < *b)
		return (-1);
	return (0);
}

//降順
int compare_down(const int * a, const int *b)
{
	if (*a > *b)
		return (-1);
	else if (*a < *b)
		return (1);
	return (0);
}


int par[3005 * 3005];  //ルートの番号
int _rank[3005 * 3005]; //木の高さ
int flag[3005 * 3005];

// 経路圧縮
// 直列で連結しているような木はすべて直接ルートにつなぐようにする
int _find(int x)
{

	if (x == par[x])
	{
		//cout << "x=" << x << endl;
		return x;
	}
	else
	{
		int a;
		//cout << "in->" << endl;
		a = _find(par[x]);
		//cout << "<-out" << endl;




		//cout << "x=" << x << ",par[x]=" << par[x] << ",a=" << a << endl;


		//ルートを更新(圧縮)
		par[x] = a;

		//新しいルートを返す
		return par[x];
	}
}

//xとyが同じ集合かどうか
bool same(int x, int y)
{
	//ルートが同じかどうかを判定すればOK
	return	_find(x) == _find(y);
}

//初期化
//はじめは全部の頂点がルート
void init(int n)
{
	for (int i = 0; i < n; i++)
	{
		par[i] = i;
		_rank[i] = 0;
	}
}


// x と y を同じツリーにする
void _union(int x, int y)
{
	int root_x;
	int root_y;

	root_x = _find(x);
	root_y = _find(y);


	if (root_x == root_y) return; //最初から同じルートならすぐ返す


	//木の高さが高いほうの木の根に、低いほうの根をつなげる
	if (_rank[root_x] < _rank[root_y])
	{
		// x<yのとき
		par[root_x] = root_y;
	}
	else
	{
		// x>y または x==y のとき

		// yをxにつなげる
		par[root_y] = root_x;

		// 高さが同じときだけ 高さを変える必要がある
		if (_rank[root_x] == _rank[root_y])
			_rank[root_x]++; // つなげられた木の高さを1つふやす(全体の木の高さは1増える)
	}

}


typedef struct HOLE {
	int x;
	int y;
	char c;
	int flag;
}HOLE;

int W, H;

HOLE C[25][25];

queue<HOLE> _queue;

int k_flag = 1;
int main()
{

	cin >> W;
	cin >> H;

	for (int y = 0; y < H; y++)
	{
		for (int x = 0; x < W; x++)
		{
			cin >> C[y][x].c;
			C[y][x].x = x;
			C[y][x].y = y;
			C[y][x].flag = 0;
		}
	}

	//周辺は壁に囲まれていることが保証されている
	for (int y = 1; y < H-1; y++)
		for (int x = 1; x < W-1; x++)
		{

			if (!C[y][x].flag && C[y][x].c == '.')
			{
				//空洞探索する
				C[y][x].flag = k_flag;
				_queue.emplace(C[y][x]); //キューに入れる


				do
				{
					HOLE k;

					//先頭みる
					k = _queue.front();
					//先頭を取り除く
					_queue.pop();

					int target = k.x + 1;
					if (target < W - 1)
					{
						if (C[k.y][target].c == '.' && !C[k.y][target].flag)
						{
							C[k.y][target].flag = k_flag;
							_queue.emplace(C[k.y][target]);
						}
					}
					
					target = k.x - 1;
					if (target > 0)
					{
						if (C[k.y][target].c == '.' && !C[k.y][target].flag)
						{
							C[k.y][target].flag = k_flag;
							_queue.emplace(C[k.y][target]);
						}
					}

					target = k.y + 1;
					if (target < H - 1)
					{
						if (C[target][k.x].c == '.' && !C[target][k.x].flag)
						{
							C[target][k.x].flag = k_flag;
							_queue.emplace(C[target][k.x]);
						}
					}

					target = k.y - 1;
					if (target > 0)
					{
						if (C[target][k.x].c == '.' && !C[target][k.x].flag)
						{
							C[target][k.x].flag = k_flag;
							_queue.emplace(C[target][k.x]);
						}
					}
					

					//キューが0のときはtrue
					if (_queue.empty() == 1)
					{
						k_flag++;
						break;
					}
				} while (1);

			}

		}

	/*
for (int y = 0; y < H; y++)
{
	for (int x = 0; x < W; x++)
	{
		if(C[y][x].flag)
			cout << C[y][x].flag << ",";
		else
			cout << C[y][x].c << ",";

	}
	cout << endl;
}
*/


	//各1から各2へのXY座標の差分値の合計を比較
	int min = 99999;

	for (int y = 0; y < H; y++)
	{
		for (int x = 0; x < W; x++)
		{
			if (C[y][x].flag == 1)
			{
				int sx = C[y][x].x;
				int sy = C[y][x].y;

				for (int y = 0; y < H; y++)
				{
					for (int x = 0; x < W; x++)
					{
						if (C[y][x].flag == 2)
						{
							int dx = abs(sx - C[y][x].x);
							int dy = abs(sy - C[y][x].y);

							if (min > dx + dy)
								min = dx + dy;
						}
					}
				}

			}
			

		}
	}

	cout << min-1 << endl;




	return 0;

}

0