Careercup - Google面试题 - 5727310284062720
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的更多相关文章
- Careercup - Google面试题 - 5732809947742208
2014-05-03 22:10 题目链接 原题: Given a dictionary, and a list of letters ( or consider as a string), find ...
- Careercup - Google面试题 - 5085331422445568
2014-05-08 23:45 题目链接 原题: How would you use Dijkstra's algorithm to solve travel salesman problem, w ...
- Careercup - Google面试题 - 4847954317803520
2014-05-08 21:33 题目链接 原题: largest number that an int variable can fit given a memory of certain size ...
- Careercup - Google面试题 - 6332750214725632
2014-05-06 10:18 题目链接 原题: Given a ,) (,) (,), (,) should be returned. Some suggest to use Interval T ...
- Careercup - Google面试题 - 5634470967246848
2014-05-06 07:11 题目链接 原题: Find a shortest path ,) to (N,N), assume is destination, use memorization ...
- Careercup - Google面试题 - 5680330589601792
2014-05-08 23:18 题目链接 原题: If you have data coming in rapid succession what is the best way of dealin ...
- Careercup - Google面试题 - 5424071030341632
2014-05-08 22:55 题目链接 原题: Given a list of strings. Produce a list of the longest common suffixes. If ...
- Careercup - Google面试题 - 5377673471721472
2014-05-08 22:42 题目链接 原题: How would you split a search query across multiple machines? 题目:如何把一个搜索que ...
- Careercup - Google面试题 - 6331648220069888
2014-05-08 22:27 题目链接 原题: What's the tracking algorithm of nearest location to some friends that are ...
随机推荐
- 移植u-boot-1.1.6之NOR的支持
u-boot-1.1.6里面默认配置文件里面支持的nor型号是 #if 0 #define CONFIG_AMD_LV400 1 /* uncomment this if you have a LV4 ...
- protobuf的使用
Protobuf的安装 正确安装方法: [http://blog.csdn.net/guoyilongedu/article/details/17093811] linux下安装protobuf教程+ ...
- ASP.Net MVC 5 高级编程 第7章 成员资格、授权和安全性
第7章 成员资格.授权和安全性 7.1 安全性 ASP.NET MVC 提供了许多内置的保护机制(默认利用 HTML 辅助方法和Razor 语法进行 HTML编码以及请求验证等功能特性,以及通过基架构 ...
- JQuery 的几个有用的技巧
JQuery代码 /* 新窗口打开链接:JQuery filter attr * 禁止鼠标弹出右键菜单:DOM contextmenu * 回到页面顶端:DOM scrollTo * 动态更换Css样 ...
- Sublime Text 使用 Emmet 补全错误问题
Sublime Text安装了Emmet后,使用Tab或者ctrl+e发现补全有问题,如: div.testClass#testId 变成了 div.<testClass id="te ...
- 【转】详解JavaScript中的this
ref:http://blog.jobbole.com/39305/ 来源:foocoder 详解JavaScript中的this JavaScript中的this总是让人迷惑,应该是js众所周知的坑 ...
- Laravel 5 基础(十一)- 子视图和表单复用
我们需要处理编辑文章的问题.当然我们可以手工添加新的路由,就像这样: Route::get('/articles/{id}/edit', 'ArticleController@edit'); 让我们在 ...
- Delphi CxGrid 汇总(3)
列 解决: <aColumn>.GroupIndex := -1; <aColumn>.Visible := True; *** ...
- Sublime Text 2编译python时出错
[Error 2] The system cannot find the file specified [Finished] 解决方法: 1.环境变量path添加: C:\Python32\Too ...
- XSS的原理分析与解剖[转http://www.freebuf.com/articles/web/40520.html]
0×01 前言: <xss攻击手法>一开始在互联网上资料并不多(都是现成的代码,没有从基础的开始),直到刺的<白帽子讲WEB安全>和cn4rry的<XSS跨站脚本攻击剖析 ...