티스토리 뷰

Coding/알고리즘

미로 탈출 로봇

빠리빵 2018. 6. 6. 16:16
첫 줄에 미로의 크기 X, Y(1≤X, Y≤100)가 주어진다.
둘째 줄에 출발점 x, y 좌표와 도착점 x, y 좌표가 공백으로 구분하여 주어진다.
셋째 줄부터 미로의 정보가 길은 0, 벽은 1로 공백이 없이 들어온다.
주의 사항으로, 좌표는 좌측상단이 가장 작은 위치이며 이 위치의 좌표는 (1,1)이다.


 
#include 
#include 
using namespace std;

int wp;
int rp;

struct queue {
	int x;
	int y;
	int time;
};

queue myQueue[10000];

bool isNotEmpty() {
	return wp > rp;
}

void push(queue input) {
	myQueue[wp++] = input;
}

queue pop() {
	return myQueue[rp++];
}

int X;
int Y;

int dx[4] = { 0,0,1,-1 };
int dy[4] = { 1,-1,0,0 };

int map[102][102];
int visited[102][102];

queue currentData;
queue nextData;

int goal_x;
int goal_y;

int main() {
	cin >> X;
	cin >> Y;

	cin >> nextData.x;
	cin >> nextData.y;
	nextData.time = 0;

	cin >> goal_x;
	cin >> goal_y;

	for (int i = 1; i <= Y; i++) {
		for (int j = 1; j <= X; j++) {
			scanf("%1d", &map[i][j]);
		}
	}

	visited[nextData.y][nextData.x] = 1;
	push(nextData);	// 처음 1개의 데이터를 넣는다.
	while (isNotEmpty()) {
		currentData = pop();
		//cout << "방문정보 -> x : " << currentData.x << " y : " << currentData.y << " time : " << currentData.time << endl;

		if ((currentData.x == goal_x) && (currentData.y == goal_y)) {
			cout << currentData.time;
			break;
		}

		for (int i = 0; i < 4; i++) {
			nextData.x = currentData.x + dx[i];
			nextData.y = currentData.y + dy[i];
			nextData.time = currentData.time + 1;

			if ((nextData.x < 1) || (nextData.x > X)) continue;
			if ((nextData.y < 1) || (nextData.y > Y)) continue;
			if (visited[nextData.y][nextData.x] == 1) continue;
			if (map[nextData.y][nextData.x] == 1) continue;

			visited[nextData.y][nextData.x] = 1;
			push(nextData);
		}
	}

	//system("pause");
}

'Coding > 알고리즘' 카테고리의 다른 글

구슬 집어넣기 게임  (0) 2018.06.06
비트방정식  (0) 2018.06.03
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함