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(记忆化搜索)的更多相关文章

  1. uva 825 - Walking on the Safe Side(dp)

    题目链接:825 - Walking on the Safe Side 题目大意:给出n,m,现在给出n行数据, 每行有k(k为不定值)个数字, 第一个数字代表行数, 后面k - 1个数代表当前行的这 ...

  2. uva 10581 - Partitioning for fun and profit(记忆化搜索+数论)

    题目链接:uva 10581 - Partitioning for fun and profit 题目大意:给定m,n,k,将m分解成n份,然后依照每份的个数排定字典序,而且划分时要求ai−1≤ai, ...

  3. UVa 10285 Longest Run on a Snowboard - 记忆化搜索

    记忆化搜索,完事... Code /** * UVa * Problem#10285 * Accepted * Time:0ms */ #include<iostream> #includ ...

  4. 【UVA 437】The Tower of Babylon(记忆化搜索写法)

    [题目链接]:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...

  5. UVA 10285 Longest Run on a Snowboard(记忆化搜索)

    Problem C Longest Run on a Snowboard Input: standard input Output: standard output Time Limit: 5 sec ...

  6. UVa 11762 Race to 1 (数学期望 + 记忆化搜索)

    题意:给定一个整数 n ,然后你要把它变成 1,变换操作就是随机从小于等于 n 的素数中选一个p,如果这个数是 n 的约数,那么就可以变成 n/p,否则还是本身,问你把它变成 1 的数学期望是多少. ...

  7. UVa 825 - Walking on the Safe Side

    题目:在一个N*M的网格中,从左上角走到右下角,有一些点不能经过,求最短路的条数. 分析:dp,帕斯卡三角.每一个点最短的就是走N条向下,M条向右的路. 到达每一个点的路径条数为左边和上面的路径之和. ...

  8. UVa 1252 Twenty Questions (状压DP+记忆化搜索)

    题意:有n件物品,每件物品有m个特征,可以对特征进行询问,询问的结果是得知某个物体是否含有该特征,要把所有的物品区分出来(n个物品的特征都互不相同), 最小需要多少次询问? 析:我们假设心中想的那个物 ...

  9. UVA 10285 - Longest Run on a Snowboard (记忆化搜索+dp)

    Longest Run on a Snowboard Input: standard input Output: standard output Time Limit: 5 seconds Memor ...

随机推荐

  1. HDU 1853Cyclic Tour(网络流之最小费用流)

    题目地址:pid=1853">HDU1853 费用流果然好奇妙. .还能够用来推断环...假设每一个点都是环的一部分并且每一个点仅仅能用到一次的话,那每一个点的初度入度都是1,这就能够 ...

  2. ASP.NET静态页生成方法(模板替换)

    本文实例讲述了ASP.NET静态页生成方法的一种简单方法,就是替换内容法. 适用场景 模板比较固定,页面替换内容较少. 基本原理 此方法中静态页生成用到的就是匹配跟替换了,首先得读取模板页的html内 ...

  3. android高仿微信UI点击头像显示大图片效果

    用过微信的朋友朋友都见过微信中点击对方头像显示会加载大图,先贴两张图片说明下: 这种UI效果对用户的体验不错,今天突然有了灵感,试着去实现,结果就出来了.. 下面说说我的思路: 1.点击图片时跳转到另 ...

  4. jquery之checkbox

    //checkbox 数据回显 var publishRange=rowData.publishRange.split(","); for(var i = 0;i < pub ...

  5. (转)url重写

    使用URLRewriter.dll后,根本不需要使用任何代码,我之前做的项目就是用的做URL重写的,其实不是进化,其实表面上看是.html扩展名而已,当然你还可以用其他的任意扩展名下面是你的配置 &l ...

  6. IE9的window.showmodaldialog显示问题

    <html xmlns="http://www.w3.org/1999/xhtml"> <head id="Head1" runat=&quo ...

  7. ionic开发ios app

    注意必须是mac系统 1. 首先要安装node环境,Ionic的安装和后续的许多前端工具的安装都依赖于node的包管理器npm. nodeJs环境的安装很简单,去官网下载最新版的NodeJs直接安装即 ...

  8. linux下enum的使用

    enum T { status1, status2, } Linux下: 1.做函数返回值时enum T f():不能写成T f(): 2.if(i == status1)不能写成 if(i == T ...

  9. jQuery1.9(辅助函数)学习之—— jQuery.param( obj ); 编辑

    jQuery.param( obj );  返回一个String 描述: 创建一个数组或对象序列化的的字符串,适用于一个URL 地址查询字符串或Ajax请求. jQuery.param( obj ); ...

  10. jQuery ajax传递特殊字符串问题

    当你用ajax传递值到服务器端,如果值中包含特殊字符串如+,&等,在服务器端获取的结果可能就会出现差异,因为这些字符有其它用途,如“+”表示连接符,在转义后你获取到的就是空格.可以看看这些特殊 ...