2014-05-06 14:04

题目链接

原题:

  1. 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
  2. and fill it with X.
  3.  
  4. example :
  5.  
  6. X X X X X
  7. X X O O X
  8. X X O O X
  9. O X X X X
  10.  
  11. Answer :
  12.  
  13. X X X X X
  14. X X X X X
  15. X X X X X
  16. O X X X X
  17.  
  18. example :
  19.  
  20. X X X X X
  21. X X O O X
  22. X X O O O
  23. O X X X X
  24.  
  25. answer :
  26. X X X X X
  27. X X O O X
  28. X X O O O
  29. O X X X X

题目:参见Leetcode题目Surrounded Regions

解法:题解位于LeetCode - Surrounded Regions

代码:

  1. // http://www.careercup.com/question?id=5727310284062720
  2. #include <iostream>
  3. #include <queue>
  4. #include <string>
  5. #include <vector>
  6. using namespace std;
  7.  
  8. class Solution {
  9. public:
  10. void solve(vector<vector<char> > &board) {
  11. // Should n or m be smaller than 3, there'll be no captured region.
  12. n = (int)board.size();
  13. if (n < ) {
  14. return;
  15. }
  16.  
  17. m = (int)board[].size();
  18. if (m < ) {
  19. return;
  20. }
  21.  
  22. int i, j;
  23.  
  24. // if an 'O' is on the border, all of its connected 'O's are not captured.
  25. // so we scan the border and mark those 'O's as free.
  26.  
  27. // the top row
  28. for (j = ; j < m; ++j) {
  29. if (board[][j] == 'O') {
  30. check_region(board, , j);
  31. }
  32. }
  33.  
  34. // the bottom row
  35. for (j = ; j < m; ++j) {
  36. if (board[n - ][j] == 'O') {
  37. check_region(board, n - , j);
  38. }
  39. }
  40.  
  41. // the left column
  42. for (i = ; i < n - ; ++i) {
  43. if (board[i][] == 'O') {
  44. check_region(board, i, );
  45. }
  46. }
  47.  
  48. // the right column
  49. for (i = ; i < n - ; ++i) {
  50. if (board[i][m - ] == 'O') {
  51. check_region(board, i, m - );
  52. }
  53. }
  54.  
  55. // other unchecked 'O's are all captured
  56. for (i = ; i < n; ++i) {
  57. for (j = ; j < m; ++j) {
  58. if (board[i][j] == '#') {
  59. // free 'O's
  60. board[i][j] = 'O';
  61. } else if (board[i][j] == 'O') {
  62. // captured 'O's
  63. board[i][j] = 'X';
  64. }
  65. }
  66. }
  67. }
  68. private:
  69. int n, m;
  70.  
  71. void check_region(vector<vector<char> > &board, int startx, int starty) {
  72. if (startx < || startx > n - || starty < || starty > m - ) {
  73. return;
  74. }
  75. if (board[startx][starty] == 'O') {
  76. board[startx][starty] = '#';
  77. check_region(board, startx - , starty);
  78. check_region(board, startx + , starty);
  79. check_region(board, startx, starty - );
  80. check_region(board, startx, starty + );
  81. }
  82. }
  83. };
  84.  
  85. int main()
  86. {
  87. int n, m;
  88. int i, j;
  89. string str;
  90. vector<vector<char> > board;
  91. Solution sol;
  92.  
  93. while (cin >> n >> m && (n > && m > )) {
  94. board.resize(n);
  95. for (i = ; i < n; ++i) {
  96. cin >> str;
  97. board[i].resize(m);
  98. for (j = ; j < m; ++j) {
  99. board[i][j] = str[j];
  100. }
  101. }
  102. sol.solve(board);
  103. for (i = ; i < n; ++i) {
  104. for (j = ; j < m; ++j) {
  105. cout << board[i][j];
  106. }
  107. cout << endl;
  108. }
  109.  
  110. for (i = ; i < n; ++i) {
  111. board[i].clear();
  112. }
  113. board.clear();
  114. }
  115.  
  116. return ;
  117. }

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. Zend Studio GitHub 使用教程

    这是我在开发项目时遇到的一些问题总结,目前基本实现协同开发.还有个问题是怎么才能像sf那样添加管理帐号,使用多个帐号协同开发,求教,欢迎留言讨论. 一.安装eGit插件 1. 由于zend studi ...

  2. 如何实现Android 中断线程的处理

    我现在对一个用户注册的功能1.用ProgressDialog将当前页面设成不可操作(保留返回键 退出ProgressDialog)2.用一个线程clientThread执行数据的提交和返回 问题:考虑 ...

  3. sublime text 2 中文乱码解决办法

    sublime text 2是一款非常优秀的跨平台文本及源代码编辑器,本人非常喜欢,但是不支持GB2312和GBK编码在某些时候比较麻烦.可以通过向sublime text 中添加编码类型转换包(比如 ...

  4. 查看Linux系统版本信息

    一.查看Linux内核版本命令(两种方法): 1.cat /proc/version [root@S-CentOS home]# cat /proc/versionLinux version 2.6. ...

  5. Hive中典型的表内数据除重写法

    insert overwrite table store select t.p_key,t.sort_word from ( select p_key, sort_word , row_number( ...

  6. laravel5.2 学习之服务提供者

    契约接口:app\Contracts\LanguageContract.php <?php namespace App\Contracts; interface LanguageContract ...

  7. shopnc二次开发(二)

    一般来说二次开发,多数就是修改界面和增加功能这两个需求 先说修改界面 mvc 架构的程序,在界面这里,基本就是调用数据. 常见的界面数据构架有三种 1.是业务端或者是控制端数据驱动界面,基本上是后台输 ...

  8. ubuntu下svn使用指南

    ubuntu下安装subversion客户端: sudo apt-get install subversion subversion-tools 详细请看 http://www.subversion. ...

  9. 把Java对象转为xml格式

    主要使用到的Java类有:javax.xml.bind.JAXBContext,javax.xml.bind.Marshaller(编排) 代码主要展示如下: public class Student ...

  10. C# 平时碰见的问题【2】

    问题1 修改命名空间后 .ashx 类型创建失败 [情景] 在调整前后台项目结构的时候,修改了默认命名空间(XXX.Admin 修改成XXX.Web),结果调试的时候发现XXX.Admin.Ajax. ...