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. 简单linux字符设备驱动程序

    本文代码参考<LINUX设备驱动程序>第三章 字符设备驱动程序 本文中的“字符设备”是一段大小为PAGE_SIZE的内存空间 功能:向字符设备写入字符串:从字符设备读出字符串 代码: 1. ...

  2. Navicat for PostgreSQL 必须知道的十大功能

    Navicat for PostgreSQL 是一套易于使用的图形化 PostgreSQL 数据库管理工具.可使用强劲的 SQL 编辑器创建和运行查询.函数和使用多功能的数据编辑工具管理数据.Navi ...

  3. 一个用WPF做的简单计算器源代码

    一.界面设计XAML代码 <Window x:Class="fengjisuanqi.MainWindow" xmlns="http://schemas.micro ...

  4. Knockout.Js官网学习(event绑定、submit绑定)

    event绑定 event绑定在DOM元素上添加指定的事件句柄以便元素被触发的时候执行定义的JavaScript 函数.大部分情况下是用在keypress,mouseover和mouseout上. 简 ...

  5. Silverlight IIs发布问题

    1.在IIS上部署系统没有问题,在vs中链接oracle中出错(数据连接不成功,请检查该数据库是否已启动尝试加载oracle客户端时引发BadImageFormatException.如果在安装32位 ...

  6. php 解决json_encode中文问题

    众所周知使用json_encode可以方便快捷地将对象进行json编码,但是如果对象的属性中存在着中文,问题也就随之而来了.json_encode会将中文转换为unicode编码例如:'胥'经过jso ...

  7. python Django 学习笔记(二)—— 一个简单的网页

    1,创建一个django项目 使用django-admin.py startproject MyDjangoSite 参考这里 2,建立视图 from django.http import HttpR ...

  8. python-抓取图片

    今天看到博客园一个文章,python抓取图片,也没看内容,心想自己也写一个抓取脚本试试看,一方面自己也在学习python,另一方面毕竟实际工作也经常会遇到这种需要临时写脚本的时候,突击锻炼还是好的嘛. ...

  9. 任务管理界面添加显示RAM信息

    显示RAM信息的核心代码是大蛋的,我只不过是整理下教程而已! 大蛋应该不会介意的吧,首先你需要apktool和SystemUI.apk,framework-res.apk 然后开始加载框架和反编译.. ...

  10. 内核同步机制 RCU

    Evernote分享地址:http://www.evernote.com/shard/s133/sh/8807320d-f54d-4e90-a31b-e2a3d35509ee/7539dc3931b8 ...