Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'.

A region is captured by flipping all 'O's into 'X's in that surrounded region.

For example,

  1. X X X X
  2. X O O X
  3. X X O X
  4. X O X X

After running your function, the board should be:

  1. X X X X
  2. X X X X
  3. X X X X
  4. X O X X
  1. class Solution {
  2. struct position
  3. {
  4. int x, y;
  5. position(int a, int b): x(a), y(b) {}
  6. };
  7. public:
  8. void solve(vector<vector<char>>& board) {
  9. int row = board.size();
  10. if(row <= )
  11. return;
  12. int col = board[].size();
  13. if(col <= )
  14. return;
  15. queue<position> q;
  16. int i, j;
  17. for(i = ; i < col; i++)
  18. {
  19. if('O' == board[][i])
  20. q.push(position(, i));
  21. if('O' == board[row-][i])
  22. q.push(position(row-, i));
  23. }
  24. for(i = ; i < row; i++)
  25. {
  26. if('O' == board[i][])
  27. q.push(position(i, ));
  28. if('O' == board[i][col-])
  29. q.push(position(i, col-));
  30. }
  31. while(!q.empty())
  32. {
  33. position p = q.front();
  34. q.pop();
  35. board[p.x][p.y] = 'N';
  36. if(p.x > && 'O' == board[(p.x)-][p.y])
  37. q.push(position((p.x)-, p.y));
  38. if(p.x < row- && 'O' == board[(p.x)+][p.y])
  39. q.push(position((p.x)+, p.y));
  40. if(p.y > && 'O' == board[p.x][(p.y)-])
  41. q.push(position(p.x, (p.y)-));
  42. if(p.y < col- && 'O' == board[p.x][(p.y)+])
  43. q.push(position(p.x, (p.y)+));
  44. }
  45. for(i = ; i < row; i++)
  46. {
  47. for(j = ; j < col; j++)
  48. {
  49. if('O' == board[i][j])
  50. board[i][j] = 'X';
  51. if('N' == board[i][j])
  52. board[i][j] = 'O';
  53. }
  54. cout<<endl;
  55. }
  56. }
  57. };

先找到四条边缘上面的字符,再找和这些字符相邻的字符。

130. Surrounded Regions -- 被某字符包围的区域的更多相关文章

  1. leetcode 200. Number of Islands 、694 Number of Distinct Islands 、695. Max Area of Island 、130. Surrounded Regions

    两种方式处理已经访问过的节点:一种是用visited存储已经访问过的1:另一种是通过改变原始数值的值,比如将1改成-1,这样小于等于0的都会停止. Number of Islands 用了第一种方式, ...

  2. 130. Surrounded Regions(M)

    130.Add to List 130. Surrounded Regions Given a 2D board containing 'X' and 'O' (the letter O), capt ...

  3. [LeetCode] 130. Surrounded Regions 包围区域

    Given a 2D board containing 'X' and 'O'(the letter O), capture all regions surrounded by 'X'. A regi ...

  4. 【LeetCode】130. Surrounded Regions (2 solutions)

    Surrounded Regions Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A ...

  5. 【一天一道LeetCode】#130. Surrounded Regions

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  6. 130. Surrounded Regions(周围区域问题 广度优先)(代码未完成!!)

    Given a 2D board containing 'X' and 'O' (the letter O), capture all regions surrounded by 'X'. A reg ...

  7. Java for LeetCode 130 Surrounded Regions

    Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured ...

  8. Leetcode 130. Surrounded Regions

    Given a 2D board containing 'X' and 'O' (the letter O), capture all regions surrounded by 'X'. A reg ...

  9. 130. Surrounded Regions

    题目: Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is capt ...

随机推荐

  1. linux留下后门的技巧

    在团队内部的wiki上已经写出 http://drops.wooyun.org/tips/1951 http://www.freebuf.com/sectool/10474.html 还有一种方法是写 ...

  2. kaili 2.0 metasploit连接postgres数据库

    第一步:使用命令 db_init 初始化数据库

  3. C语言--指针问题_1

    #include <stdio.h> #include <string.h> main() { int *a,*b,*c; a=b=c=(int *)malloc(sizeof ...

  4. poj 1265 Area (Pick定理+求面积)

    链接:http://poj.org/problem?id=1265 Area Time Limit: 1000MS   Memory Limit: 10000K Total Submissions:  ...

  5. hdu 1217 (Floyd变形)

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=1217 Arbitrage Time Limit: 2000/1000 MS (Java/Others)   ...

  6. 3.mybatis注解

    在上篇2.mybatis入门实例(一) 连接数据库进行查询的基础上 1.添加Mapper接口:UserMapper接口,并使用mybatis的注解 import java.util.List; imp ...

  7. VS生成事件

    源自:http://www.cnblogs.com/FreeDong/p/3406737.html 如果说磨刀不误砍柴工,同样用好Visual Studio,会大大增加咱.NET程序猿效率.本文说的就 ...

  8. 作用域内优先级及this指针

    函数声明>变量声明 作用域:顶部----------------------> 函数声明会覆盖变量声明,但不会覆盖变量赋值 this指针不是变量.当调用括号的左边不是引用类型而是其它类型, ...

  9. iOS - Block 代码块

    1.Block Block 是一段预先准备好的代码,可以在需要的时候执行,可以当作参数传递.Block 可以作为函数参数或者函数的返回值,而其本身又可以带输入参数或返回值.Block 是 C 语言的, ...

  10. Python学习(8)字符串

    目录 Python 字符串 Python 访问字符串中的值 Python 字符串更新 Python 转义字符 Python 字符串运算符 Python 字符串格式化 Python 三引号 Unicod ...