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 2159 FATE(全然背包+二维费用背包)

    FATE Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submi ...

  2. android怎样写一个循环文字滚动的TextView

    效果图: 在layout中这样来声明: <com.kaixin001.view.ScrollText android:id="@+id/news_statustxt" and ...

  3. samba服务简介

    1.1 Samba 概述Samba主要用于Windows和Linux之间的文件共享,也一样用于Linux和Linux之间的共享文件:不过对于Linux和Linux之间共享文件有更好的网络文件系统NFS ...

  4. Linux 数据 CD 刻录

    http://www.cyberciti.biz/tips/linux-burning-multi-session-cds-on-linux.html #mkisofs -dvd-video -inp ...

  5. python 下的数据结构与算法---3:python内建数据结构的方法及其时间复杂度

    目录 一:python内部数据类型分类 二:各数据结构 一:python内部数据类型分类 这里有个很重要的东西要先提醒注意一下:原子性数据类型和非原子性数据类型的区别 Python内部数据从某种形式上 ...

  6. Hyper-v虚拟机上网

    Windows 8中内置的Hyper-V管理器可以说给许多人带来了惊喜!在Hyper-V管理器强大的同时,也同样面临着设置中一些不可避免的麻烦.有人说,Hyper-V虚拟机联网麻烦,其实,只要掌握了技 ...

  7. C#生成高清缩略图

    /// <SUMMARY> /// 为图片生成缩略图 /// </SUMMARY> /// <PARAM name="phyPath">原图片的 ...

  8. 在iOS虚拟机上使CLPlacemark获取中文信息

    -(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{ CLLocat ...

  9. MySql用statement实现DDL,DML,DQL的操作Demo

    Demo1 Connection connection=null; Statement stmt=null; int result=-1; try { Class.forName("com. ...

  10. 本地windows下PHP连接远程oracle遇到的诸多问题

    任务目的:本地windows下PHP连接远程服务器下的oracle. 必须必须 确定服务器的数据库版本,如果本地的驱动和对方服务器版本不一致,会导致许多报错. 已知的oracle版本  分为 32位的 ...