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 ...
随机推荐
- Zend Studio GitHub 使用教程
这是我在开发项目时遇到的一些问题总结,目前基本实现协同开发.还有个问题是怎么才能像sf那样添加管理帐号,使用多个帐号协同开发,求教,欢迎留言讨论. 一.安装eGit插件 1. 由于zend studi ...
- 如何实现Android 中断线程的处理
我现在对一个用户注册的功能1.用ProgressDialog将当前页面设成不可操作(保留返回键 退出ProgressDialog)2.用一个线程clientThread执行数据的提交和返回 问题:考虑 ...
- sublime text 2 中文乱码解决办法
sublime text 2是一款非常优秀的跨平台文本及源代码编辑器,本人非常喜欢,但是不支持GB2312和GBK编码在某些时候比较麻烦.可以通过向sublime text 中添加编码类型转换包(比如 ...
- 查看Linux系统版本信息
一.查看Linux内核版本命令(两种方法): 1.cat /proc/version [root@S-CentOS home]# cat /proc/versionLinux version 2.6. ...
- Hive中典型的表内数据除重写法
insert overwrite table store select t.p_key,t.sort_word from ( select p_key, sort_word , row_number( ...
- laravel5.2 学习之服务提供者
契约接口:app\Contracts\LanguageContract.php <?php namespace App\Contracts; interface LanguageContract ...
- shopnc二次开发(二)
一般来说二次开发,多数就是修改界面和增加功能这两个需求 先说修改界面 mvc 架构的程序,在界面这里,基本就是调用数据. 常见的界面数据构架有三种 1.是业务端或者是控制端数据驱动界面,基本上是后台输 ...
- ubuntu下svn使用指南
ubuntu下安装subversion客户端: sudo apt-get install subversion subversion-tools 详细请看 http://www.subversion. ...
- 把Java对象转为xml格式
主要使用到的Java类有:javax.xml.bind.JAXBContext,javax.xml.bind.Marshaller(编排) 代码主要展示如下: public class Student ...
- C# 平时碰见的问题【2】
问题1 修改命名空间后 .ashx 类型创建失败 [情景] 在调整前后台项目结构的时候,修改了默认命名空间(XXX.Admin 修改成XXX.Web),结果调试的时候发现XXX.Admin.Ajax. ...