uva825 - Walking on the Safe Side(动规)
来源:互联网 发布:cool edit pro有mac版 编辑:程序博客网 时间:2024/05/15 12:55
动规, 记忆化搜索。。。。
思路不难
状态:dp[i][j]表示点(I,j)到终点的最短路径数目;
状态转移:dp[i][j] = dp[i+1][j]+dp[i][j+1];
还有就是这道题的测试数据很操蛋,两个数之间不是规律的有一个空格,有可能有多个空格,我卡到这一点上wa了好几次。
所以说读取的时候要用字符串读取。
代码如下;
#include <cstdio>#include <cstring>#define M 105#define N 105int m, n, a[N][N], d[N][N];char s[N];int dp(int x, int y){ int &ans = d[x][y]; if(ans) return ans; if(a[x][y]) return ans = 0; if(x==m&&y==n) return ans = 1; ans = 0; int nx = x+1, ny = y+1; if(nx<=m) ans+=dp(nx,y); if(ny<=n) ans+=dp(x,ny); return ans;}int main (){ int cas, x, t, tt = 0; char ch; scanf("%d",&cas); while(cas--) { memset(a,0,sizeof(a)); memset(d,0,sizeof(d)); scanf("%d%d",&m,&n); for(int i = 1; i <= m; i++) { scanf("%d",&x); gets(s); int len = strlen(s); t = 0; for(int j = 0; j <= len; j++) { if(s[j]>='0'&&s[j]<='9') t = t*10+s[j]-'0'; else { a[x][t] = 1; t = 0; } } } if(tt++) printf("\n"); printf("%d\n",dp(1,1)); } return 0;}
- uva825 - Walking on the Safe Side(动规)
- UVA825- Walking on the Safe Side
- UVA825- Walking on the Safe Side
- uva 825 Walking on the Safe Side
- UVA 825 Walking on the Safe Side
- 825 - Walking on the Safe Side
- UVa 825 - Walking on the Safe Side
- uva 825 - Walking on the Safe Side
- UVa 825 - Walking on the Safe Side
- UVA-825-Walking on the Safe Side
- UVa:825 Walking on the Safe Side
- UVA - 825 Walking on the Safe Side
- uva 825 Walking on the Safe Side
- UVa 825 - Walking on the Safe Side
- UVA - 825 Walking on the Safe Side
- UVA - 825 Walking on the Safe Side
- uva 825 Walking on the Safe Side
- UVA 825 Walking on the Safe Side
- CmdletBindingAttribute之PostionalBinding的作用
- 分布式设计与开发(三)------高一致性服务ZooKeeper
- 61条面向对象设计的经验原则
- Android UI控件四
- 师兄有话说
- uva825 - Walking on the Safe Side(动规)
- 基于Jupiter建立code review机制
- 单例模式
- 二叉排序树(BST)的查找算法(非递归算法)
- 系统学习TCP/IP协议(一)------有用的策略和模式
- 收集小知识 持续更新...
- 对云计算技术的看法
- beans.xml
- 设计模式(22)---->访问者模式