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的更多相关文章

  1. Cracking the coding interview 第一章问题及解答

    Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...

  2. 《Cracking the Coding Interview》读书笔记

    <Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...

  3. Cracking the coding interview

    写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...

  4. Cracking the Coding Interview(Trees and Graphs)

    Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...

  5. 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 ...

  6. Cracking the coding interview目录及资料收集

    前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...

  7. 二刷Cracking the Coding Interview(CC150第五版)

    第18章---高度难题 1,-------另类加法.实现加法. 另类加法 参与人数:327时间限制:3秒空间限制:32768K 算法知识视频讲解 题目描述 请编写一个函数,将两个数字相加.不得使用+或 ...

  8. 《Cracking the Coding Interview》——第9章:递归和动态规划——题目11

    2014-03-21 20:20 题目:给定一个只包含‘0’.‘1’.‘|’.‘&’.‘^’的布尔表达式,和一个期望的结果(0或者1).如果允许你用自由地给这个表达式加括号来控制运算的顺序,问 ...

  9. 《Cracking the Coding Interview》——第9章:递归和动态规划——题目10

    2014-03-20 04:15 题目:你有n个盒子,用这n个盒子堆成一个塔,要求下面的盒子必须在长宽高上都严格大于上面的.如果你不能旋转盒子变换长宽高,这座塔最高能堆多高? 解法:首先将n个盒子按照 ...

  10. 《Cracking the Coding Interview》——第9章:递归和动态规划——题目9

    2014-03-20 04:08 题目:八皇后问题. 解法:DFS解决. 代码: // 9.9 Eight-Queen Problem, need I say more? #include <c ...

随机推荐

  1. MYSQL:随机抽取一条数据库记录

    今天我们要实现从随机抽取一条数据库记录的功能,并且抽取出来的数据记录不能重复: 1.首先我们看文章表中的数据: 2.实现功能代码如下: 1 /** * 获取随机的N篇文篇 * @param int $ ...

  2. 同步软件UltraCompare 64位 软件及注册机

    软件及注册机下载: https://share.weiyun.com/f09e6243887e374ead1b3a3ab8f611a9 软件官方下载地址:  https://www.ultraedit ...

  3. Node.js与npm安装(转载)

    2009年的JSCOnf大会上,一个叫Ryan Dahl的年轻程序员向人们展示了一个他正在做的项目,一个基于Google V8引擎的JavaScript运行平台,它提供了一套事件循环和低IO的应用程序 ...

  4. 如何实现SQL Server临时表的创建?

    以下的文章主要是对SQL Server临时表的创建的实际操作步骤,以及在实际操作中我们要用到的实际应用代码的介绍,我在一个信誉度很好的网站找到一个关于其相关内容今天拿出来供大家分享. 创建临时表 方法 ...

  5. CentOS安装配置MongoDB

    1.下载安装包: wget http://fastdl.mongodb.org/linux/mongodb-linux-i686-2.0.3.tgz 2.解压: tar -zxvf mongodb-l ...

  6. selenium+chrome下载文件,格式怎么选择???

    学习了下载 if browser == "Chrome": options=webdriver.ChromeOptions() prefs={'profile.default_co ...

  7. python_4_interaction

    #1(方法1)尽量不用这种拼接法,效率低下,占用内存多 name=input("name:") age=input('age:') job=input('job:') salary ...

  8. mysql关键字了解

    unsigned  无符号 就是没有负数 列-1  -2 auto_increment 自增 comment 注释 primary key 主键 foreign key ()   references ...

  9. 第13章 GPIO-位带操作—零死角玩转STM32-F429系列

    第13章     GPIO—位带操作 全套200集视频教程和1000页PDF教程请到秉火论坛下载:www.firebbs.cn 野火视频教程优酷观看网址:http://i.youku.com/fire ...

  10. Java开发.gitignore文件包含.iml,.log的看法

    有一个开源项目https://github.com/github/gitignore 主要用来规范所有开发项目的.gitignore文件的编写,基本涵盖了所有的开发语言.开发环境等.今日我向JetBr ...