[코드업/C++] 1099 성실한 개미

Hanbi·2023년 1월 4일
0

Problem Solving

목록 보기
44/108
post-thumbnail

문제

https://codeup.kr/problem.php?id=1099

풀이

x, y 좌표를 움직여가며 푸는 문제인데 생각보다 신경써야 하는 조건이 많았음

  • 배열 범위를 벗어난 경우
  • 출발지와 도착지가 동일한 경우
  • 0일 때 뿐만 아니라 2일 때도 오른쪽으로 이동
  • 미로 상자 테두리가 벽으로 되어있으므로 (9,9)가 아니라 (8,8)일 때 프로그램 종료

코드

#include <stdio.h>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

int main() {

	int arr[10][10];
	int x = 1;
	int y = 1;

	for (int i = 0; i < 10; i++) {
		for (int j = 0; j < 10; j++) {
			int n;
			cin >> n;
			arr[i][j] = n;
		}
	}

	while (1) {
		if (x == 8 && y == 8) {
			arr[x][y] = 9;
			break;
		}
		if (x > 9 || y > 9) {
			break;
		}
		if (arr[x][y] == 2) {
			arr[x][y] = 9;
			break;
		}

		arr[x][y] = 9;
		if (arr[x][y + 1] == 0 || arr[x][y + 1] == 2) { //오른쪽으로 이동
			if (arr[x][y + 1] == 2) {
				arr[x][++y] = 9;
				break;
			}
			arr[x][++y] = 9;
		}
		else { //아래로 이동
			if (arr[x+1][y] == 2) {
				arr[++x][y] = 9;
				break;
			}
			arr[++x][y] = 9;
		}
	}

	for (int i = 0; i < 10; i++) {
		for (int j = 0; j < 10; j++) {
			cout << arr[i][j] << ' ';
		}
		cout << endl;
	}

	return 0;
}
profile
👩🏻‍💻

0개의 댓글