2014-05-02 00:22

题目链接

原题:

Given a matrix consisting of 's and 1's, find the largest connected component consisting of 's.

题目:给定一个01矩阵,找出由1构成的连通分量中最大的一个。

解法:四邻接还是八邻接也不说,我就当四邻接了。这种Flood Fill问题,可以用BFS或者DFS解决。

代码:

 // http://www.careercup.com/question?id=5998719358992384
#include <vector>
using namespace std; class Solution {
public:
int maxConnectedComponent(vector<vector<int> > &v) {
n = (int)v.size();
if (n == ) {
return ;
}
m = (int)v[].size();
if (m == ) {
return ;
} int res, max_res; max_res = ;
for (i = ; i < n; ++i) {
for (j = ; j < m; ++j) {
if (!v[i][j]) {
continue;
}
res = ; v[i][j] = ;
++res;
DFS(v, i, j, res);
max_res = res > max_res ? res : max_res;
}
} return max_res;
};
private:
int n, m; void DFS(vector<vector<int> > &v, int i, int j, int &res) {
static const int d[][] = {
{-, },
{ , -},
{+, },
{ , +}
}; int k, x, y;
for (k = ; k < ; ++k) {
x = i + d[k][];
y = j + d[k][];
if (x < || x > n - || y < || y > m - ) {
continue;
}
if (!v[x][y]) {
continue;
}
v[x][y] = ;
++res;
DFS(v, x, y, res);
}
};
};

Careercup - Facebook面试题 - 5998719358992384的更多相关文章

  1. Careercup - Facebook面试题 - 6026101998485504

    2014-05-02 10:47 题目链接 原题: Given an unordered array of positive integers, create an algorithm that ma ...

  2. Careercup - Facebook面试题 - 5344154741637120

    2014-05-02 10:40 题目链接 原题: Sink Zero in Binary Tree. Swap zero value of a node with non-zero value of ...

  3. Careercup - Facebook面试题 - 5765850736885760

    2014-05-02 10:07 题目链接 原题: Mapping ' = 'A','B','C' ' = 'D','E','F' ... ' = input: output :ouput = [AA ...

  4. Careercup - Facebook面试题 - 5733320654585856

    2014-05-02 09:59 题目链接 原题: Group Anagrams input = ["star, astr, car, rac, st"] output = [[& ...

  5. Careercup - Facebook面试题 - 4892713614835712

    2014-05-02 09:54 题目链接 原题: You have two numbers decomposed in binary representation, write a function ...

  6. Careercup - Facebook面试题 - 6321181669982208

    2014-05-02 09:40 题目链接 原题: Given a number N, write a program that returns all possible combinations o ...

  7. Careercup - Facebook面试题 - 5177378863054848

    2014-05-02 08:29 题目链接 原题: Write a function for retrieving the total number of substring palindromes. ...

  8. Careercup - Facebook面试题 - 4907555595747328

    2014-05-02 07:49 题目链接 原题: Given a set of n points (coordinate in 2d plane) within a rectangular spac ...

  9. Careercup - Facebook面试题 - 5435439490007040

    2014-05-02 07:37 题目链接 原题: // merge sorted arrays 'a' and 'b', each with 'length' elements, // in-pla ...

随机推荐

  1. java之旅——JDK版本下载

    作为一名IT工作者,技术学无止境,最近开始学习java. 学习java就需要安装jdk,直接到官网上下载,总是找不到很好的版本,在资源中找到一个下载jdk的链接,想下载哪个版本都有. http://w ...

  2. Part 30 to 31 Talking about Interfaces in c#

    Part 30 Interfaces in c# We create interfaces using interface keyword. just like classes interfaces ...

  3. STP详解

    STP详解 Write From YangWj Wednesday, March 5, 2014 一. 生成树协议 STP的主要任务是阻止在第2层网络(网桥或交换机)产生网络环路,它警惕的监视着网络中 ...

  4. 64位系统下注册32位dll文件

    64位系统下注册32位dll文件 在64位系统里注册32位软件所需的一些dll会提示不兼容,大概因为32 位进程不能加载64位Dll,64位进程也不可以加载32的导致. 若要支持的32 位和64 位C ...

  5. Codevs 1205 单词翻转

    时间限制: 1 s  空间限制: 128000 KB  题目等级 : 青铜 Bronze 题解       题目描述 Description 给出一个英语句子,希望你把句子里的单词顺序都翻转过来 输入 ...

  6. JS事件冒泡与捕获

    1事件传播——冒泡与捕获 默认情况下,事件使用冒泡事件流,不使用捕获事件流.然而,在Firefox和Safari里,你可以显式的指定使用捕获事件流,方法是在注册事件时传入useCapture参数,将这 ...

  7. BMP文件格式分析

    前两天要做一个读取bmp文件的小程序,顺便查找了一些关于BMP格式的文章,现在post上来. 简介 BMP(Bitmap-File)图形文件是Windows采用的图形文件格式,在Windows环境下运 ...

  8. UBUNTU12.4 安装磊科无线网卡驱动

    UBUNTU12.4 安装磊科无线网卡驱动 在淘宝低价买了一个网卡,回来发现不能用 ,擦 无语了. 无赖只能在网上各种找驱动,编译 安装 .今天在终于安装好了WIFI驱动了: 下载地址:https:/ ...

  9. c++11:function的用法

    function是函数.函数对象.函数指针.和成员函数的包装器,可以容纳任何类型的函数对象,函数指针,引用函数,成员函数的指针 普通函数 #include <functional> voi ...

  10. header页头内容整理

    meta标签 <meta charset="UTF-8"/> <!--视窗宽度--> <meta name="viewport" ...