130. Surrounded Regions -- 被某字符包围的区域
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 {
struct position
{
int x, y;
position(int a, int b): x(a), y(b) {}
};
public:
void solve(vector<vector<char>>& board) {
int row = board.size();
if(row <= )
return;
int col = board[].size();
if(col <= )
return;
queue<position> q;
int i, j;
for(i = ; i < col; i++)
{
if('O' == board[][i])
q.push(position(, i));
if('O' == board[row-][i])
q.push(position(row-, i));
}
for(i = ; i < row; i++)
{
if('O' == board[i][])
q.push(position(i, ));
if('O' == board[i][col-])
q.push(position(i, col-));
}
while(!q.empty())
{
position p = q.front();
q.pop();
board[p.x][p.y] = 'N';
if(p.x > && 'O' == board[(p.x)-][p.y])
q.push(position((p.x)-, p.y));
if(p.x < row- && 'O' == board[(p.x)+][p.y])
q.push(position((p.x)+, p.y));
if(p.y > && 'O' == board[p.x][(p.y)-])
q.push(position(p.x, (p.y)-));
if(p.y < col- && 'O' == board[p.x][(p.y)+])
q.push(position(p.x, (p.y)+));
}
for(i = ; i < row; i++)
{
for(j = ; j < col; j++)
{
if('O' == board[i][j])
board[i][j] = 'X';
if('N' == board[i][j])
board[i][j] = 'O';
}
cout<<endl;
}
}
};
先找到四条边缘上面的字符,再找和这些字符相邻的字符。
130. Surrounded Regions -- 被某字符包围的区域的更多相关文章
- 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 用了第一种方式, ...
- 130. Surrounded Regions(M)
130.Add to List 130. Surrounded Regions Given a 2D board containing 'X' and 'O' (the letter O), capt ...
- [LeetCode] 130. Surrounded Regions 包围区域
Given a 2D board containing 'X' and 'O'(the letter O), capture all regions surrounded by 'X'. A regi ...
- 【LeetCode】130. Surrounded Regions (2 solutions)
Surrounded Regions Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A ...
- 【一天一道LeetCode】#130. Surrounded Regions
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 130. Surrounded Regions(周围区域问题 广度优先)(代码未完成!!)
Given a 2D board containing 'X' and 'O' (the letter O), capture all regions surrounded by 'X'. A reg ...
- Java for LeetCode 130 Surrounded Regions
Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured ...
- Leetcode 130. Surrounded Regions
Given a 2D board containing 'X' and 'O' (the letter O), capture all regions surrounded by 'X'. A reg ...
- 130. Surrounded Regions
题目: Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is capt ...
随机推荐
- Margin and Padding in Windows Forms Controls
https://msdn.microsoft.com/en-us/library/ms229627.aspx Margin and Padding Precise placement of contr ...
- 如何选择正确的DevOps工具
坦白的讲:世界上没有哪种工具能够像DevOps这么神奇(或敏捷,或精益).DevOps在开发和运营团队之间建立了完美的合作与沟通,因此与其说这是一种神奇的工具,不如说是一种文化的转变. 然而,团队之间 ...
- PS(photoshop)里A4纸张的像素是多大?
A4纸的尺寸是210mm×297mm, 当你设定的分辨率是72像素/英寸时,A4纸的尺寸的图像的像素是595×842, 当你设定的分辨率是150像素/英寸时,A4纸的尺寸的图像的像素是1240×175 ...
- ABAP WRITE、WRITE TO、FORMAT语句
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
- [SAP ABAP开发技术总结]Function远程、同步、异步调用
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
- Linux Shell脚本攻略 读书笔记
Linux Shell脚本攻略 读书笔记 这是一本小书,总共253页,但内容却很丰富,书中的示例小巧而实用,对我这样总是在shell门前徘徊的人来说真是如获至宝:最有价值的当属文本处理,对这块我单独整 ...
- hdu 1568 Fibonacci 快速幂
Fibonacci Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Proble ...
- bzoj 1054: [HAOI2008]移动玩具 bfs
1054: [HAOI2008]移动玩具 Time Limit: 10 Sec Memory Limit: 162 MB[Submit][Status][Discuss] Description 在 ...
- 讓 SourceTree 讀取自定的 SSH key
我目前都在 Mac 底下開發,用 Git 來管理我的程式碼,比較一番之後決定用 SourceTree 來做為 Git client.SourceTree 是一款 Mac 底下的版本控制系統 clien ...
- eclipse 技巧
1. eclipse中xml中提示需有xsd文档 如在线eclipse将自动网络获取.xsd 否则 手动本地添加(在xml catalog参数设置选项) 2.当明确实现功能时,可将已有方法抽取成接口, ...