UVA - 825 Walking on the Safe Side

题目大意:给出n,m,现在给出n行数据, 每行有k(k为不定值)个数字, 第一个数字代表行数, 后面k - 1个数代表当前行的这个位置不可走, 问有多少路径可以从(1,1)到(n,m),只能向下或向右。


解题思路:dp[i][j] = dip[i - 1][j] + dp[i][j - 1], 很简单的dp问题。

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

int main() {
	int T, cnt = 0;
	scanf("%d", &T);
	while (T--) {
		int temp, R, C, DP[300][300] = {1};
		char str[200];
		scanf("%d%d%*c", &R, &C);
		for (int i = 0; i < R; i++) {
			string line;
			getline(cin, line);
			stringstream ss(line);
			int temp;
			ss >> temp;
			while (ss >> temp)
				DP[i][temp-1] = -1;
		}

		for (int i = 0; i < R; i++)
			for (int j = 0; j < C; j++)
				if (DP[i][j] != -1) {
					if (DP[i][j+1] != -1)
						DP[i][j+1] += DP[i][j];

					if (DP[i+1][j] != -1)
						DP[i+1][j] += DP[i][j];
				}
		if (cnt++)
			printf("\n");
		printf("%d\n", DP[R-1][C-1]);
	}
	return 0;
}


你可能感兴趣的:(UVA - 825 Walking on the Safe Side)