ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 불!
    백준알고리즘 2019. 8. 26. 18:41
    #include<iostream>
    #include<cstdio>
    #include<vector>
    #include<queue>
    #include<utility>
    
    using namespace std;
    struct man {
    
    	int x, y;
    
    	bool life = true;
    
     
    
    };
    man ji;
    int n, m;
    int map[1000][1000];
    int d[1000][1000];
    bool quit = false;
    bool dead = false;
    int dx[] = { 0,0,1,-1 };
    int dy[] = { 1,-1,0,0 };
    int timee = 0;
    vector<pair<int, int>> mm;
    vector<pair<int, int>> f;
    
    void bfs2() {
    	queue<pair<int, int>> q;
    	for (int i = 0; i < mm.size(); i++) {
    		q.push({ mm[i].first, mm[i].second });
    	}
    	if (mm.empty()) dead = true;
    	mm.clear();
    	while (!q.empty()) {
    		int x = q.front().first; int y = q.front().second;
    		q.pop();
    		for (int i = 0; i < 4; i++) {
    			int nx = x + dx[i]; int ny = y + dy[i];
    			if (nx >= 0 && nx < n && ny >= 0 && ny < m) {
    				if (map[nx][ny] == 0 && d[nx][ny] == 0) {
    					d[nx][ny] = d[x][y] + 1;
    					mm.push_back({ nx,ny });
    					if (nx == 0 || nx == n - 1 || ny == m - 1 || ny == 0) {
    						if (map[nx][ny] != 2 &&map[nx][ny] == 0) {
    							ji.x = nx; ji.y = ny;
    							quit = true;
    							timee = d[nx][ny];
    						}
    					}
    				}
    			}
    		}
    	}
    }
    
    void bfs() {
    	queue<pair<int, int>> q;
    	for (int i = 0; i < f.size(); i++) {
    		q.push({ f[i].first, f[i].second });
    		d[f[i].first][f[i].second] = true;
    	}
    	f.clear();
    		while (!q.empty()) {
    			int x = q.front().first; int y = q.front().second;
    			q.pop();
    			for (int i = 0; i < 4; i++) {
    				int nx = x + dx[i]; int ny = y + dy[i];
    				if (nx >= 0 && nx < n && ny >= 0 && ny < m) {
    					if (map[nx][ny] == 0) {
    						map[nx][ny] = 2;
    						f.push_back({ nx,ny });
    						if (nx == n - 1 || nx == 0 || ny == m - 1 || ny == 0) {
    							if (ji.x == nx && ji.y == ny) {
    								quit = false;
    							}
    						}
    					}
    				}
    			}
    		}
    }
    int main() {
    	cin >> n >> m;
    	for (int i = 0; i < n; i++) {
    		for (int j = 0; j < m; j++) {
    			char go;
    			cin >> go;
    			if (go == '#')
    			{
    				map[i][j] = 3;
    			}
    			if (go == 'J')
    			{
    				d[i][j] = 1;
    				ji.x = i; ji.y = j;
    				mm.push_back({ i,j });
    			}
    			if (go == 'F') {
    				map[i][j] = 2;
    				f.push_back({ i,j });
    			}
    			if (go == '.')
    				map[i][j] = 0;
    		}
    	}
    	if(ji.x == 0 || ji.x == n-1 || ji.y == 0 ||ji.y ==m-1){
    		timee = 1;
    		quit = true;
    	}
    	while (!quit && !dead) {
    		/*for (int i = 0; i < n; i++) {
    			for (int j = 0; j < m; j++) {
    				cout << d[i][j] << " ";
    			}
    			cout << endl;
    		}
    		cout << endl;*/
    		bfs2();
    		bfs();
    		if (dead) break;
    }
    	if (quit&&!dead)
    		cout << timee << endl;
    	else
    		cout << "IMPOSSIBLE" << endl;
    	return 0;
    
    }

    시간 줄이는 방법이야 여러개 있겠지만, 그냥 늘 하던대로 했다. 사람 BFS, 불 BFS 하나씩 써야하는 상황이였는데,

    queue에서, 다른 지역으로 넘어가는 순간에 또 queue 에 집어넣으면 끝날때 까지 끝난게 아니라, BFS를 벡터에 집어넣고 

    while 루프에 가둬서 반복했다. 시간줄이는 방법이 여러개 있을듯 해보이지만, 학원에서 풀다보니 어쩔수없이 이렇게했다.

    '백준알고리즘' 카테고리의 다른 글

    백조의 호수  (0) 2019.08.27
    비숍  (0) 2019.08.27
    가르침  (0) 2019.08.26
    수들의 합 2  (0) 2019.08.26
    부분합  (0) 2019.08.26

    댓글

Designed by Tistory.