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. 如何用代码填充S/4HANA销售订单行项目的数量字段

    我的任务是用代码生成S/4HANA销售订单(Sales Order)的行项目,并且填充对应的quantity(数量)值. 最开始我用了下面的代码,把quantity的值写入item字段target_q ...

  2. linux lnmp搭建

    1.安装nginx: yum install gcc -y yum install -y pcre pcre-devel yum install -y zlib zlib-devel yum inst ...

  3. CORS跨域请求的限制和解决

    我们模拟一个跨域的请求,一个是8888,一个是8887 //server.js const http = require('http'); const fs = require('fs'); http ...

  4. matlab的figure窗口命名为中文

    figure('NumberTitle', 'off', 'Name', '我的窗口名字');

  5. git常用命令图解

  6. java对象传递小解析

    先上代码: import org.apache.commons.lang.builder.ToStringBuilder; import org.apache.commons.lang.builder ...

  7. 史上最简单的SpringCloud教程 | 第八篇: 消息总线(Spring Cloud Bus)

    转载请标明出处: 原文首发于:https://www.fangzhipeng.com/springcloud/2017/07/12/sc08-bus/ 本文出自方志朋的博客 最新Finchley版本请 ...

  8. jquery 操作css 选择器

    .addClass() 为每个匹配的元素添加指定的样式类名 .addClass(className) className 为每个匹配元素所有增加的一个或多个样式名 .addClass(function ...

  9. Python学习之购物车

    实现功能: 程序启动,提示用户输入用户名和密码,程序读取余额文件last_salary.txt内容(文件不存在则自动创建),若文件内容为空则提示“首次登录,请输入工资”: 用户可以输入商品编号进行购买 ...

  10. JAVA / MySql 编程——第五章 事务、视图、索引、备份和恢复

    1.事务(Transaction): 事务是将一系列数据操作绑成一个整体进行统一管理. 如果一事务执行成功,则咋子该事务中进行的所有数据更改均会提交,称为数据库中的永久成部分. 如果事务执行是遇到错误 ...