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

 

이번에 볼 문제는 백준 15241번 문제인 Counting paths이다.
문제는 아래 링크를 확인하자.

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

 

15241번: Counting paths

Every afternoon, Jack runs from his house to John's. Their houses are in an open field of size N x M. Jack is trying to use a different path each day but he is not sure how many different ways to reach John's house exist. We will represent the field using

www.acmicpc.net

격자칸의 좌상단 칸에서 우하단 칸까지 이동하는 경로의 수를 세는 문제이다.

 

dp[r][c]를 좌상단 칸에서 r행 c열 칸까지 이동하는 경로의 수로 정의할 때 해당 위치가 'X'가 아니라면 dp[r][c] = dp[r-1][c] + dp[r][c-1]이 성립한다. 해당 위치가 'X'라면 값은 0이다.

 

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

#include <iostream>
#include <string>
using namespace std;

int R, C;
string s[200];
int arr[201][201];

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

	cin >> R >> C;
	for (int r = 0; r < R; r++) cin >> s[r];

	arr[1][1] = 1;
	s[0][0] = 'X';
	for (int r = 1; r <= R; r++) {
		for (int c = 1; c <= C; c++) {
			if (s[r - 1][c - 1] == 'X') continue;
			arr[r][c] = arr[r - 1][c] + arr[r][c - 1];
			if (arr[r][c] > 1000000006) arr[r][c] -= 1000000007;
		}
	}

	cout << arr[R][C];
}
728x90

'BOJ' 카테고리의 다른 글

[BOJ 1600 // C++] 말이 되고픈 원숭이  (0) 2022.07.10
[BOJ 24954 // C++] 물약 구매  (0) 2022.07.10
[BOJ 24725 // C++] 엠비티아이  (0) 2022.07.10
[BOJ 15233 // C++] Final Score  (0) 2022.07.10
[BOJ 14499 // C++] 주사위 굴리기  (0) 2022.07.10

+ Recent posts