UVA - 825Walking on the Safe Side(dp)
id=19217">称号: UVA - 825Walking on the Safe Side(dp)
题目大意:给出一个n * m的矩阵。起点是1 * 1,终点是n * m。这个矩阵上有些点是不能够经过的,要求从起点到终点距离最短,而且不能走那种不能走的点。一共同拥有多少种方式。
解题思路:要求路径最短的话,每一个点要不向右走,要不向下走。dp【i】【j】 = dp【i】【j + 1】 + dp【i + 1】【j】;当这个点不能通过。dp【i】【j】 = 0;这个坑点在例子输入。不一定是规范的输入。可能两个数字之间非常多空格,或者最后一个数字和换行符之间非常多空格。
代码:
#include <cstdio>
#include <cstring> const int N = 1005; typedef long long ll; int G[N][N];
ll dp[N][N];
char str[N]; void handle () { int x, y;
bool flag = 1;
x = y = 0;
// printf ("%s\n", str);
for (int i = 0; i <= strlen (str); i++) { if (str[i] >= '0' && str[i] <= '9') { if (flag)
x = x * 10 + str[i] - '0';
else
y = y * 10 + str[i] - '0';
} else { if (!flag)
G[x][y] = 1;
// printf ("%d %d\n", x, y);
y = 0;
flag = 0;
}
}
} int main () { int t, n, m;
int x, y;
char ch;
scanf ("%d", &t);
while (t--) { scanf ("%d%d%*c", &n, &m);
memset (G, 0, sizeof (G));
for (int i = 1; i <= n; i++) { gets(str);
handle();
} for (int i = n; i >= 1; i--)
for (int j = m; j >= 1; j--) { dp[i][j] = 0;
if (G[i][j])
continue; if (i == n && j == m) {
dp[i][j] = 1;
continue;
}
if (i != n)
dp[i][j] += dp[i + 1][j];
if (j != m)
dp[i][j] += dp[i][j + 1];
} printf ("%lld\n", dp[1][1]);
if (t)
printf ("\n");
}
return 0;
}
版权声明:本文博客原创文章,博客,未经同意,不得转载。
UVA - 825Walking on the Safe Side(dp)的更多相关文章
- UVA 10163 Storage Keepers(两次DP)
UVA 10163 Storage Keepers(两次DP) http://uva.onlinejudge.org/index.php? option=com_onlinejudge&Ite ...
- uva 11584 Partitioning by Palindromes 线性dp
// uva 11584 Partitioning by Palindromes 线性dp // // 题目意思是将一个字符串划分成尽量少的回文串 // // f[i]表示前i个字符能化成最少的回文串 ...
- uva 825 - Walking on the Safe Side(dp)
题目链接:825 - Walking on the Safe Side 题目大意:给出n,m,现在给出n行数据, 每行有k(k为不定值)个数字, 第一个数字代表行数, 后面k - 1个数代表当前行的这 ...
- UVA 10564 Paths through the Hourglass[DP 打印]
UVA - 10564 Paths through the Hourglass 题意: 要求从第一层走到最下面一层,只能往左下或右下走 问有多少条路径之和刚好等于S? 如果有的话,输出字典序最小的路径 ...
- UVA - 11584 Partitioning by Palindromes[序列DP]
UVA - 11584 Partitioning by Palindromes We say a sequence of char- acters is a palindrome if it is t ...
- UVA 1401 - Remember the Word(Trie+DP)
UVA 1401 - Remember the Word [题目链接] 题意:给定一些单词.和一个长串.问这个长串拆分成已有单词,能拆分成几种方式 思路:Trie,先把单词建成Trie.然后进行dp. ...
- [uva 11762]Race to 1[概率DP]
引用自:http://hi.baidu.com/aekdycoin/item/be20a91bb6cc3213e3f986d3,有改动 题意: 已知D, 每次从[1,D] 内的所有素数中选择一个Ni, ...
- uva live 4394 String painter 间隔dp
// uva live 4394 String painter // // 问题是,在培训指导dp运动主题,乍一看,我以为只是一点点复杂 // A A磕磕磕,两个半小时后,.发现超过例子.然而,鉴于他 ...
- Uva 11077 Find the Permutations [置换群 DP]
题意: 给定$n$和$k$,问有多少排列交换$k$次能变成升序 $n \le 21$ $uva$貌似挂掉了$vjudge$上一直排队 从某个排列到$1,2,...,n$和从$1,2,...,n$到某个 ...
随机推荐
- VCS编译仿真警告Warning
VCS编译仿真警告Warning 问题描述 在较大的SOC集成中,通常使用Perl脚本例化子模块到Top层,然而,有时会出现例化出来的输入端口名没有在Top层定义,而且端口的位宽为1bit,那么,ve ...
- php实现不用加减乘除号做加法(1、善于寻找资源:去搜为什么位运算可以实现加法,里面讲的肯定要详细一万倍)
php实现不用加减乘除号做加法(1.善于寻找资源:去搜为什么位运算可以实现加法,里面讲的肯定要详细一万倍) 一.总结 1.善于寻找资源:去搜为什么位运算可以实现加法,里面讲的肯定要详细一万倍 二.ph ...
- 【社交分享SDK】ShareSDK for Android 2.5.9已经公布
ShareSDK for Android 2.5.9已经公布 版本号:V2.5.9 2015-3-19 1.升级Dropbox对API接口的调用.包含授权.获取用户信息,分享三个接口 2.升级Kak ...
- 开源 免费 java CMS - FreeCMS1.9 会员管理
项目地址:http://www.freeteam.cn/ 会员管理 1. 会员管理 从左側管理菜单点击会员管理进入. 2. 加入会员 在会员列表下方点击"加入"button. 填写 ...
- 【codeforces 755B】PolandBall and Game
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 一大波Java来袭(四)String类、StringBuilder类、StringBuffer类对照
本文主要介绍String类.StringBuffer类.StringBuilder类的差别 : 一.概述 (一)String 字符串常量.可是它具有不可变性,就是一旦创建,对它进行的不论什么改动操作 ...
- java基础—网络编程———建立聊天的形式
接收器和发射器的简单演示 import java.io.*; import java.net.*; public class SocketDemo { public static void main( ...
- Android学习Scroller(三)——控件平移划过屏幕 (Scroller简单使用)
MainActivity例如以下: package cc.cn; import android.os.Bundle; import android.view.View; import android. ...
- POJ 1985 - 树的直径
传送门 题目大意 给一颗n个点的树,求树的直径(最长的一条链) 题解 先随便找一个点u,dfs出离它最远的点v 于是有以下情况: 直径就是这条链 直径经过u,是这条链的延长 直径不经过u 只需要从v再 ...
- 属性动画Property Animation
ViewPropertyAnimation 使用方式:View.animate() 后跟 translationX() 等方法,动画会自动执行. 注意translationX实现是调用对应的set方法 ...