벽을 한 번 부신다!!
코드가 길지 않으니 설명은 없다.
This file contains 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 <cstdio> | |
#include <queue> | |
/** | |
* https://www.acmicpc.net/problem/14923 | |
* BOJ 백준온라인져지 14923 미로탈출 풀이 | |
*/ | |
#define INF 99999999 | |
using namespace std; | |
int xxxx[] = {0, 1, 0 , -1}; | |
int yyyy[] = {-1, 0, 1 , 0}; | |
class coordinate{ | |
public: | |
int x, y; | |
bool z; | |
coordinate(int _x, int _y, bool _z) :x(_x), y(_y), z(_z){} | |
}; | |
int min(int a, int b){ | |
return a < b ? a : b; | |
} | |
int main(){ | |
int rowSize, columnSize; | |
scanf("%d%d", &rowSize, &columnSize); | |
rowSize++, columnSize++; | |
int ***resultArray = new int**[rowSize], **map = new int*[rowSize]; | |
int startX, startY; | |
int endX, endY; | |
scanf("%d%d%d%d", &startX, &startY, &endX, &endY); | |
for(int i = 1; i < rowSize; i++){ | |
resultArray[i] = new int*[columnSize]; | |
map[i] = new int[columnSize]; | |
for(int j = 1; j < columnSize; j++){ | |
resultArray[i][j] = new int[2]; | |
scanf("%d", &map[i][j]); | |
resultArray[i][j][0] = resultArray[i][j][1] = INF; | |
} | |
} | |
resultArray[startX][startY][0] = 0; | |
queue<coordinate> queue; | |
queue.push(coordinate(startX, startY, false)); | |
while(!queue.empty()){ | |
coordinate polledData = queue.front(); | |
queue.pop(); | |
int x = polledData.x; | |
int y = polledData.y; | |
bool z = polledData.z; | |
int moveCount = resultArray[x][y][z] + 1; | |
for(int i = 0 ; i < 4; i++){ | |
int newX = xxxx[i] + x; | |
int newY = yyyy[i] + y; | |
if(newX <= 0 || newX >= rowSize || newY <= 0 || newY >= columnSize) continue; | |
if(map[newX][newY] == 0 && moveCount < resultArray[newX][newY][z]){ | |
queue.push(coordinate(newX, newY, z)), resultArray[newX][newY][z] = moveCount; | |
}else if(map[newX][newY] == 1 && !z && moveCount < resultArray[newX][newY][1]){ | |
queue.push(coordinate(newX, newY, 1)), resultArray[newX][newY][1] = moveCount, | |
resultArray[newX][newY][2] = 1; | |
} | |
} | |
} | |
int result = min(resultArray[endX][endY][0], resultArray[endX][endY][1]); | |
if(result == INF) result = -1; | |
printf("%d", result); | |
} |
'IT > 알고리즘' 카테고리의 다른 글
BOJ 백준온라인져지 14924 폰 노이만과 파리 풀이 (0) | 2017.12.16 |
---|---|
BOJ 백준온라인져지 2206 벽 부수고 이동하기 풀이 (0) | 2017.12.15 |
BOJ 백준온라인져지 1456 거의 소수 풀이 (0) | 2017.12.14 |
BOJ 백준온라인져지 14921 용액 합성하기 풀이 (0) | 2017.12.14 |
BOJ 백준온라인져지 2530 인공지능 시계 풀이 (0) | 2017.12.14 |