Created
April 5, 2020 08:05
-
-
Save niklasjang/5854f9c8432024f6e77c5b046d25c596 to your computer and use it in GitHub Desktop.
[PS][완전탐색][BFS]/[BOJ][14442][벽 부수고 이동하기2]
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| #include <vector> | |
| #include <queue> | |
| #include <tuple> | |
| using namespace std; | |
| int n=0, m=0,k=0, dist=1; | |
| int map[1000][1000]; | |
| bool visited[1000][1000][11]; | |
| int dx[4] ={0,0,1,-1}; | |
| int dy[4] ={1,-1,0,0}; | |
| int cx, cy, cb; | |
| queue<pair<pair<int, int>, int> > q; | |
| void bfs(int x, int y){ | |
| int size = 0; | |
| q.push(make_pair(make_pair(x,y),0)); | |
| visited[x][y][0] = true; | |
| while(!q.empty()){ | |
| size = q.size(); | |
| while(size--){ | |
| cx = q.front().first.first; | |
| cy = q.front().first.second; | |
| cb = q.front().second; | |
| if(cx == n-1 && cy == m-1){ | |
| cout<< dist; | |
| return; | |
| } | |
| q.pop(); | |
| for(int i=0; i<4; i++){ | |
| int nx = cx + dx[i]; | |
| int ny = cy + dy[i]; | |
| if(nx<0 || nx>=n) continue; | |
| if(ny<0 || ny>=m) continue; | |
| if(map[nx][ny] == 0 && !visited[nx][ny][cb]){ | |
| visited[nx][ny][cb] = true; | |
| q.push(make_pair(make_pair(nx,ny),cb)); | |
| }else{ | |
| if(cb < k && !visited[nx][ny][cb+1]){ | |
| visited[nx][ny][cb+1] = true; | |
| q.push(make_pair(make_pair(nx,ny),cb+1)); | |
| } | |
| } | |
| } | |
| } | |
| dist++; | |
| } | |
| cout<< -1; | |
| return; | |
| } | |
| int main (void){ | |
| cin.tie(NULL); | |
| ios::sync_with_stdio("false"); | |
| cin>> n>> m >> k; | |
| for(int i=0; i<n; i++){ | |
| for(int j=0; j<m; j++){ | |
| scanf("%1d", &map[i][j]); | |
| } | |
| } | |
| bfs(0,0); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment