[LeetCode] - Surrounded Regions
来源:互联网 发布:腾达 访客网络 编辑:程序博客网 时间:2024/05/16 09:26
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 XX O O XX X O XX O X X
After running your function, the board should be:
X X X XX X X XX X X XX O X X
这个题目的思路分两步:
1. 把棋盘四周的"O"以及与其直接相连的“O”全部都变为“Q”,因为这种“O”是“X”围不住的。这里采用BFS算法。BFS我习惯的写法就是MIT算法课里面讲的,frontier加next进行while循环。要注意的是,把点放到frontier和next之前就要改变其状态,要不然会有重复的点被加进去。
2. 经过第一步之后,棋盘中还剩下的“O”就是可以被“X”围住的。所以接下来就要将“O”变为“X”,然后将“Q“变为”O“。这里要注意先后顺序,要不然第一步就白忙活了,又都变回去了。
代码如下:
public class Solution { public void solve(char[][] board) { if(board==null || board.length==0 || board[0].length==0) return; flipOToQ(board); flipOToX(board); return; } public void flipOToQ(char[][] board) { for(int i=0; i<board[0].length; i++) { if(board[0][i] == 'O') { flipOToQHelper(board, 0, i); } if(board[board.length-1][i] == 'O') { flipOToQHelper(board, board.length-1, i); } } for(int j=0; j<board.length; j++) { if(board[j][0] == 'O') { flipOToQHelper(board, j, 0); } if(board[j][board[0].length-1] == 'O') { flipOToQHelper(board, j, board[0].length-1); } } return; } // BFS, change state before adding into frontier and next public void flipOToQHelper(char[][] board, int row, int col) { Queue<Position> frontier = new LinkedList<Position>(); frontier.add(new Position(row, col)); board[row][col] = 'Q'; while(!frontier.isEmpty()) { Queue<Position> next = new LinkedList<Position>(); while(!frontier.isEmpty()) { Position temp = frontier.poll(); if(temp.col-1>=0 && board[temp.row][temp.col-1]=='O') { board[temp.row][temp.col-1] = 'Q'; next.add(new Position(temp.row, temp.col-1)); } if(temp.col+1<board[0].length && board[temp.row][temp.col+1]=='O') { board[temp.row][temp.col+1] = 'Q'; next.add(new Position(temp.row, temp.col+1)); } if(temp.row-1>=0 && board[temp.row-1][temp.col]=='O') { board[temp.row-1][temp.col] = 'Q'; next.add(new Position(temp.row-1, temp.col)); } if(temp.row+1<board.length && board[temp.row+1][temp.col]=='O') { board[temp.row+1][temp.col] = 'Q'; next.add(new Position(temp.row+1, temp.col)); } } frontier = next; } return; } public void flipOToX(char[][] board) { for(int i=0; i<board.length; i++) { for(int j=0; j<board[0].length; j++) { if(board[i][j]=='O') board[i][j]='X'; if(board[i][j]=='Q') board[i][j]='O'; } } return; }}class Position { public int row; public int col; public Position(int r, int c) { this.row = r; this.col = c; }}
0 0
- LeetCode : Surrounded Regions
- [leetcode] Surrounded Regions
- leetcode 128: Surrounded Regions
- LeetCode 39: Surrounded Regions
- 【leetcode】Surrounded Regions
- LeetCode - Surrounded Regions
- LeetCode之Surrounded Regions
- leetcode:Surrounded Regions
- [LeetCode]Surrounded Regions
- LeetCode - Surrounded Regions
- [Leetcode]Surrounded Regions
- Leetcode Surrounded Regions
- [leetcode]Surrounded Regions
- leetcode Surrounded Regions
- Leetcode: Surrounded Regions
- LeetCode-Surrounded Regions
- [leetcode] Surrounded Regions
- leetcode Surrounded Regions 详解
- 【ThinkingInC++】14、联合体使用
- redhat linux系统文本界面下安装图形界面
- City Horizon
- 设计模式之观察者模式
- Android 通过系统使用NotificationListenerService 监听各种Notification的使用方法
- [LeetCode] - Surrounded Regions
- 【ThinkingInC++】15、命令行参数
- 最常见的电话号码
- SimpleDate()详解
- 匿名委托 Invoke,Predicate<T> 委托
- 【ThinkingInC++】16、吧变量和表达式转换成字符串
- Ubuntu win8 小设备版本
- 利用.bat(批处理)来删除KEIL编译生成的无用文件
- iphone 显示蓝牙电量