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. 笨办法学Python(三十六)

    习题 36: 设计和调试 现在你已经学会了“if 语句”,我将给你一些使用“for 循环”和“while 循环”的规则,一面你日后碰到麻烦.我还会教你一些调试的小技巧,以便你能发现自己程序的问题.最后 ...

  2. s7nodave用于上位机连接西门子PLC,开源项目epics

    s7nodave 可以看作是Prodave的开源替代者,在PLC侧,不需要编程 This device support does not require any special programming ...

  3. [转载]弹出一个不带地址栏、工具栏的IE非模态窗口

    标签:ie /非模态窗口 window.open(url,'_blank','menubar=no,fullscreen=1,toolbar=no,resizable=no,location=no,s ...

  4. IOS NSThread(线程同步)

    @interface HMViewController () /** 剩余票数 */ @property (nonatomic, assign) int leftTicketsCount; @prop ...

  5. 前端高质量知识(四)-JS详细图解作用域链与闭包

    攻克闭包难题 初学JavaScript的时候,我在学习闭包上,走了很多弯路.而这次重新回过头来对基础知识进行梳理,要讲清楚闭包,也是一个非常大的挑战. 闭包有多重要?如果你是初入前端的朋友,我没有办法 ...

  6. python 爬糗事百科

    糗事百科网站段子爬取,糗事百科是我见过的最简单的网站了!!! #-*-coding:utf8-*- import requests import re import sys reload(sys) s ...

  7. 火车进出站(POJ1363)

    题目链接:http://poj.org/problem?id=1363 #include <stdio.h> #include <stack> using namespace ...

  8. 【转】Android BroadcastReceiver介绍

    本文主要介绍BroadcastReceiver的概念.使用.生命周期.安全性.分类.特殊的BroadcastReceiver(本地.粘性.有序.粘性有序广播).示例代码见BroadcastReceiv ...

  9. python 爬取猫眼榜单100(二)--多个页面以及多进程

    #!/usr/bin/env python # -*- coding: utf- -*- # @Author: Dang Kai # @Date: -- :: # @Last Modified tim ...

  10. 2018.7.23 oracle中的CLOB数据类型

    Oarcle中的LOB类型 1.在Oracle中,LOB(Large Object,大型对象)类型的字段现在用得越来越多了.因为这种类型的字段,容量大(最多能容纳4GB的数据),且一个表中可以有多个这 ...