博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
UVA 825 Walking on the Safe Side(记忆化搜索)
阅读量:6406 次
发布时间:2019-06-23

本文共 3246 字,大约阅读时间需要 10 分钟。

 

  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.

 

\epsfbox{p825.eps}

 

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 

14 512 23 3 54

 

Sample Output 

4

题意:一个人要从左上角走到右下角。中间有些点是不能走的,要求出最小步数的路径有多少条。

思路:记忆化搜索,注意输入格式,还有方向只能向下和向右。

 

#include 
#include
#include
#include
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;}

 

 

转载地址:http://vdxea.baihongyu.com/

你可能感兴趣的文章
kvm中虚拟机的硬盘扩容
查看>>
Android (Launch Mode) 四种启动模式
查看>>
透视学理论(二)
查看>>
Dubbo/HSF在Service Mesh下的思考和方案
查看>>
Django form表单
查看>>
CTYL-9.14(tomcat端口与阿里云安全组,域名与tomcat配置,域名与反向代理)
查看>>
Java 多线程相关问题记录
查看>>
LNMP架构介绍、MySQL安装、PHP安装、 Nginx介绍
查看>>
es6 class 笔记
查看>>
简单的Spark+Mysql整合开发
查看>>
阿里java面试经验大汇总(附阿里职位需求)
查看>>
Python全套零基础视频教程+软件2018最新编程视频!
查看>>
内存管理之1:x86段式内存管理与保护模式
查看>>
20180925上课截图
查看>>
IO输入/输出流的简单总结
查看>>
JavaScript之DOM-9 HTML DOM(HTML DOM概述、常用HTML DOM对象、HTML表单)
查看>>
技术成长之路(一)
查看>>
中国北方国际五金城硬件选型
查看>>
Discuz X2 [Type] 查询语句安全威胁
查看>>
php.exe启动时提示缺少MVCR110.dall 64位 window系统 解决
查看>>