No Rules Rules

보드 점프 (feat. 백준, 3372번) 본문

생활/코테

보드 점프 (feat. 백준, 3372번)

개발하는 완두콩 2022. 9. 6. 17:33
728x90
반응형

보드 점프
https://www.acmicpc.net/problem/3372

 

3372번: 보드 점프

N × N 게임 보드에 양의 숫자들이 적혀있다. 목적은 왼쪽 위에서 오른쪽 아래까지 규칙에 맞게 점프를 해서 가는 것이다. 숫자들은 현재 점에서 갈 수 있는 거리를 의미한다. 반드시 오른쪽이나

www.acmicpc.net

 

반응형

 

// woohyeon.kim
// kim519620.tistory.com
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
void add_string(string in1, string in2, string& out) {
	out.clear();
	int big_size = max(in1.size(), in2.size());
	while (big_size != in1.size())
		in1 = "0" + in1;
	while (big_size != in2.size())
		in2 = "0" + in2;
	int data = 0;
	for (register int i = big_size - 1, v; i >= 0; --i) {
		v = (in1.at(i) - '0') + (in2.at(i) - '0') + data;
		if (v > 9)
			data = 1, v -= 10;
		else
			data = 0;
		out = to_string(v) + out;
	}
	if (data == 1)
		out = to_string(data) + out;
}
int main() {
	ios::sync_with_stdio(false), cin.tie(NULL);
	register int N;
	int arr[101][101];
	string dp[101][101];
	cin >> N;
	for (register int i = 1, j; i <= N; ++i)
		for (j = 1; j <= N; ++j)
			cin >> arr[i][j], dp[i][j] = "0";
	dp[1][1] = "1";
	for (register int x = 1, y; x <= N; ++x)
		for (y = 1; y <= N; ++y) {
			if ((x == N && y == N) || (dp[x][y] == "0"))
				continue;
			register int dist = arr[x][y];
			register int down = x + dist;
			register int right = y + dist;
			if (down <= N)
				add_string(dp[x][y], dp[down][y], dp[down][y]);
			if (right <= N)
				add_string(dp[x][y], dp[x][right], dp[x][right]);
		}
	cout << dp[N][N] << "\n";
	return 0;
}
// *&)*@*

 

해당 문제는 아래 "점프" 와 유형은 동일하나 경로의 개수가 long long 자료형 이상일 수 있다는 조건이 있습니다.

 

점프 (feat. 백준, 1890번)

점프 https://www.acmicpc.net/problem/1890 1890번: 점프 첫째 줄에 게임 판의 크기 N (4 ≤ N ≤ 100)이 주어진다. 그 다음 N개 줄에는 각 칸에 적혀져 있는 수가 N개씩 주어진다. 칸에 적혀있는 수는 0보다 크..

kim519620.tistory.com

 

  1. 경로를 long long 자료형이 아닌 string 자료형으로 변경하였습니다.
  2. string과 string을 더하는 add_string 함수를 구현하였습니다. (string간 동일한 인덱스간 값을 더하고 만약 10보다 크다면 다음 인덱스 연산에 1을 보상하는 방식입니다.)

 

728x90
반응형

'생활 > 코테' 카테고리의 다른 글

Steps (feat. 백준, 4395번)  (0) 2022.09.06
멍멍이 쓰다듬기 (feat. 백준, 1669번)  (0) 2022.09.06
점프 (feat. 백준, 1890번)  (0) 2022.09.06
Pascal's Travels (feat. 백준, 4620번)  (0) 2022.09.06
세 부분 (feat. 백준, 2993번)  (0) 2022.09.06
Comments