※ 글쓴이는 취미로 코딩을 익혀보는 사람이라 정확하지 않은 내용을 담고 있을 수 있다 ※

 

이번에 볼 문제는 백준 6905번 문제인 Snakes and Ladders이다.
문제는 아래 링크를 확인하자.

https://www.acmicpc.net/problem/6905 

 

6905번: Snakes and Ladders

Here (see illustration) is a game board for the game Snakes and Ladders. Each player throws a pair of dice to determine how many squares his/her game piece will advance. If the piece lands on the bottom of a ladder, the piece moves up to the square at the

www.acmicpc.net

(1) 주사위를 굴리고 (2) 말을 옮기고 (3) 현재 말의 위치를 출력하는 것을 반복하는 시뮬레이션 코드를 작성해 문제를 해결하자.

 

어떤 칸이 뱀 또는 사다리를 이용해 이동해야하는 칸이면 그 칸이, 그렇지 않은 칸이면 0이 기록되어있는 배열을 준비하자. 이 배열을 이용하면 구현을 간단하게 할 수 있다.

 

100을 넘는 위치로 이동하려는 시도는 무시해야 함에 유의하자. 또한 100에 도달하지 않은 상태로 게임을 마치면 "You Quit!"을 출력해야 함을 놓치지 말자.

 

아래는 제출한 소스코드이다.

#include <iostream>
using namespace std;

int cur = 1;
int arr[101];
int x;

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);

	arr[54] = 19, arr[90] = 48, arr[99] = 77, arr[9] = 34, arr[40] = 64, arr[67] = 86;

	cin >> x;
	while (x) {
		if (cur + x < 101) cur += x;
		if (arr[cur]) cur = arr[cur];

		cout << "You are now on square " << cur << '\n';
		if (cur == 100) {
			cout << "You Win!";
			return 0;
		}
		
		cin >> x;
	}

	cout << "You Quit!";
}
728x90

'BOJ' 카테고리의 다른 글

[BOJ 6904 // C++] Picture Perfect  (0) 2023.08.17
[BOJ 3234 // C++] LUKA  (0) 2023.08.17
[BOJ 17613 // C++] 점프  (0) 2023.08.17
[BOJ 2729 // C++] 이진수 덧셈  (0) 2023.08.16
[BOJ 4107 // C++] Knitting  (0) 2023.08.16

+ Recent posts