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. ios获得文件字节总数

    NSDictionary *attributes = [[NSFileManager defaultManager] attributesOfItemAtPath:self.finalPath err ...

  2. doviceone- http组件进行webservice的POST请求

    var http = mm("do_Http"); http.method = "POST"; // GET | POST http.timeout = 100 ...

  3. Linux下搭建PHP开发环境(LAMP)

    LAMP:Linux+Apache+Mysql/MariaDB+Perl/PHP/Python 一键安装方法如下: 注:Ubuntu下可使用sudo su 命名切换到root用户. 开始安装之前,先执 ...

  4. ElasticSearch搜索term和terms的区别

    今天同事使用ES查询印地语的文章.发现查询报错,查询语句和错误信息如下: 查询语句:{    "query":{        "bool":{         ...

  5. 成功者的特点 VS 失败者的特点

  6. 【Python】删除字符串的空白

    在程序中,额外的空白可能让人迷惑,对于程序员来说,'python'跟'python '看起来几乎一样,但是对于程序来说,可是千差万别 (lstrip)删除开头空白 >>> Langu ...

  7. fiddler不能监听 localhost和 127.0.0.1的问题 .

    localhost/127.0.0.1的请求不会通过任何代理发送,fiddler也就无法截获. 解决方案 用 http://localhost. (locahost紧跟一个点号) 用 http://1 ...

  8. 记录MySQL运行的SQL

    对照Oracle功能去学习Mysql总会发现亮点 Oracle中通过日志挖掘这一技能,能够找到以前运行过的全部记录: Mysql中也提供了3种方法{验证过的,我会记录详细做法} 方法1:{已验证} 记 ...

  9. ios控件自定义指引

    转载自:http://bbs.9ria.com/thread-256747-1-1.html 一直以来都想写点什么,做点有意义的事,从今天开始我将会把自己在这一年的学习和应用IOS开发中的学习心得和体 ...

  10. layui-字体图标

    layui官网下载:GitHub:https://github.com/sentsin/layui/ layui官网首页-下载:http://www.layui.com/ layui-字体图标-官方网 ...