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

这题的输入数据就是个坑,题目有没有说明数据之间有多个空格,结尾换行符之前也不止一个空格,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. Hive体系结构介绍

    http://www.aboutyun.com/thread-6217-1-1.html   1.Hive架构与基本组成     下面是Hive的架构图. 图1.1 Hive体系结构     Hive ...

  2. try catch异常捕获

    格式为: try            {                int i = int.Parse(Console.ReadLine());           //容易发生错误的语句    ...

  3. 九度OJ 1480 最大上升子序列和 -- 动态规划

    题目地址:http://ac.jobdu.com/problem.php?pid=1480 题目描述: 一个数的序列bi,当b1 < b2 < ... < bS的时候,我们称这个序列 ...

  4. OpenJudge 2773 2726 2727 采药

    1.链接地址: http://bailian.openjudge.cn/practice/2773/ http://bailian.openjudge.cn/practice/2726/ http:/ ...

  5. CENTOS7 使用网络管理器配置静态IP地址

    CENTOS7 的网络配置和CENTOS6有些不同. 如果你想要使用网络管理器来管理该接口,你可以使用nmtui(网络管理器文本用户界面),它提供了在终端环境中配置配置网络管理器的方式. 在使用nmt ...

  6. 排序算法TWO:快速排序QuickSort

    import java.util.Random ; /** *快速排序思路:用到了分治法 * 一个数组A[0,n-1] 分解为三个部分,A[0,p - 1] , A[p] , A[p + 1, n-1 ...

  7. Python OptionParser学习

    from optparse import OptionParser import sys def main(): p = OptionParser() p.add_option('-n','--nam ...

  8. 基于Emgu CV 的手势识别实现PPT的控制放映

    Emgu CV 简介         众所周知,Emgu CV是.NET平台下对OpenCV图像处理库的封装,也就是.NET版的OpenCV.开发者可以很方便的通过C#,VB等语言调用OpenCV函数 ...

  9. ASP.NET 学习小记 -- “迷你”MVC实现(1)

    ASP.NET 由于采用了管道式设计,具有很好的扩展性.整个ASP.NET MVC应用框架就是通过扩展ASP.NET实现的.通过ASP.NET的管道设计,我们知道,ASP.NET的扩展点主要是体现在H ...

  10. Codeforces Round #344 (Div. 2) C. Report

    Report 题意:给长度为n的序列,操作次数为m:n and m (1 ≤ n, m ≤ 200 000) ,操作分为t r,当t = 1时表示将[1,r]序列按非递减排序,t = 2时表示将序列[ ...