LeetCode: Surrounded Regions
来源:互联网 发布:linux查看文件编码方式 编辑:程序博客网 时间:2024/05/13 09:35
思路:刚开始觉得如果每个'O'点如果四个方向上存在'X'点,则其被包围,其实如果存在一条路径,即所有'O'点相连,最后连接到边界上去了,那么这个点就没有被包围,所以,只能通过边界的'O'点来进行深度搜索,只要与边界'O'相连通的'O'点都是没有被覆盖的,剩下的就好办了。
code:
class Solution {public: bool isRightLoc(int i,int j,int rows,int cols){ if(i <rows && i >=0 && j < cols && j >=0) return true; return false; } void solve(vector<vector<char>> &board) { if(board.size() == 0)return; int rows = board.size(), cols = board[0].size(); for(int j = 0;j < cols;j++){ if(board[0][j] == 'O'){ //board[0][j] = 'T';//cout<<"sdf"<<endl; dfs(0,j,board); } if(board[rows-1][j] == 'O'){ //board[rows-1][j] = 'T'; dfs(rows-1,j,board); } } for(int i = 1;i < rows-1;i++){ if(board[i][0] == 'O'){ //board[i][0] = 'T'; dfs(i,0,board); } if(board[i][cols-1] == 'O'){ //board[i][cols-1] = 'T'; dfs(i,cols-1,board); } } for(int i = 0;i < rows;i++) for(int j = 0;j < cols;j++){ if(board[i][j] == 'O') board[i][j] = 'X'; else if(board[i][j] == 'T') board[i][j] = 'O'; } } void dfs(int i,int j,vector<vector<char>> &board){ int rows = board.size(), cols = board[0].size(); if(isRightLoc(i,j,rows,cols)){//cout<<i<<" "<<j<<" "<<board[i][j]<<endl; if(board[i][j] == 'O'){ board[i][j] = 'T'; dfs(i,j+1,board); dfs(i-1,j,board); dfs(i,j-1,board); dfs(i+1,j,board); } } }};
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 详解
- IT精英故事100之Oculus VR的创始人
- hdu 1150 最小覆盖点==最大二分匹配 Machine Schedule
- C调用C++库
- 新浪微博注意事项:
- 基于域名的7层转发的实现(NAT+反向代理)
- LeetCode: Surrounded Regions
- 杭电2028 一系列数的最小公倍数
- [综合面试] 牛人整理分享的面试知识:操作系统、计算机网络、设计模式、Linux编程,数据结构总结
- vs2008 字符串定义乱码之解决方案
- Point-in-Time (Incremental) Recovery Using the Binary Log
- HDOJ 题目1717 小数化分数2(数学)
- 二叉树
- 最简单的GIS代码分享
- opera, chrome调试wap网站