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 streets run North-South or East-West dividing the city into regular blocks. Most street intersections are safe for pedestrians to cross. In some of them, however, crossing is not safe and pedestrians are forced to use the available underground passages. Such intersections are avoided by walkers. The entry to the city park is on the North-West corner of town, whereas the railway station is on the South-East corner.
Suppose you want to go from the park to the railway station, and do not want to walk more than the required number of blocks. You also want to make your way avoiding the underground passages, that would introduce extra delay. Your task is to determine the number of different paths that you can follow from the park to the station, satisfying both requirements.
The example in the picture illustrates a city with 4 E-W streets and 5 N-S streets. Three intersections are marked as unsafe. The path from the park to the station is 3 + 4 = 7 blocks long and there are 4 such paths that avoid the underground passages.

Input
The input begins with a single positive integer on a line by itself indicating the number of the cases following, each of them as described below. This line is followed by a blank line, and there is also a blank line between two consecutive inputs.
The first line of the input contains the number of East-West streets W and the number of North-South streets N. Each one of the following W lines starts with the number of an East-West street, followed by zero or more numbers of the North-South crossings which are unsafe. Streets are numbered from 1.
Output
For each test case, the output must follow the description below. The outputs of two consecutive cases will be separated by a blank line.
The number of different minimal paths from the park to the station avoiding underground passages.
Sample Input
1 4 5
1
2 2
3 3 5
4
Sample Output
4
题意:一个人要从左上角走到右下角。中间有些点是不能走的,要求出最小步数的路径有多少条。
思路:记忆化搜索,注意输入格式,还有方向只能向下和向右。
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <limits.h>
const int MAXN = 105, d[2][2] = {{1, 0}, {0, 1}};
int t, n, m, map[MAXN][MAXN], dp[MAXN][MAXN], i, j, ans, Min; void dfs(int bu, int x, int y) {
int i;
if (x == n && y == m) {
if (Min > bu) {
Min = bu;
ans = 0;
}
ans ++;
return;
}
for (i = 0; i < 2; i ++) {
if ((x + d[i][0] >= 1) && (x + d[i][0] <= n) && (y + d[i][1] >= 1) && (y + d[i][1] <= m) && !map[x + d[i][0]][y + d[i][1]] && dp[x + d[i][0]][y + d[i][1]] >= bu + 1) {
dp[x + d[i][0]][y + d[i][1]] = bu + 1;
dfs(bu + 1, x + d[i][0], y + d[i][1]);
}
}
}
int main() {
scanf("%d", &t);
while (t --) {
ans = 0;
Min = INT_MAX;
memset(map, 0, sizeof(map));
memset(dp, 0, sizeof(dp));
scanf("%d%d%", &n, &m);
char c[105];
for (i = 1; i <= n; i ++) {
for (j = 1; j <= m; j ++)
dp[i][j] = INT_MAX;
int sb;
scanf("%d", &sb);
gets(c);
int lenc = strlen(c);
c[lenc] = ' ';
int num = 0;
for (j = 0; j <= lenc; j ++) {
if (isdigit(c[j])) {
num = num * 10 + c[j] - '0';
}
else {
map[sb][num] = 1;
num = 0;
}
}
}
dp[1][1] = 0;
dfs(0, 1, 1);
printf("%d\n", ans);
if (t)
printf("\n");
}
return 0;
}
UVA 825 Walking 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 10581 - Partitioning for fun and profit(记忆化搜索+数论)
题目链接:uva 10581 - Partitioning for fun and profit 题目大意:给定m,n,k,将m分解成n份,然后依照每份的个数排定字典序,而且划分时要求ai−1≤ai, ...
- UVa 10285 Longest Run on a Snowboard - 记忆化搜索
记忆化搜索,完事... Code /** * UVa * Problem#10285 * Accepted * Time:0ms */ #include<iostream> #includ ...
- 【UVA 437】The Tower of Babylon(记忆化搜索写法)
[题目链接]:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...
- UVA 10285 Longest Run on a Snowboard(记忆化搜索)
Problem C Longest Run on a Snowboard Input: standard input Output: standard output Time Limit: 5 sec ...
- UVa 11762 Race to 1 (数学期望 + 记忆化搜索)
题意:给定一个整数 n ,然后你要把它变成 1,变换操作就是随机从小于等于 n 的素数中选一个p,如果这个数是 n 的约数,那么就可以变成 n/p,否则还是本身,问你把它变成 1 的数学期望是多少. ...
- UVa 825 - Walking on the Safe Side
题目:在一个N*M的网格中,从左上角走到右下角,有一些点不能经过,求最短路的条数. 分析:dp,帕斯卡三角.每一个点最短的就是走N条向下,M条向右的路. 到达每一个点的路径条数为左边和上面的路径之和. ...
- UVa 1252 Twenty Questions (状压DP+记忆化搜索)
题意:有n件物品,每件物品有m个特征,可以对特征进行询问,询问的结果是得知某个物体是否含有该特征,要把所有的物品区分出来(n个物品的特征都互不相同), 最小需要多少次询问? 析:我们假设心中想的那个物 ...
- UVA 10285 - Longest Run on a Snowboard (记忆化搜索+dp)
Longest Run on a Snowboard Input: standard input Output: standard output Time Limit: 5 seconds Memor ...
随机推荐
- [iOS] App引导页的简单实现 (Swift 2)
转载请注明出处:http://www.jianshu.com/p/024dd2d6e6e6# 已更新至 Xcode7.2.Swift2.1 在第一次打开App或者App更新后通常用引导页来展示产品特性 ...
- zend_db连接mysql(附完整代码)(转)
在看这些之前请确保你正确加载了PDO扩展. 作法是编辑php.ini手动增加下面这两行(前面要没有分号;):extension=php_pdo.dllextension=php_pdo_mysql.d ...
- POJ3692 Kindergarten 【最大独立集】
Kindergarten Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 5317 Accepted: 2589 Desc ...
- MySQL与mabits大小比较、日期比较示例
首先,使用mysql查询从今往后的60天数据 SELECT count(*), b1.record_date FROM nk_house_use_record AS b1, ( SELECT a.th ...
- linux 常用find命令
1.查找当前目录下以test开头的所有文件-会进入子目录中去查找 [root@rusky hgfs]# find -name test* 2.查找当前目录下名为test.txt的文件-会进入子目录中去 ...
- javascript 实现jsonp
jsonp原理其实也简单,虽然ajax不能跨域,但是通过src这个属性我们可以实现跨域,其实和我们引入第三方jquery调用它的方法一样的. html: <!DOCTYPE html> & ...
- Ubuntu下安装android studio的时候,无法进入图形界面--/usr/lib/jdk1.8.0_60/jre/lib/i386/libawt_xawt.so: libXtst.so.6: 无法打开共享对象文件: 没有那个文件或目录
详细错误描述: java.lang.reflect.InvocationTargetException at sun.reflect.NativeMethodAccessorImpl.invok ...
- C#类中字段,属性与方法
person类 using System; using System.Collections.Generic; using System.Linq; using System.Text; using ...
- 深入剖析C/C++函数的参数传递机制
2014-07-29 20:16 深入剖析C/C++函数的参数传递机制 C语言的函数入口参数,可以使用值传递和指针传递方式,C++又多了引用(reference)传递方式.引用传递方式在使用上类 ...
- LLVM对注释的新增支持 @ WWDC 2013
很久之前我就在想:“我应该按照什么格式写注释,才能像Apple官方API那样按住Option键并点击函数名可以跳出文档说明”,如下图: 我理所当然地认为这个功能应该是根据现有注释的格式来进行排版的,于 ...