2014-05-06 14:04

题目链接

原题:

given an 2D matrix M, is filled either using X or O, you need to find the region which is filled by O and surrounded by X
and fill it with X. example : X X X X X
X X O O X
X X O O X
O X X X X Answer : X X X X X
X X X X X
X X X X X
O X X X X example : X X X X X
X X O O X
X X O O O
O X X X X answer :
X X X X X
X X O O X
X X O O O
O X X X X

题目:参见Leetcode题目Surrounded Regions

解法:题解位于LeetCode - Surrounded Regions

代码:

 // http://www.careercup.com/question?id=5727310284062720
#include <iostream>
#include <queue>
#include <string>
#include <vector>
using namespace std; class Solution {
public:
void solve(vector<vector<char> > &board) {
// Should n or m be smaller than 3, there'll be no captured region.
n = (int)board.size();
if (n < ) {
return;
} m = (int)board[].size();
if (m < ) {
return;
} int i, j; // if an 'O' is on the border, all of its connected 'O's are not captured.
// so we scan the border and mark those 'O's as free. // the top row
for (j = ; j < m; ++j) {
if (board[][j] == 'O') {
check_region(board, , j);
}
} // the bottom row
for (j = ; j < m; ++j) {
if (board[n - ][j] == 'O') {
check_region(board, n - , j);
}
} // the left column
for (i = ; i < n - ; ++i) {
if (board[i][] == 'O') {
check_region(board, i, );
}
} // the right column
for (i = ; i < n - ; ++i) {
if (board[i][m - ] == 'O') {
check_region(board, i, m - );
}
} // other unchecked 'O's are all captured
for (i = ; i < n; ++i) {
for (j = ; j < m; ++j) {
if (board[i][j] == '#') {
// free 'O's
board[i][j] = 'O';
} else if (board[i][j] == 'O') {
// captured 'O's
board[i][j] = 'X';
}
}
}
}
private:
int n, m; void check_region(vector<vector<char> > &board, int startx, int starty) {
if (startx < || startx > n - || starty < || starty > m - ) {
return;
}
if (board[startx][starty] == 'O') {
board[startx][starty] = '#';
check_region(board, startx - , starty);
check_region(board, startx + , starty);
check_region(board, startx, starty - );
check_region(board, startx, starty + );
}
}
}; int main()
{
int n, m;
int i, j;
string str;
vector<vector<char> > board;
Solution sol; while (cin >> n >> m && (n > && m > )) {
board.resize(n);
for (i = ; i < n; ++i) {
cin >> str;
board[i].resize(m);
for (j = ; j < m; ++j) {
board[i][j] = str[j];
}
}
sol.solve(board);
for (i = ; i < n; ++i) {
for (j = ; j < m; ++j) {
cout << board[i][j];
}
cout << endl;
} for (i = ; i < n; ++i) {
board[i].clear();
}
board.clear();
} return ;
}

Careercup - Google面试题 - 5727310284062720的更多相关文章

  1. Careercup - Google面试题 - 5732809947742208

    2014-05-03 22:10 题目链接 原题: Given a dictionary, and a list of letters ( or consider as a string), find ...

  2. Careercup - Google面试题 - 5085331422445568

    2014-05-08 23:45 题目链接 原题: How would you use Dijkstra's algorithm to solve travel salesman problem, w ...

  3. Careercup - Google面试题 - 4847954317803520

    2014-05-08 21:33 题目链接 原题: largest number that an int variable can fit given a memory of certain size ...

  4. Careercup - Google面试题 - 6332750214725632

    2014-05-06 10:18 题目链接 原题: Given a ,) (,) (,), (,) should be returned. Some suggest to use Interval T ...

  5. Careercup - Google面试题 - 5634470967246848

    2014-05-06 07:11 题目链接 原题: Find a shortest path ,) to (N,N), assume is destination, use memorization ...

  6. Careercup - Google面试题 - 5680330589601792

    2014-05-08 23:18 题目链接 原题: If you have data coming in rapid succession what is the best way of dealin ...

  7. Careercup - Google面试题 - 5424071030341632

    2014-05-08 22:55 题目链接 原题: Given a list of strings. Produce a list of the longest common suffixes. If ...

  8. Careercup - Google面试题 - 5377673471721472

    2014-05-08 22:42 题目链接 原题: How would you split a search query across multiple machines? 题目:如何把一个搜索que ...

  9. Careercup - Google面试题 - 6331648220069888

    2014-05-08 22:27 题目链接 原题: What's the tracking algorithm of nearest location to some friends that are ...

随机推荐

  1. 单例模式(Winform窗体的实现)

    在我的设计模式分类当中,我选择单例模式作为我第一个要写的设计模式,其一,单例模式简单.容易理解让人接受,其二,单例模式很常用,在实际的Winform窗体应用开发中能够带来更好的客户体验. 单例模式的核 ...

  2. 彻底解决_OBJC_CLASS_$_某文件名", referenced from:问题转

    最近在使用静态库时,总是出现这个问题.下面总结一下我得解决方法: 1. .m文件没有导入   在Build Phases里的Compile Sources 中添加报错的文件 2. .framework ...

  3. 自动计算尺寸列表功能案例ios源码

    源码HTKDynamicResizingCell,HTKDynamicResizingCell提供自动计算尺寸的TableViewCell/CollectionViewCel,只要设置了合适AutoL ...

  4. 利用Python抓取CSDN博客

    这两天发现了一篇好文章,陈皓写的makefile的教程,具体地址在这里<跟我一起写makefile> 这篇文章一共分成了14个部分,我看东西又习惯在kindle上面看,感觉一篇一篇地复制成 ...

  5. silverlight 退出当前页面、跳转到主页面

    1.退出当前页面 private void imgExit_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) { if (Message ...

  6. SDUST 软件工程2016-作业4-A 百钱买鸡问题

    解决百钱买鸡问题原本并不困难,关键的是这道题对其进行了升级,测试数据太大,传统的解法,像三重循环,二重循环都会导致超时. 这道题正确的解法应该是结合数学方程进行化简,将其转化为1层循环: x+y+z= ...

  7. 输出内容(document.write)四种写法

    第一种:输出内容用""括起,直接输出""号内的内容 <script type="text/javascript"> docume ...

  8. MongoDB中通过MapReduce实现合计Sum功能及返回格式不一致问题分析

    建立下述测试数据,通过MapReduce统计每个班级学生数及成绩和. 代码如下: public string SumStudentScore() { var collection = _dataBas ...

  9. 关于 mvc 中 连字符 - 和下划线 _转换的问题。

     [潜水]大崔||哈尔滨(759666247) 10:02:16  如图   C#不承认 “-”[知府]古道今-湖北\xig<systemobject@126.com> 10:03:54  ...

  10. STM32F0xx_TIM输入捕获(计算频率)配置详细过程

    前言 关于STM32的定时器,可谓是功能强大,估计没有多少人研究完STM32定时器的所有功能(包括我也没有),只是使用常用的一些功能,后续我会推出关于STM32定时器的更多功能. STM32芯片多数为 ...