《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 ...
随机推荐
- MYSQL导入excel
MYSQL使用navicat导入excel 第一步:首先需要准备好有数据的excel 第二步:选择"文件"->"另存为",保存为"CSV(逗号分 ...
- 数组:获取GET的键名
1.今天仓鼠遇到这个情况:通过$_GET获取参数,但是参数变成了键名形式 2.那仓鼠想要拿到这个键名,那就要:使用array_keys()获取数组中的所有键名,然后进行转换 代码如下: 结果: 以上 ...
- java Date equals 的坑
今天在JDK6上做开发,遇到一个很诡异的问题. Domain中一个实体是Date,称为变量 a, 使用Calendar构造出来的Date,称为变量b, 虽然都是同一天,比如 2016-11-11 00 ...
- 【转载】#330 - Derived Classes Do Not Inherit Constructors
A derived class inherits all of the members of a base class except for its constructors. You must de ...
- .net core 下调用.net framework框架的WCF方法写法
通过添加服务引用后生成的代码,可以得知首先要设置Basic连接写法的属性,并且设置WCF服务的地址: 我在这里建立工厂类如下: using System; using System.ServiceMo ...
- Miller rabin
蛤蛤,终于基本上搞懂了 #include<iostream> #include<cstdio> using namespace std; long long num[10]={ ...
- git fetch和push的区别
获取fetch的用法 git-fetch用于从另一个reposoitory下载objects和refs. 命令格式为:git fetch … 其中表示远端的仓库路径.git remote add or ...
- vue项目不能同时被localhost和ip地址同时访问的方法
方法1.将index.js中的host的值改为‘0.0.0.0’ 方法2.修改package.json中script下dev的值,在后面加入--host 0.0.0.0 也可以解决
- LeetCode706. Design HashMap
题目 不使用任何内建的哈希表库设计一个哈希映射 具体地说,你的设计应该包含以下的功能 put(key, value):向哈希映射中插入(键,值)的数值对.如果键对应的值已经存在,更新这个值. get ...
- 【jQeury】input输入框状态,input事件,blur事件,focus事件
//输入框正在输入时 $("#test1").on('input',function(){ alert('正在输入'); }) //输入框得到焦点时 $("#test2& ...