미로탈출로봇
미로탈출 로봇 대회를 개최하였다. 대회에 사용되는 미로는 가로(X), 세로(Y) 100이하의 크기이며, 미로를 한 칸 이동하는 데는 1초가 걸린다.

로봇이 출발점에서 도착점까지 가장 빨리 이동할 경우 걸리는 시간을 구하는 프로그램을 작성하시오.
입력
첫 줄에 미로의 크기 X, Y(1≤X, Y≤100)가 주어진다.
둘째 줄에 출발점 x, y 좌표와 도착점 x, y 좌표가 공백으로 구분하여 주어진다.
셋째 줄부터 미로의 정보가 길은 0, 벽은 1로 공백이 없이 들어온다.
주의 사항으로, 좌표는 좌측상단이 가장 작은 위치이며 이 위치의 좌표는 (1,1)이다.
출력
첫 줄에 출발점에서 도착점까지 가장 빠른 시간을 출력한다.
입력 예시
8 7
1 2 7 5
11111111
00000111
10110011
10111001
10111101
10000001
11111111
출력 예시
9
도움말
[입력 예시 2]
8 10
1 1 7 9
00000101
11001000
10000010
01101010
00000110
01010000
01110110
10000100
10011101
01000001
[출력 예시 2]
16
---------------------------------------------------------------------------------------------------
[입력 예시 3]
5 5
1 1 5 5
00000
01101
00100
01110
00000
[출력 예시 3]
8
#include <stdio.h>
#define MAX 100000
int X, Y;
int startX, startY;
int targetX, targetY;
int map[101][101];
int visited[101][101];
typedef struct QUEUE {
int x;
int y;
int time;
}QUEUE;
QUEUE Q[MAX];
int wp, rp;
int dir[4][2] = { { 0, 1 }, { 0, -1 }, { 1, 0 }, { -1, 0 } };
void push_queue(QUEUE t) {
Q[wp++] = t;
}
QUEUE pop_queue() {
return Q[rp++];
}
void input_data() {
scanf("%d %d", &X, &Y);
scanf("%d %d %d %d", &startX, &startY, &targetX, &targetY);
for (int j = 1; j <= Y; j++) {
for (int i = 1; i <= X; i++) {
scanf("%1d",&map[j][i]);
}
}
wp = rp = 0;
QUEUE tmp;
tmp.time = 0;
tmp.x = startX;
tmp.y = startY;
visited[startY][startX] = 1;
push_queue(tmp);
}
int solve() {
while (1) {
if (wp <= rp) break;
QUEUE tmp = pop_queue();
/*printf("%d %d %d \n", tmp.time, tmp.x, tmp.y);*/
for (int i = 0; i < 4; i++) {
int nextX = tmp.x + dir[i][0];
int nextY = tmp.y + dir[i][1];
int nextT = tmp.time + 1;
if (nextX <1 || nextX >X || nextY <1 || nextY >Y) continue;
if (visited[nextY][nextX] == 1) continue;
if (map[nextY][nextX] == 0) {
if ((nextX == targetX) && (nextY == targetY)) return nextT;
visited[nextY][nextX] = 1;
QUEUE tmp2;
tmp2.time = nextT;
tmp2.x = nextX;
tmp2.y = nextY;
/* printf("push : %d %d %d \n", tmp.time, tmp.x, tmp.y);*/
push_queue(tmp2);
}
}
}
return -1;
}
int main(void)
{
// 여기서부터 작성
input_data();
printf("%d\n", solve());
return 0;
}
댓글
댓글 쓰기