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

这题的输入数据就是个坑,题目有没有说明数据之间有多个空格,结尾换行符之前也不止一个空格,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. hasLayout与Block formatting contexts的学习(下)

    BFC布局规则: 内部的Box会在垂直方向,一个接一个地放置. Box垂直方向的距离由margin决定.属于同一个BFC的两个相邻Box的margin会发生重叠 每个元素的margin box的左边, ...

  2. html同一个页面多个倒计时

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head ...

  3. SQL Server 负载均衡集群方案之Moebius

    一.本文所涉及的内容(Contents) 本文所涉及的内容(Contents) 背景(Contexts) 架构原理(Architecture) 测试环境(Environment) 安装Moebius( ...

  4. MySql 5.7密码查看或修改

    一.启动命令行,输入: taskkill /f /im mysqld.exe //关闭mysql 二.转入mysql的bin目录下 三.输入:mysqld --skip-grant-tables // ...

  5. 查看Unix系统是32位还是64位

    #getconf查看OS位数 以下经过测试了HP: getconf KERNEL_BITSLinux: getconf LONG_BITAIX: getconf KERNEL_BITMODE #AIX ...

  6. ###STL学习--适配器

    点击查看Evernote原文. #@author: gr #@date: 2014-08-24 #@email: forgerui@gmail.com STL中的适配器. ###stl学习 |--迭代 ...

  7. (转)QRCODE二维码介绍及常用控件推荐

    什么是QR Code码? QR Code码是由日本Denso公司于1994年9月研制的一种矩阵二维码符号,它具有一维条码及其它二维条码所具有的信息容量大.可靠性高.可表示汉字及图象多种文字信息.保密防 ...

  8. 如何在Markdown、HTML编辑器上输入一些特殊字符

    如何输入EntityCode 参考: 1.EntityCode 2.Common HTML entities used for typography 3.Latin Supplement-拉丁补充

  9. Windows 8上强制Visual Studio以管理员身份运行

    原文链接:http://edi.wang/post/2013/2/28/force-visual-studio-always-run-as-admin-on-windows-8 Windows 8的一 ...

  10. Google设计理念

    Google的十大信条 我们首次拟就这“十大信条”还是在Google刚刚成立没几年的时候.此后,我们时常重新审视这份清单,看看它是否依然适用.我们希望这些信条永不过时,而您也可以监督我们是否遵守了这些 ...