UVA - 825 Walking on the Safe Side

来源:互联网 发布:网络推广 photoshop 编辑:程序博客网 时间:2024/05/16 19:11

题目大意:有一个矩阵,要求从起点走到终点,问有几条这样的路径

解题思路:本题的输入比较麻烦,随机的数目,这点要注意,从起点走到终点的最短的路径,并不是要路径都达到最小值,而是要求在只能往左或往下走的情况下能到达终点,所以用dp[i][j]表示到达该点能有几条路径,那么该点不是障碍的情况下,dp[i][j] = dp[i-1][j] + dp[i][j-1]

#include<cstdio>#include<cstring>#define maxn 1010char str[maxn];int num[maxn][maxn];int dp[maxn][maxn];int n,m;int main() {int test;scanf("%d", &test);while(test--) {int x;scanf("%d%d",&n,&m);memset(num,0,sizeof(num));for(int i = 1; i <= n; i++) {scanf("%d",&x);gets(str);int len = strlen(str);int temp = 0;for(int j = 1; j <= len; j++) if(str[j] >= '0' && str[j] <= '9')temp = temp * 10 + str[j] - '0';else {num[x][temp] = 1;temp = 0;}}memset(dp,0,sizeof(dp));dp[1][0] = 1;for(int i = 1; i <= n; i++)for(int j = 1; j <= m; j++) {if(num[i][j] == 1)dp[i][j] = 0;elsedp[i][j] = dp[i-1][j] + dp[i][j-1];}printf("%d\n",dp[n][m]);if(test)printf("\n");}return 0;}


0 0
原创粉丝点击