根据地图,要求固定两点间最短路径的条数 。

这题的输入数据就是个坑,题目有没有说明数据之间有多个空格,结尾换行符之前也不止一个空格,WA了好几遍,以后这种情况看来都要默认按照多空格的情况处理了。

可以先利用bfs求出起点到各点的最短距离,然后dfs统计 num[x][y]表示起点到x,y的最短路径数,转移方程为 num[x][y] += num[nx][ny], nx = x +dx[k],ny = y + dy[k],

map[nx][ny] != 0.

代码:

 #include <iostream>
#include <cstdio>
#include <climits>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <algorithm>
#define esp 1e-6
#define pb push_back
#define in freopen("in.txt", "r", stdin);
#define out freopen("out.txt", "w", stdout);
#define print(a) printf("%d\n",(a));
#define bug puts("********))))))");
#define Rep(i, c) for(__typeof(c.end()) i = c.begin(); i != c.end(); i++)
#define inf 0x0f0f0f0f
using namespace std;
typedef long long LL;
typedef vector<int> VI;
typedef pair<int, int> pii;
typedef vector<pii,int> VII;
typedef vector<int>:: iterator IT;
#define N 200
int map[N][N], vis[N][N], num[N][N];
int dis[N][N];
int n, m;
int dx[] = {-, , , };
int dy[] = {, , , -};
void init(void)
{
memset(map, -, sizeof(map));
memset(vis, , sizeof(vis));
memset(num, , sizeof(num));
memset(dis, , sizeof(dis));
}
void bfs(int x, int y)
{
int u = x*m+y;
queue<int> q;
q.push(u);
vis[x][y] = ;
while(!q.empty())
{
int u = q.front();
int x = u/m, y = u%m;
q.pop();
// print(vis[0][1]) print(vis[1][0])
for(int k = ; k < ; k++)
{
int nx = x + dx[k], ny = y + dy[k];
if(nx >= && nx < n && ny >= && ny < m && !vis[nx][ny] && map[nx][ny])
{
dis[nx][ny] = dis[x][y] + ;
q.push(nx*m+ny);
vis[nx][ny] = ;
}
}
}
}
int dfs(int x, int y)
{
int ans = num[x][y];
if(x == && y == && map[x][y])
return num[x][y] = ;
if(ans > )
return ans ;
for(int k = ; k < ; k++)
{
int nx = x+dx[k], ny = y+dy[k];
if(nx >= && nx < n && ny >= && ny < m && dis[nx][ny]+ == dis[x][y] && map[nx][ny])
ans += dfs(nx, ny);
}
return num[x][y] = ans;
}
int main(void)
{
int T;
for(int t = scanf("%d", &T); t <= T; t++)
{
if(t - )
puts("");
init();
scanf("%d%d", &n, &m);
char ch;
for(int i = ; i < n; i++)
{
int j;
scanf("%*d");
while((ch = getchar())== ' ') ;
while(ch != '\n')
{
ungetc(ch, stdin);
scanf("%d", &j);
while((ch = getchar()) == ' ');
map[i][j-] = ;
}
}
bfs(, );
printf("%d\n", dfs(n-,m-));
}
return ;
}

其实可以直接用dp的这样似乎思路简单不少,不知道为什么我总是没想到这种简便方法,=、=

dp[i][j] = dp[i-1][j] + dp[i][j-1];

UVA 825 Walkiing on the safe side的更多相关文章

  1. uva 825 - Walking on the Safe Side(dp)

    题目链接:825 - Walking on the Safe Side 题目大意:给出n,m,现在给出n行数据, 每行有k(k为不定值)个数字, 第一个数字代表行数, 后面k - 1个数代表当前行的这 ...

  2. UVA 825 Walking on the Safe Side(记忆化搜索)

      Walking on the Safe Side  Square City is a very easy place for people to walk around. The two-way ...

  3. UVa 825 - Walking on the Safe Side

    题目:在一个N*M的网格中,从左上角走到右下角,有一些点不能经过,求最短路的条数. 分析:dp,帕斯卡三角.每一个点最短的就是走N条向下,M条向右的路. 到达每一个点的路径条数为左边和上面的路径之和. ...

  4. UVa 825【简单dp,递推】

    UVa 825 题意:给定一个网格图(街道图),其中有一些交叉路口点不能走.问从西北角走到东南角最短走法有多少种.(好像没看到给数据范围...) 简单的递推吧,当然也就是最简单的动归了.显然最短路长度 ...

  5. uva 825

    这个......小学生就会的  坑在输入输出了  两个数之间可能不止一个空格....wa了好几遍啊 #include <cstdio> #include <cstring> # ...

  6. UVA题目分类

    题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics ...

  7. 算法入门经典大赛 Dynamic Programming

    111 - History Grading LCS 103 - Stacking Boxes 最多能叠多少个box DAG最长路 10405 - Longest Common Subsequence ...

  8. UVA - 825Walking on the Safe Side(dp)

    id=19217">称号: UVA - 825Walking on the Safe Side(dp) 题目大意:给出一个n * m的矩阵.起点是1 * 1,终点是n * m.这个矩阵 ...

  9. 紫书 习题 8-25 UVa 11175 (结论证明)(配图)

    看了这篇博客https://blog.csdn.net/u013520118/article/details/48032599 但是这篇里面没有写结论的证明, 我来证明一下. 首先结论是对于E图而言, ...

随机推荐

  1. Each child in an array or iterator should have a unique "key" prop. Check the render method of `CreditCategoryModal`

    参考地址:http://f00sun.com/category/react

  2. TFS2013 安装出现TF400102错误解决

    我是参照:http://www.cnblogs.com/zhibincai/archive/2013/11/25/3442285.html 进行安装的windows 2012 + sql 2012 S ...

  3. HTTP和HTTPS详解

    http://blog.csdn.net/mingli198611/article/details/8055261/ 转自:http://www.cnblogs.com/ok-lanyan/archi ...

  4. ###STL学习--标准模板库

    下面进行STL的学习.希望能了解标准模板库中的常用容器,迭代器,可以自由运用STL以提高编写代码的效率.下面的内容我想以知识点为总结,不再像<Effective C++>那样以章节进行总结 ...

  5. NSArray函数

    1.判断是否包含某一个元素,返回1则表示有 - (BOOL)countainsObject:(id)anObject BOOL isContain = [arrayboy containsObject ...

  6. [译]当你在浏览器输入url后发生了什么

    面试题会经常问这个,之前也被问过,回答的不是很好,后来看到百度前端的一篇博客,啰嗦了好多,很么触摸屏都上了..后来看到stackoverflow上的一个回答,比较短. 原文地址:http://stac ...

  7. javascript学习笔记20160121-css选择器

    元素可以用id.标签名或类来描述: 更一般的,元素可以基于属性来选取: 这些基本的选择器可以组合使用: 选择器可以指定文档结构(重要,之前一直不太明白>的使用): 选择器可以组合起来选取多个或多 ...

  8. OpenJudge/Poj 1083 Moving Tables

    1.链接地址: http://poj.org/problem?id=1083 http://bailian.openjudge.cn/practice/1083/ 2.题目: 总时间限制: 1000m ...

  9. 快速排序算法 Quick sort

    作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4046189.html 首先随机选择一个轴,并调整数组内各个数字,使得比轴值大的数在轴的右边, ...

  10. Javascript 中 null、NaN和undefined的区别

    1.类型分析: js中的数据类型有undefined,boolean,number,string,object等5种,前4种为原始类型,第5种为引用类型. 代码 var a1; var a2 = tr ...