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

这题的输入数据就是个坑,题目有没有说明数据之间有多个空格,结尾换行符之前也不止一个空格,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. jquery自动生成二维码

    把下面的jquery代码放到想要在当前页面上面生成二维码: 代码如下: <script type="text/javascript">var _qrContent='' ...

  2. 通过公网连接云数据库Memcache--ECS Windows篇

    目前云数据库Memcache是需要通过ECS的内网进行连接访问,如果用户本地需要通过公网访问云数据库Memcache,可以在ECS Windows云服务器中通过netsh进行端口映射实现. 一.搭建要 ...

  3. SQL Server 2012 中 Update FROM子句

    首先说明一下需求以及环境 创建Table1以及Table2两张表,并插入一下数据 USE AdventureWorks2012; GO IF OBJECT_ID ('dbo.Table1', 'U') ...

  4. excel知识

    excel中导出文本中的制表符去除方法:

  5. shell小程序

    因此需要挑选学生,因此需要一个抓阄的程序:要求:1.执行脚本后,想去的同学输入英文名字全拼,产生随机数01-99之间的数字,数字越大就去参加项目实践,前面已经抓到的数字,下次不能在出现相同数字.2.第 ...

  6. 从零开始部署小型企业级虚拟桌面 -- Vmware Horizon View 6 For Linux VDI -- 结构规划

    环境说明 注,本套环境所用机器全部是64位的. 管理服务器载体:安装win7操作系统,通过VMware Workstation安装4台虚拟机,用作vCenter,Connection Server,D ...

  7. c#实现FTP上传

    /// <summary> /// 上传文件 /// </summary> /// <param name="fileinfo">需要上传的文件 ...

  8. mina2.0 spring

    Apache MINA是一个网络应用程序框架,它可以帮助用户开发的高性能.高扩展性的网络应用程序.它提供了一个抽象的事件驱动的异步API在不同传输如TCP/IP和UDP/IP通过java NIO. A ...

  9. 02_setter注入

    工程截图如下 [HelloWorld.java] package com.HigginCui; public class HelloWorld { private String words; publ ...

  10. OpenJudge 2813 画家问题 / Poj 1681 Painter's Problem

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