[leetcode] Surrounded Regions
2015-07-01 11:12
330 查看
From : https://leetcode.com/problems/surrounded-regions/
Given a 2D board containing
capture all regions surrounded by
A region is captured by flipping all
in that surrounded region.
For example,
After running your function, the board should be:
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
class Solution { public: void solve(vector<vector<char> >& board) { if(!board.size() || !board[0].size()) return; m=board.size(), n=board[0].size(); for(int i=0; i<m; i++) { if(board[i][0]=='O') search(board, i ,0); if(board[i][n-1]=='O') search(board, i ,n-1); } for(int i=0; i<n; i++) { if(board[0][i]=='O') search(board, 0, i); if(board[m-1][i]=='O') search(board, m-1, i); } for(int i=0; i<m; i++) { for(int j=0; j<n; j++) { if(board[i][j]=='O') board[i][j]='X'; else if(board[i][j]=='P') board[i][j]='O'; } } } void search(vector<vector<char> >& board, int i, int j) { queue<point> q; q.push(point(i, j)); board[i][j] = 'P'; while(!q.empty()) { point p = q.front(); q.pop(); i = p.x; j = p.y; if(i-1>=0 && board[i-1][j]=='O') { q.push(point(i-1, j)); board[i-1][j] = 'P'; } if(i+1< m && board[i+1][j]=='O') { q.push(point(i+1, j)); board[i+1][j] = 'P'; } if(j-1>=0 && board[i][j-1]=='O') { q.push(point(i, j-1)); board[i][j-1] = 'P'; } if(j+1< n && board[i][j+1]=='O') { q.push(point(i, j+1)); board[i][j+1] = 'P'; } } } private: struct point{ int x; int y; point(int i, int j):x(i),y(j) {} }; int n, m; };这道题应该注意,在入队的时候,将board更新,而不是出队的时候更新。
public class Solution { private static class Point { int X; int Y; public Point(int x, int y) { X = x; Y = y; } } private int m, n; private int[] X = { 1, 0, -1, 0 }; private int[] Y = { 0, 1, 0, -1 }; public void solve(char[][] board) { if (board == null || board.length == 0) { return; } m = board.length; n = board[0].length; for(int i=0; i<m; ++i) { if(board[i][0] == 'O') { set(board, i, 0); } if(board[i][n-1] =='O') { set(board, i, n-1); } } for(int j=0; j<n;++j) { if(board[0][j] == 'O') { set(board, 0, j); } if(board[m-1][j] == 'O') { set(board, m-1, j); } } for(int i=0; i<m; ++i) { for(int j=0; j<n; ++j) { if(board[i][j] == '-') { board[i][j] = 'O'; } else if(board[i][j] == 'O') { board[i][j] = 'X'; } } } } private void set(char[][] board, int i, int j) { Queue<Point> q = new LinkedList<Point>(); board[i][j] = '-'; q.add(new Point(i, j)); while (!q.isEmpty()) { Point p = q.poll(); int a = p.X; int b = p.Y; for (int k = 0; k < 4; ++k) { int x = a + X[k]; int y = b + Y[k]; if (valid(x, y) && board[x][y] == 'O') { board[x][y] = '-'; q.add(new Point(x, y)); } } } } private boolean valid(int x, int y) { return x >= 0 && x < m && y >= 0 && y < n; } }
相关文章推荐
- Android主题应用
- 文章标题
- sublime text3空格和tab的显示
- 电影 杭州景点
- 动态添加删除css/js文件
- JDBC
- uleb128、sleb128和uleb128p1编码格式介绍
- iOS IAP 自动更新的订阅服务
- IndexDB 使用问题
- Spring MVC拦截器+注解方式实现防止表单重复提交
- jQuery 中获取调用Tab的当前页面
- 上课知识
- Tomcat内存设置详解
- 《Java课程实习》日志(周二)
- POJ3229
- centos6.4安装kvm
- python实现的希尔排序算法实例
- 文本文件的读取方式一
- CentOS7安装Hadoop2.7完整流程
- mysql怎么让一个存储过程定时执行