#include <iostream> #include <queue> #include <utility> #include <string.h> #include <algorithm> #include <fstream> using namespace std; const int dy[4] = {-1, 1, 0, 0}; const int dx[4] = {0, 0, -1, 1}; int checkfire[1001][1001]; int checkperson[1001][1001]; char map[1001][1001]; queue<pair<int, int>> fq; queue<pair<int, int>> pq; int fire(int m, int n) { while(!fq.empty()) { int y = fq.front().first; int x = fq.front().second; fq.pop(); if(y == 0 || x == 0 || y == m - 1 || x == n - 1){ return checkfire[y][x]; } for(int i = 0; i < 4; i++){ int yy = y + dy[i]; int xx = x + dx[i]; if(yy >= 0 && yy < m && xx >= 0 && xx < n){ if(map[yy][xx] == '.' && checkfire[yy][xx] == 0){ checkfire[yy][xx] = checkfire[y][x] + 1; fq.push(make_pair(yy, xx)); } } } } return 1e9; // 불이 벽에 도달하지 못하면 큰 값을 반환 } int person( int m, int n) { while(!pq.empty()) { int y = pq.front().first; int x = pq.front().second; pq.pop(); if(y == 0 || x == 0 || y == m - 1 || x == n - 1){ return checkperson[y][x]; } for(int i = 0; i < 4; i++){ int yy = y + dy[i]; int xx = x + dx[i]; if(yy >= 0 && yy < m && xx >= 0 && xx < n){ if(map[yy][xx] == '.' && checkperson[yy][xx] == 0){ checkperson[yy][xx] = checkperson[y][x] + 1; pq.push(make_pair(yy, xx)); } } } } return 1e9; // 사람이 도달하지 못하는 경우 큰 값을 반환 } int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); #ifdef JH_DEBUG static ifstream in( "5427_input.txt"); cin.rdbuf( in.rdbuf()); #endif int t; cin >> t; while(t--){ int n, m; int mf = 1e9, mp = 1e9; cin >> n >> m; memset(checkfire, 0, sizeof(checkfire)); // 불이 여러개 있을 경우 초기화 memset(checkperson, 0, sizeof(checkperson)); while( !fq.empty()) fq.pop(); while( !pq.empty()) pq.pop(); for(int i = 0; i < m; i++){ for(int j = 0; j < n; j++){ cin >> map[i][j]; if (map[i][j] == '*') { fq.push(make_pair(i, j)); checkfire[i][j] = 1; } else if (map[i][j] == '@') { pq.push(make_pair(i, j)); checkperson[i][j] = 1; } } } mf = fire(m, n); mp = person(m, n); if(mf <= mp) if(mf == 1 && mp == 1){ // 불과 사람이 벽에서 시작한 경우 예외처리 cout << mp << '\n'; }else{ cout << "IMPOSSIBLE" << '\n'; } else cout << mp << '\n'; } }
1 100 100 ##################################################.################################################# #..................................................................................................# #..................................................................................................# #..................................................................................................# #..................................................................................................# #..................................................................................................# #..................................................................................................# #..................................................................................................# #..................................................................................................# #..................................................................................................# #..................................................................................................# #..................................................................................................# #..................................................................................................# #..................................................................................................# #..................................................................................................# #..................................................................................................# #..................................................................................................# #..................................................................................................# #..................................................................................................# #..................................................................................................# #..................................................................................................# #..................................................................................................# #..................................................................................................# #..................................................................................................# #..................................................................................................# #..................................................................................................# #..................................................................................................# #..................................................................................................# #..................................................................................................# #..................................................................................................# #..................................................................................................# #..................................................................................................# #..................................................................................................# #..................................................................................................# #..................................................................................................# #..................................................................................................# #..................................................................................................# #..................................................................................................# #..................................................................................................# #..................................................................................................# #..................................................................................................# #..................................................................................................# #..................................................................................................# #..................................................................................................# #..................................................................................................# #..................................................................................................# #..................................................................................................# #..................................................................................................# #..................................................................................................# #..................................................................................................# #..................................................................................................# #..................................................................................................# #..................................................................................................# #..................................................................................................# #..................................................................................................# #..................................................................................................# #..................................................................................................# #..................................................................................................# #..................................................................................................# #..................................................................................................# #..................................................................................................# #..................................................................................................# #..................................................................................................# #..................................................................................................# #..................................................................................................# #..................................................................................................# #..................................................................................................# #..................................................................................................# #..................................................................................................# #..................................................................................................# #..................................................................................................# #..................................................................................................# #..................................................................................................# #..................................................................................................# #..................................................................................................# #..................................................................................................# #..................................................................................................# #..................................................................................................# #..................................................................................................# #..................................................................................................# #..................................................................................................# #..................................................................................................# #..................................................................................................# #..................................................................................................# #..................................................................................................# #..................................................................................................# #..................................................................................................# #..................................................................................................# #..................................................................................................# #..................................................................................................# #..................................................................................................# #..................................................................................................# #..................................................................................................# #..................................................................................................# #..................................................................................................# #..................................................................................................# #..................................................................................................# #..................................................................................................# #*************************************************@************************************************# ####################################################################################################