結果
| 問題 |
No.157 2つの空洞
|
| コンテスト | |
| ユーザー |
ty70
|
| 提出日時 | 2015-05-27 11:11:55 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
MLE
|
| 実行時間 | - |
| コード長 | 2,801 bytes |
| コンパイル時間 | 708 ms |
| コンパイル使用メモリ | 97,112 KB |
| 実行使用メモリ | 814,684 KB |
| 最終ジャッジ日時 | 2024-07-06 10:36:44 |
| 合計ジャッジ時間 | 5,894 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 7 MLE * 1 -- * 8 |
ソースコード
#include <iostream>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <algorithm> // require sort next_permutation count __gcd reverse etc.
#include <cstdlib> // require abs exit atof atoi
#include <cstdio> // require scanf printf
#include <functional>
#include <numeric> // require accumulate
#include <cmath> // require fabs
#include <climits>
#include <limits>
#include <cfloat>
#include <iomanip> // require setw
#include <sstream> // require stringstream
#include <cstring> // require memset
#include <cctype> // require tolower, toupper
#include <fstream> // require freopen
#include <ctime> // require srand
#define rep(i,n) for(int i=0;i<(n);i++)
#define ALL(A) A.begin(), A.end()
#define INF 1<<10
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<P, int> PI;
const int dr[4] = {-1, 0, 1, 0 };
const int dc[4] = { 0, 1, 0,-1 };
int W, H;
char cave[22][22];
int used[22][22];
void dfs (int r, int c, char ch1, char ch2 ){ // 現在位置 (r, c ) 通過可能文字 ch1 上書き文字 ch2
if (r < 0 || r >= H || c < 0 || c >= W ) return;
if (cave[r][c] != ch1 ) return;
cave[r][c] = ch2;
rep (k, 4 )
dfs (r+dr[k], c+dc[k], ch1, ch2 );
}
int bfs (int r, int c, char ch1, char ch2 ){ // 現在位置 (r, c ) 通過可能文字 ch1 探索文字 ch2
memset (used, -1, sizeof (used ) );
used[r][c] = 0;
queue<PI> que;
que.push (PI(P (r, c ), 0 ) );
while (!que.empty() ){
PI curr = que.front(); que.pop();
int cr = curr.first.first;
int cc = curr.first.second;
int cn = curr.second;
used[cr][cc] = cn;
rep (k, 4 ){
int nr = cr + dr[k];
int nc = cc + dc[k];
if (nr < 0 || nr >= H || nc < 0 || nc >= W ) continue;
if (cave[nr][nc] == ch2 ) return cn;
if (cave[nr][nc] != ch1 && cave[nr][nc] != '1' ) continue;
if (used[nr][nc] == -1 || used[nr][nc] > used[r][c] + 1 ){
que.push (PI( P (nr, nc ), cn + 1 ) );
} // end if
} // end rep
} // end while
return -1;
}
void disp_cave (void ){
rep (i, H ){
rep (j, W ){
cerr << cave[i][j];
} // end rep
cerr << endl;
} // end rep
}
void disp_used (void ){
cerr << endl;
rep (i, H ){
rep (j, W ){
cerr << setw(2) << used[i][j];
} // end rep
cerr << endl;
} // end rep
}
int main()
{
memset (cave, 0, sizeof (cave ) );
ios_base::sync_with_stdio(0);
cin >> W >> H;
rep (i, H ) rep (j, W ) cin >> cave[i][j];
int cnt = 0;
rep (i, H ) rep (j, W )
if (cave[i][j] == '.' ){
dfs (i, j, '.', (char)('1'+cnt) );
cnt++;
} // end if
// disp_cave ();
int res = INF;
rep (i, H ) rep (j, W ) if (cave[i][j] == '1' ){
int curr = bfs (i, j, '#', '2' );
res = min (res, curr );
// disp_used ();
} // end if
cout << res << endl;
return 0;
}
ty70