結果

問題 No.157 2つの空洞
ユーザー GyuutoGyuuto
提出日時 2015-11-25 11:56:40
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 2,901 bytes
コンパイル時間 827 ms
コンパイル使用メモリ 88,916 KB
実行使用メモリ 4,352 KB
最終ジャッジ日時 2023-10-11 18:29:56
合計ジャッジ時間 1,736 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,352 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 1 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,352 KB
testcase_07 AC 2 ms
4,352 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 1 ms
4,352 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 2 ms
4,352 KB
testcase_12 AC 2 ms
4,352 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 2 ms
4,352 KB
testcase_18 AC 1 ms
4,352 KB
testcase_19 AC 2 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// Standard I/O
#include <iostream>
#include <sstream>
#include <cstdio>
// Standard Library
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <cmath>
// Template Class
#include <complex>
#include <string>
#include <vector>
#include <list>
#include <set>
#include <map>
#include <queue>
#include <stack>
// Container Control
#include <algorithm>

using namespace std;

#define rep( i, n ) for( int i = 0; i < n; ++i )
#define irep( i, n ) for( int i = n-1; i >= 0; --i )
#define reep( i, s, n ) for ( int i = s; i < n; ++i )
#define ireep( i, n, s ) for ( int i = n-1; i >= s; --i )
#define foreach(itr, x) for( typeof(x.begin()) itr = x.begin(); itr != x.end(); ++itr)

#define mp( a, b ) make_pair( a, b )
#define pb( a ) push_back( a )
#define all( v ) v.begin(), v.end()
#define fs first
#define sc second
#define vc vector

// for visualizer.html
double SCALE = 1.0;
double OFFSET_X = 0.0;
double OFFSET_Y = 0.0;
#define LINE(x,y,a,b) cerr << "line(" << SCALE*(x) + OFFSET_X << ","	\
	<< SCALE*(y) + OFFSET_Y << ","										\
	<< SCALE*(a) + OFFSET_X << ","										\
	<< SCALE*(b) + OFFSET_Y << ")" << endl;
#define CIRCLE(x,y,r) cerr << "circle(" << SCALE*(x) + OFFSET_X << ","	\
	<< SCALE*(y) + OFFSET_Y << ","										\
	<< SCALE*(r) << ")" << endl;

typedef long long ll;
typedef complex<double> Point;

typedef pair<int, int> pii;
typedef pair<int, pii> ipii;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector< vector<int> > vii;
typedef vector< vector<double> > vdd;

typedef vector<int>::iterator vi_itr;

const int IINF = 1 << 28;
const double INF = 1e30;
const double EPS = 1e-10;
const double PI = acos(-1.0);

// Direction : L U R D
const int dx[] = { -1, 0, 1, 0};
const int dy[] = { 0, -1, 0, 1 };

int W, H;
char field[20][20];

void func ( int x, int y, char c )
{
	field[y][x] = c;
	
	rep(i, 4){
		int ny = y + dy[i];
		int nx = x + dx[i];

		if( ny < 0 || nx < 0 || ny >= H || nx >= W ) continue;
		if( field[ny][nx] != '.' ) continue;

		func( nx, ny, c );
	}
}

int main()
{
	cin >> W >> H;
	rep(i, H) rep(j, W) cin >> field[i][j];

	char c = '1';
	rep(i, H) rep(j, W) if( field[i][j] == '.' ){
		func(j, i, c);
		++c;
	}

	typedef tuple<int, int, int> node;
	priority_queue<node, vector<node>, greater<node>> que;
	int used[20][20];
	rep(i, H) rep(j, W) used[i][j] = IINF;
	rep(i, H) rep(j, W) if( que.empty() && field[i][j] == '1' ){
		que.push( make_tuple(0, i, j) );
		break;
	}
	while( !que.empty() ){
		auto p = que.top(); que.pop();
		int x, y, c;
		tie(c, y, x) = p;

		if( field[y][x] == '2' ){
			cout << c << endl;
			break;
		}

		if( used[y][x] != IINF ) continue;
		used[y][x] = c;
		
		rep(i, 4){
			int ny = y + dy[i];
			int nx = x + dx[i];
			int nc = c + (field[ny][nx] == '#');

			if( nx < 0 || nx >= W || ny < 0 || ny >= H ) continue;
			if( used[ny][nx] < nc  ) continue;
			que.push( make_tuple(nc, ny, nx) );
		}
	}
}
0