《Cracking the Coding Interview》——第9章:递归和动态规划——题目2
2014-03-20 02:55
题目:从(0, 0)走到(x, y),其中x、y都是非负整数。每次只能向x或y轴的正方向走一格,那么总共有多少种走法。如果有些地方被障碍挡住不能走呢?
解法1:如果没有障碍的话,组合数学,排列组合公式C(x + y, x)。
代码:
// 9.2 How many ways are there to go from (0, 0) to (x, y), if you only go left or up, and one unit at a time?
#include <cstdio>
using namespace std; int combination(int n, int k)
{
if (n < k) {
return ;
} else if (n == ) {
return ;
} else if (k > n / ) {
return combination(n, n - k);
} int i;
int res, div; res = div = ;
for (i = ; i <= k; ++i) {
res *= (n + - i);
div *= i;
if (res % div == ) {
res /= div;
div = ;
}
}
if (res % div == ) {
res /= div;
div = ;
} return res;
} int main()
{
int x, y; while (scanf("%d%d", &x, &y) == && x >= && y >= ) {
printf("%d\n", combination(x + y, x));
} return ;
}
解法2:如果有障碍的话,用动态规划。
代码:
// 9.2 How many ways are there to go from (0, 0) to (x, y), if you only go left or up, and one unit at a time?
// If some of the positions are off limits, what would you do?
#include <cstdio>
#include <vector>
using namespace std; int main()
{
int x, y;
int i, j;
vector<vector<int> > off_limits;
vector<vector<int> > res; while (scanf("%d%d", &x, &y) == && x >= && y >= ) {
++x;
++y;
off_limits.resize(x);
res.resize(x);
for (i = ; i < x; ++i) {
off_limits[i].resize(y);
res[i].resize(y);
}
for (i = ; i < x; ++i) {
for (j = ; j < y; ++j) {
scanf("%d", &off_limits[i][j]);
off_limits[i][j] = !!off_limits[i][j];
res[i][j] = ;
}
} res[][] = off_limits[][] ? : ;
for (i = ; i < x; ++i) {
res[i][] = off_limits[i][] ? : res[i - ][];
}
for (j = ; j < y; ++j) {
res[][j] = off_limits[][j] ? : res[][j - ];
}
for (i = ; i < x; ++i) {
for (j = ; j < y; ++j) {
res[i][j] = off_limits[i][j] ? : res[i - ][j] + res[i][j - ];
}
} for (i = ; i < x; ++i) {
for (j = ; j < y; ++j) {
printf("%d ", res[i][j]);
}
printf("\n");
}
printf("%d\n", res[x - ][y - ]); for (i = ; i < x; ++i) {
off_limits[i].clear();
res[i].clear();
}
off_limits.clear();
res.clear();
} return ;
}
《Cracking the Coding Interview》——第9章:递归和动态规划——题目2的更多相关文章
- Cracking the coding interview 第一章问题及解答
Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...
- 《Cracking the Coding Interview》读书笔记
<Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...
- Cracking the coding interview
写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...
- Cracking the Coding Interview(Trees and Graphs)
Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...
- Cracking the Coding Interview(Stacks and Queues)
Cracking the Coding Interview(Stacks and Queues) 1.Describe how you could use a single array to impl ...
- Cracking the coding interview目录及资料收集
前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...
- 二刷Cracking the Coding Interview(CC150第五版)
第18章---高度难题 1,-------另类加法.实现加法. 另类加法 参与人数:327时间限制:3秒空间限制:32768K 算法知识视频讲解 题目描述 请编写一个函数,将两个数字相加.不得使用+或 ...
- 《Cracking the Coding Interview》——第9章:递归和动态规划——题目11
2014-03-21 20:20 题目:给定一个只包含‘0’.‘1’.‘|’.‘&’.‘^’的布尔表达式,和一个期望的结果(0或者1).如果允许你用自由地给这个表达式加括号来控制运算的顺序,问 ...
- 《Cracking the Coding Interview》——第9章:递归和动态规划——题目10
2014-03-20 04:15 题目:你有n个盒子,用这n个盒子堆成一个塔,要求下面的盒子必须在长宽高上都严格大于上面的.如果你不能旋转盒子变换长宽高,这座塔最高能堆多高? 解法:首先将n个盒子按照 ...
- 《Cracking the Coding Interview》——第9章:递归和动态规划——题目9
2014-03-20 04:08 题目:八皇后问题. 解法:DFS解决. 代码: // 9.9 Eight-Queen Problem, need I say more? #include <c ...
随机推荐
- May 04th 2017 Week 18th Thursday
No matter how far you may fly, never forget where you come from. 无论你能飞多远,都别忘了你来自何方. I never forget w ...
- POJ-1509 Glass Beads---最小表示法模板
题目链接: https://vjudge.net/problem/POJ-1509 题目大意: 给你一个循环串,然后找到一个位置,使得从这个位置开始的整个串字典序最小. 解题思路: 最小表示法模板 注 ...
- 砍树,POJ(2665)
题目链接:http://poj.org/problem?id=2665 解题报告: 这里的区域没有重复,若有重复的话,模拟即可. #include <cstdio> #include &l ...
- 离散外微积分(DEC:Discrete Exterior Calculus)基础
原文链接 “若人们不相信数学简单,只因为他们未意识到生命之复杂.”——Johnvon Neumann DEC主要讨论离散情况下的外积分,它在计算机领域有重要用途.我们知道,使用计算机来处理几何图形的时 ...
- android design 新控件
转载请标明出处: http://blog.csdn.net/forezp/article/details/51873137 本文出自方志朋的博客 最近在研究android 开发的新控件,包括drawe ...
- Java日志框架介绍
一.序言 日志为系统的必不可少的一部分,通过输出的日志我们可以排查线上出现的各种问题,就像断案的线索一样.我们还可以通过日志数据分析用户的行为习惯做大数据分析. 二.日志框架分类及其历史 框架的种类: ...
- Angular项目下载启动
Angular cmdb-front 新建项目流程 打开idea New>Project 2,打开项目仓库,拷贝项目地址 拷贝项目路径,填写项目名 点击clone,然后一路Next 项目代码已 ...
- tomcat.apache startup.bat闪退两种解决方法
tomcat bin文件夹中的startup.bat闪退原因及解决方法两种 方法一:在启动tomcat时闪退,重新检查java的jre运行环境.如果环境变量忘记配置一定会导致了tomcat的闪退. 追 ...
- Hive[6] HiveQL 查询
6.1 SELECT ... FROM 语句 hive> SELECT name,salary FROM employees; --普通查询 hive>SELECT e.n ...
- UISearchController(使用)
效果图1 效果图2 其实一般是在第一种情况使用的UISearchController,但是第二种情况这种又懒得去用uisearchbar+uitableview等等去处理, 其实主要是对数据源的合理使 ...