《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 ...
随机推荐
- Js arguments.callee();函数自己调用自己
1.阶乘的时候,函数一般要用到递归算法,所以函数内部一定会调用自身 //递归,阶乘 function sum(num){ ) { ; } else{ ); //自己调用自己,递归 } } alert( ...
- leetcode: 复杂度
1. single-number Given an array of integers, every element appears twice except for one. Find that s ...
- Selenium入门系列1 打开浏览器访问网页,退出浏览器
对于功能自动化的理解就是用测试工具替代手工.手工怎么操作的,工具也如何操作. 手工测试:在前置条件下,执行一定的操作步骤>与预期结果对比 功能自动化:在前置条件下,识别对象 >操作对象&g ...
- bzoj1801 [Ahoi2009]中国象棋
Description 在N行M列的棋盘上,放若干个炮可以是0个,使得没有任何一个炮可以攻击另一个炮. 请问有多少种放置方法,中国像棋中炮的行走方式大家应该很清楚吧. Input 一行包含两个整数N, ...
- 【转】NodeJS教程--基于ExpressJS框架的文件上传
本文是翻译的一篇文章,原文地址:Handle File Uploads in Express (Node.js). 在NodeJS发展早期上传文件是一个较难操作的功能,随后出现了formidable. ...
- Spring使用Setter依赖注入
一个简单的Spring例子来展示如何通过setter方法注入依赖项,最常用DI方法注入bean. 1. IOutputGenerator 接口和实现类 package faj.test.javad ...
- P1290 【欧几里德的游戏】
P1290 [欧几里德的游戏] 真·做题全凭感性 从题目中很容易看出 这是一道\(Gcd\)的题 同时又结合了一些略略的博弈论(丢下锅跑真爽 我们看,辗转相减的\(a,b\)一共只有两种情况 \(a- ...
- (转)ActionContext和ServletActionContext
前面已经了解到ActionContext是Action执行时的上下文,里面存放着Action在执行时需要用到的对象,我们也称之为广义值栈. Struts2在每次执行Action之前都会创建新的Acti ...
- 子div作为遮罩层
效果图: 子div的代码:
- css 伪类选择器制作登录框表单
使用伪类选择器 制作鼠标悬停时文本框出现橙色虚线边框 制作鼠标激活时出现背景颜色淡橙色 使用css制作文本框圆角矩形效果,制作文本框背景图片,及背景不重复效果 <!DOCTYPE html> ...