UVA 825 Walkiing on the safe side
根据地图,要求固定两点间最短路径的条数 。
这题的输入数据就是个坑,题目有没有说明数据之间有多个空格,结尾换行符之前也不止一个空格,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的更多相关文章
- uva 825 - Walking on the Safe Side(dp)
题目链接:825 - Walking on the Safe Side 题目大意:给出n,m,现在给出n行数据, 每行有k(k为不定值)个数字, 第一个数字代表行数, 后面k - 1个数代表当前行的这 ...
- 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 ...
- UVa 825 - Walking on the Safe Side
题目:在一个N*M的网格中,从左上角走到右下角,有一些点不能经过,求最短路的条数. 分析:dp,帕斯卡三角.每一个点最短的就是走N条向下,M条向右的路. 到达每一个点的路径条数为左边和上面的路径之和. ...
- UVa 825【简单dp,递推】
UVa 825 题意:给定一个网格图(街道图),其中有一些交叉路口点不能走.问从西北角走到东南角最短走法有多少种.(好像没看到给数据范围...) 简单的递推吧,当然也就是最简单的动归了.显然最短路长度 ...
- uva 825
这个......小学生就会的 坑在输入输出了 两个数之间可能不止一个空格....wa了好几遍啊 #include <cstdio> #include <cstring> # ...
- UVA题目分类
题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics ...
- 算法入门经典大赛 Dynamic Programming
111 - History Grading LCS 103 - Stacking Boxes 最多能叠多少个box DAG最长路 10405 - Longest Common Subsequence ...
- UVA - 825Walking on the Safe Side(dp)
id=19217">称号: UVA - 825Walking on the Safe Side(dp) 题目大意:给出一个n * m的矩阵.起点是1 * 1,终点是n * m.这个矩阵 ...
- 紫书 习题 8-25 UVa 11175 (结论证明)(配图)
看了这篇博客https://blog.csdn.net/u013520118/article/details/48032599 但是这篇里面没有写结论的证明, 我来证明一下. 首先结论是对于E图而言, ...
随机推荐
- SqLite 框架 GreenDAO
GreenDAO: 会生成一个数据访问,不用我们书写访问数据库的代码: 核心原理图 生成代码 就是用生成器生成一个对应的java类的生成工厂 public static void main(Strin ...
- 关于数据导出到Excel科学计数法的处理
SELECT '=T("'+字段+'")' from table 在这里在显示的字段内容前加了 '=T("',在后面也加了'")'.在这这里T()是Exc ...
- window的画图工具(mspaint)也可以帮助我们开发和调试代码的.
经常在视频中看到老师使用画图板来给学生讲解概念. 久而久之,发现私下里,开发程序调试程序时也可以使用画图板来辅助开发. 新建一个"无标题"的画图板 -> 把将要区分的问题扔进 ...
- MySQL中,修改表的某一字段的部分值
语法:update 表名 set 字段名 = replace(字段名,'替换前内容','替换后的内容') where 条件. 如: 执行sql语句:update student set name = ...
- js方法的命名不能使用表单元素的名称或ID
今天在写页面的时候,遇到一个关于js方法的命名问题,先看下代码: 表单元素如下: <select name="isCulture" onchange="isCult ...
- 实现scp自动输入密码(判断yesno选项)
1.apt-get install expect 2.编写shell脚本test.sh #!/usr/bin/expect -f#!/bin/shset password 1spawn scp roo ...
- Raphael:JS矢量图形库
Raphael:JS矢量图形库 2016-08-29 http://dmitrybaranovskiy.github.io/raphael/
- Beyond Compare 使用介绍
Beyond Compare 背景 平时工作中对于源代码都是使用SVN来管理,在线状态下工作的很好,但是有时候离线状态下,对于多个版本之间的代码合并就比较麻烦.尤其是涉及到多人协作时更是如此. 所以找 ...
- [DevExpress]SplitContainerControl使用小计
1.修改成纵向分割 Horizontal = false; 2.设置伸缩箭头 3.固定某个PANEL大小 最大化后依然保持着比例 4.隐藏某个PANEL splitContainerControl1. ...
- HTML5:基本使用
单行文本输入框 type为text表示input元素为一个单行文本框,是input元素的默认表现形式.单行文本输入框支持下面的属性设置. A:设定元素大小 maxlength属性设定用户能够输入的字符 ...