国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

leecode 解題總結(jié):130. Surrounded Regions

2019-11-08 03:28:10
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友
#include <iostream>#include <stdio.h>#include <vector>#include <string>#include <queue>using namespace std;/*問(wèn)題:Given a 2D board containing 'X' and 'O' (the letter 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 XAfter running your function, the board should be:X X X XX X X XX X X XX O X X輸入:4(行) 4(列)X X X XX O O XX X O XX O X X輸出:X X X XX X X XX X X XX O X X關(guān)鍵1 分析:此題實(shí)際上找到四周都是"X"的"O",并把這些"O"替換為"X"。參考這位作者的解法:http://www.cnblogs.com/ganganloveu/p/3755191.html關(guān)鍵就是從何處開始替換。觀察發(fā)現(xiàn)在邊界上的"O"都不會(huì)被替換,先將這部分"O"轉(zhuǎn)換為"#",對(duì)于最后不是"#"的"O"就全部設(shè)置為"X",然后再把"#"設(shè)置為"O"即可后續(xù)的遍歷中會(huì)對(duì)邊界的鄰居結(jié)點(diǎn)上的"O"繼續(xù)遞歸操作*/struct MyPoint{	MyPoint(int row , int col):_row(row),_col(col){}	MyPoint(){}	int _row;	int _col;};class Solution {public:	//對(duì)位置(curRow,curCol)即處于邊界的元素上的"O"嘗試都設(shè)置為"#"	void bfs(int curRow , int curCol , int row , int col,vector<vector<char>>& board)	{		if(curRow < 0 || curRow >= row || curCol < 0 || curCol >= col)		{			return;		}		//易錯(cuò),當(dāng)前元素要設(shè)置為"#"		board.at(curRow).at(curCol) = '#';		queue<MyPoint> points;		points.push(MyPoint(curRow , curCol));		MyPoint point;		while(!points.empty())		{			point = points.front();			points.pop();			//向下尋找"O"的元素			if( (point._row + 1) < row && 'O' == board.at( point._row + 1 ).at(point._col))			{				board.at( point._row + 1 ).at(point._col) = '#';				points.push(MyPoint(point._row + 1, point._col));			}						//向上			if( (point._row - 1) >= 0 && 'O' == board.at( point._row - 1 ).at(point._col) )			{				board.at(point._row - 1).at(point._col) = '#';				points.push(MyPoint(point._row - 1 , point._col));			}			//向右			if( (point._col + 1) < col && 'O' == board.at( point._row ).at(point._col + 1) )			{				board.at( point._row ).at(point._col + 1) = '#';				points.push(MyPoint( point._row , point._col + 1));			}			//向左			if( (point._col - 1) >= 0 && 'O' == board.at( point._row ).at(point._col - 1) )			{				board.at( point._row ).at(point._col - 1) = '#';				points.push(MyPoint( point._row , point._col - 1));			}		}	}    void solve(vector<vector<char>>& board) {        if(board.empty())		{			return;		}		int row = board.size();		int col = board.at(0).size();		//尋找四個(gè)邊界上的"O",然后遞歸修改為"#"		for(int i = 0 ; i < row ; i++)		{			for(int j = 0 ; j < col ; j++)			{				if(0 == i || row - 1 == i || 0 == j || col - 1 == j)				{					if('O' == board.at(i).at(j))					{						bfs(i , j , row , col , board);					}				}			}		}		//將"#"字符替換回原來(lái)的"O"		for(int i = 0 ; i < row ; i++)		{			for(int j = 0 ; j < col ; j++)			{				if('#' == board.at(i).at(j))				{					board.at(i).at(j) = 'O';				}				//凡是不是"#"的"O",都是被圍住的,全部修改為"X"				else if('O' == board.at(i).at(j))				{					board.at(i).at(j) = 'X';				}			}		}    }};void PRint(vector< vector<char> > result){	if(result.empty())	{		cout << "no result" << endl;		return;	}	int size = result.size();	int len = result.at(0).size();	for(int i = 0 ; i < size ; i++)	{		for(int j = 0 ; j < len; j++)		{			cout << result.at(i).at(j) << " " ;		}		cout << endl;	}}void process(){	 vector< vector<char> > board;	 char value;	 Solution solution;	 int row;	 int col;	 while(cin >> row >> col )	 {		 board.clear();		 for(int i = 0 ; i < row ; i++)		 {			 vector<char> nums;			 for(int j = 0 ; j < col ; j++)			 {				 cin >> value;				 nums.push_back(value);			 }			 board.push_back(nums);		 }		 solution.solve(board);		 print(board);	 }}int main(int argc , char* argv[]){	process();	getchar();	return 0;}
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 朝阳区| 湛江市| 济宁市| 南和县| 赤水市| 高邑县| 昭平县| 连江县| 龙山县| 拉孜县| 陇南市| 句容市| 禹城市| 砀山县| 建始县| 济南市| 清远市| 晋宁县| 江西省| 图们市| 安塞县| 沂水县| 城固县| 临漳县| 德安县| 眉山市| 昔阳县| 奉贤区| 淮北市| 怀化市| 札达县| 酒泉市| 玛曲县| 黎川县| 哈尔滨市| 方正县| 京山县| 唐山市| 丰镇市| 绥化市| 永德县|