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,

X X X X
X O O X
X X O X
X O X X

After running your function, the board should be:

X X X X
X X X X
X X X X
X O X X
解题思路:

对最外面一圈进行BFS,替换掉最外圈的连通范围,剩下的‘O’都是被包围的,就可以直接kill掉,JAVA实现如下:

	static public void solve(char[][] board) {
if (board.length <= 2 || board[0].length <= 2)
return;
for (int row = 0; row < board.length; row++) {
if (board[row][0] == 'O') {
board[row][0] = 'T';
bfs(board, row * board[0].length);
}
if (board[row][board[0].length - 1] == 'O') {
board[row][board[0].length - 1] = 'T';
bfs(board, row * board[0].length + board[0].length - 1);
}
}
for (int col = 1; col < board[0].length - 1; col++) {
if (board[0][col] == 'O') {
board[0][col] = 'T';
bfs(board, col);
}
if (board[board.length - 1][col] == 'O') {
board[board.length - 1][col] = 'T';
bfs(board, (board.length - 1) * board[0].length + col);
}
}
for (int row = 0; row < board.length; row++)
for (int col = 0; col < board[0].length; col++) {
if (board[row][col] == 'T')
board[row][col] = 'O';
else if (board[row][col] == 'O')
board[row][col] = 'X';
} } static public void bfs(char[][] board, int num) {
Queue<Integer> queue = new LinkedList<Integer>();
queue.add(num);
while (!queue.isEmpty()) {
num=queue.poll();
int row = num / board[0].length;
int col = num - row * board[0].length;
if (row - 1 >= 0 && board[row - 1][col] == 'O') {
board[row - 1][col] = 'T';
queue.add(num - board[0].length);
}
if (row + 1 <= board.length - 1 && board[row + 1][col] == 'O') {
board[row + 1][col] = 'T';
queue.add(num + board[0].length);
}
if (col - 1 >= 0 && board[row][col - 1] == 'O') {
board[row][col - 1] = 'T';
queue.add(num - 1);
}
if (col + 1 <= board[0].length - 1 && board[row][col + 1] == 'O') {
board[row][col + 1] = 'T';
queue.add(num + 1);
}
}
}

Java for LeetCode 130 Surrounded Regions的更多相关文章

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

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

  2. Leetcode 130. Surrounded Regions

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

  3. leetcode 130 Surrounded Regions(BFS)

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

  4. Leetcode 130 Surrounded Regions DFS

    将内部的O点变成X input X X X XX O O X X X O XX O X X output X X X XX X X XX X X XX O X X DFS的基本框架是 void dfs ...

  5. 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 用了第一种方式, ...

  6. 130. Surrounded Regions(M)

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

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

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

  8. [LeetCode] 130. Surrounded Regions_Medium tag: DFS/BFS

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

  9. 【leetcode】Surrounded Regions

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

随机推荐

  1. Caught exception while loading file struts-default.xml 的错误

    转自刘长炯的博客:http://www.blogjava.net/beansoft/archive/2008/10/13/233962.html MyEclipse 6开发JDK6和Struts 2冲 ...

  2. 深入解析hostname

    结论:/etc/sysconfig/network 确实是hostname的配置文件,hostname的值跟该配置文件中的HOSTNAME有一定的关联关系,但是没有必然关系,hostname的值来自内 ...

  3. HPU 3639--Hawk-and-Chicken【SCC缩点反向建图 &amp;&amp; 求传递的最大值】

    Hawk-and-Chicken Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  4. implement-stack-using-queues(easy,但也有思考价值)

    https://leetcode.com/problems/implement-stack-using-queues/ 还有种方法,就是利用同一个队列,知道队列长度前提下,把内容从头到尾,再向尾部依次 ...

  5. spring Multiple MongoTemplate

    <!-- 数据环境配置 --> <mongo:repositories base-package="com.my9yu.manager.module.server.repo ...

  6. MySQL主从同步异常问题解决Client requested master to start replication from position > file size

    MySQL主从同步异常问题解决Client requested master to start replication from position > file size 一.问题描述 MySQ ...

  7. API测试工具postman使用总结

    一.Postman介绍: Postman是google开发的一款功能强大的网页调试与发送网页HTTP请求,并能运行测试用例的的Chrome插件,主要用于模拟网络请求包,快速创建请求,回放.管理请求,验 ...

  8. AWK 思维导图

      完整的AWK思维导图 文章来源:刘俊涛的博客 地址:http://www.cnblogs.com/lovebing

  9. [LeetCode] Decode Ways 解码方法个数、动态规划

    A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...

  10. 数据结构基础-Hash Table详解(转)

    理解Hash 哈希表(hash table)是从一个集合A到另一个集合B的映射(mapping). 映射是一种对应关系,而且集合A的某个元素只能对应集合B中的一个元素.但反过来,集合B中的一个元素可能 ...