2014-03-20 03:35

题目:实现画图的Flood Fill操作。

解法:DFS和BFS皆可,但BFS使用的队列在时间复杂度上常数项比较大,速度略慢,所以我选了DFS。当然,如果图很大的话DFS是会导致call stack溢出的,那就摊上事儿了。

代码:

 // 9.7 Implement a flood fill painter that changes a certain area to a certain color. You are given one point as the seed.
#include <cstdio>
#include <vector>
using namespace std; void floodFill(int i, int j, int n, int m, int new_color, vector<vector<int> > &canva)
{
int old_color = canva[i][j]; canva[i][j] = new_color;
if (i >= && old_color == canva[i - ][j]) {
floodFill(i - , j, n, m, new_color, canva);
}
if (i <= n - && old_color == canva[i + ][j]) {
floodFill(i + , j, n, m, new_color, canva);
}
if (j >= && old_color == canva[i][j - ]) {
floodFill(i, j - , n, m, new_color, canva);
}
if (j <= m - && old_color == canva[i][j + ]) {
floodFill(i, j + , n, m, new_color, canva);
}
} int main()
{
int i, j, c;
int n, m;
vector<vector<int> > canva; scanf("%d%d", &n, &m);
canva.resize(n);
for (i = ; i < n; ++i) {
canva[i].resize(m);
} for (i = ; i < n; ++i) {
for (j = ; j < m; ++j) {
scanf("%d", &canva[i][j]);
}
} while (scanf("%d%d%d", &i, &j, &c) == ) {
if (i >= && i <= n - && j >= && j <= m - ) {
floodFill(i, j, n, m, c, canva);
}
for (i = ; i < n; ++i) {
for (j = ; j < m; ++j) {
printf((j == ? "%d" : " %d"), canva[i][j]);
}
printf("\n");
}
} for (i = ; i < n; ++i) {
canva[i].clear();
}
canva.clear(); return ;
}

《Cracking the Coding Interview》——第9章:递归和动态规划——题目7的更多相关文章

  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. java核心-流基础

    1.流的概念: 1.1输入流:向内存中写入数据 1.2输出流:有内存向指定的地方输出数据 2.

  2. 如何将windows日志转成syslog格式并发到远程sysylog服务器

      安装Snare, 随便找了个版本下载下来,安装一路next,除了中间让你输入一次http的管理登录口令.   2,配置 之后打开URL:http://192.168.37.23:6161/,输入默 ...

  3. S7-1500 读取V90/S120/S210/G120的常用驱动参数

    S7-1500 读取V90/S120的常用驱动参数 此程序已更新,可以下载例子程序 https://files.cnblogs.com/files/lion-zheng/PLC_async_drive ...

  4. MHA故障切换方案

    mha故障切换当master 挂掉后会自动切换到slave01上去,可以看 tailf /var/mha_log/manager.log4个问题:1,切换后且原master01mysql服务启动OK, ...

  5. 如果有反向代理的情况下,获取最原始的IP的办法

    HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_HOST"];

  6. 引用类型(二):Array类型

    一.js中的数组与其它语言中的数组的区别1.ECMAScript数组的每一项可以保存任何类型的数据2.ECMAScript数组的大小是可以动态调整的 二.创建数组的基本方式1.使用Array构造函数 ...

  7. 使用正则表达式验证IP地址

    实现效果: 知识运用: 实现代码: public bool validate(string str_IP) { string regex = @"(25[0-5]|2[0-4]\d|[0-1 ...

  8. mysql随机字符串函数

    drop function if exists rand_str; delimiter $$ ) charset 'utf8' begin # 定义接收初始化类型 ) ; # 定义初始化数字 ) '; ...

  9. 三、Object-C内存管理

    一.管理范围:任何继承了NSObject的对象,对基本数据类型无效 原理: 1.每个对象都有个引用计数器,是一个与之关联的整数 2.使用了 3.给对象发送一条retain消息,可以使对计数器+1 4. ...

  10. 动态生成的DOM做点击事件无效

    有时候我们的标签都是从后台获取的数据,然后利用JS添加到页面上,当我们写生成的标签的点击事件(click)时没有效果. 例如: <section> 测试动态生成的DOM点击事件 <b ...