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. mybatis一对多,多对一

    假设两张表 person对order为一对多 实体类 person package com.kerwin.mybatis.pojo; import java.util.List; public cla ...

  2. C#基础语法总结

    C#笔记——基础篇 一.入门知识 VS中的常用快捷键 Ctrl+K+D:快速对齐代码 Ctrl+Z:撤销 Ctrl+S:保存(一定要经常保存!) Ctrl+J:快速弹出智能提示 Shift+End . ...

  3. (七)《Java编程思想》——多态的缺陷

    1.不能“覆盖”私有方法 package chapter8; /** * 不能"覆盖"私有方法 */ public class PrivateOverride { private ...

  4. VS2010字体优化

    文本编辑器:Consolas 环境字体:微软雅黑

  5. 华为 oj 表示数字(代码有参考)理解算法设计

    虽然是初级题目,但是 也不是太容易就做出来的 还是用c++ 好些 因为c++ string 是可以存储到缓冲区的, 字符串长度可以变化 参考了某神的代码 和我的思路一样 ,就拿来主义了,挺经典的一道面 ...

  6. POJ 1064 Cable master(二分查找+精度)(神坑题)

    POJ 1064 Cable master 一开始把 int C(double x) 里面写成了  int C(int x) ,莫名奇妙竟然过了样例,交了以后直接就wa. 后来发现又把二分查找的判断条 ...

  7. javascript中的substr和substring

    1.substr 方法 返回一个从指定位置开始的指定长度的子字符串. stringvar.substr(start [, length ]) 参数: stringvar 必选项.  要提取子字符串的字 ...

  8. curl抓取信息

    <?php $hotel = new curl(false,0); $str = $hotel -> post("http://www.todayinns.com/login.p ...

  9. ecshop代码详解之init.php

    在includes/init.php目录下 因为工作原因,需要对ecshop二次开发,顺便记录一下对ecshop源代码的一些分析: 首先是init.php文件,这个文件在ecshop每个页面都会 调用 ...

  10. C语言基础学习基本数据类型-其他整数类型

    其他整数类型 初学C语言时,int类型会满足你对整数的大多数需求. C语言还提供了三个关键字用以修饰基本的整数类型:short.long和unsigned.有以下几个注意点: (1)C语言没有具体规定 ...