Leetcode: 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
我的最初思路是直接找被包围的O区域,如果这个区域不与边界接触则是被包围区域,但是这种判断要等到dfs完成才能知道是否是被包围区域,所以需要再一次dfs来将这些点变成'X'。所以这样的复杂度太高,Judge Large超时。这个版本的代码包括下面的dfs和changeBoard,以及Solve中被注释掉的部门代码;后来参考了网上的一个代码,将思路转变为先对不被包围的区域做标记'C',然后再遍历一遍按要求修改。这样就避免了做两遍深度搜索。
代码如下:(有点难看,见谅……)
//
// SurroundedRegions.cpp
// SJMcode
//
// Created by Jiamei Shuai on 13-8-30.
// Copyright (c) 2013年 Jiamei Shuai. All rights reserved.
// #include <iostream>
#include <vector>
#include <assert.h>
using namespace std; class Solution {
public: void dfs2(vector<vector<char>> &board, int i, int j)
{
if(i > board.size()- || i < || j > board[].size()- || j < )
return; if(board[i][j] == 'O')
{
board[i][j] = 'C';
dfs2(board, i+, j);
dfs2(board, i-, j);
dfs2(board, i, j-);
dfs2(board, i, j+);
} } bool dfs(vector<vector<char>> &board, int i, int j, int height, int width, vector<vector<int>> &isVis, bool &flag) // working but too slow
{
//if(board[i][j] == 'X') return true; if(i == height- || i == || j == width- || j == )
flag = false; // 'o' touch the border: means this block cannot be the answer isVis[i][j] = ; if(i >= && !isVis[i-][j] && board[i-][j] == 'O')
{
dfs(board, i-, j, height, width, isVis, flag); //上
}
if(i < (int)board.size()- && !isVis[i+][j] && board[i+][j] == 'O')
{
dfs(board, i+, j, height, width, isVis, flag); //下
}
if(j >= && !isVis[i][j-] && board[i][j-] == 'O')
{
dfs(board, i, j-, height, width, isVis, flag); //左
}
if(j < (int)board[].size() && !isVis[i][j+] && board[i][j+] == 'O')
{
dfs(board, i, j+, height, width, isVis, flag); //右
} return flag;
} void changeBoard(vector<vector<char>> &board, int i, int j) // working but too slow
{
vector<int> queue;
board[i][j] = 'X';
assert(i > && i < board.size()- && j > && j < board[].size()-);
if(board[i-][j] == 'O')
changeBoard(board, i-, j);
if(board[i+][j] == 'O')
changeBoard(board, i+, j);
if(board[i][j-] == 'O')
changeBoard(board, i, j-);
if(board[i][j+] == 'O')
changeBoard(board, i, j+);
} void solve(vector<vector<char>> &board) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int height = (int)board.size();
if(height == ) return;
int width = (int)board[].size(); // vector<int> temp(width, 0);
//
// vector<vector<int>> isVis(height, temp);
// bool flag = true;
//
// for(int i = 0; i < height; i++)
// {
// for(int j = 0; j < width; j++)
// {
// if(board[i][j] == 'O' && !isVis[i][j])
// {
// flag = true;
// if(dfs(board, i, j, height, width, isVis, flag)) // surround regions
// {
// changeBoard(board, i, j); // Find surround region directly may cause runtime error
// }
// }
// }
// } // Change my strategy to mark unsurrounded regions for(int i = ; i < height; i++)
{
dfs2(board, i, );
dfs2(board, i, width-);
} for(int j = ; j < width; j++)
{
dfs2(board, , j);
dfs2(board, height-, j);
} for(int i = ; i < height; i++)
{
for(int j = ; j < width; j++)
{
if(board[i][j] == 'O') board[i][j] = 'X';
if(board[i][j] == 'C') board[i][j] = 'O';
}
} // print result
for(int i = ; i < height; i++)
{
for(int j = ; j < width; j++)
{
cout << board[i][j] << ' ';
}
cout << endl;
} } }; int main()
{
vector<vector<char>> board{{'X','X','X','X'},{'X','O','O','X'},{'X','X','O','X'},{'X','O','X','X'}}; Solution sln;
sln.solve(board); return ;
}
此外,提交的代码是不能有输出语句的,否则会报Internal Error。
BFS也可以做:http://blog.sina.com.cn/s/blog_b9285de20101j1dt.html
Leetcode: Surrounded regions的更多相关文章
- [LeetCode] Surrounded Regions 包围区域
Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured ...
- 验证LeetCode Surrounded Regions 包围区域的DFS方法
在LeetCode中的Surrounded Regions 包围区域这道题中,我们发现用DFS方法中的最后一个条件必须是j > 1,如下面的红色字体所示,如果写成j > 0的话无法通过OJ ...
- LeetCode: Surrounded Regions 解题报告
Surrounded Regions Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A ...
- [leetcode]Surrounded Regions @ Python
原题地址:https://oj.leetcode.com/problems/surrounded-regions/ 题意: Given a 2D board containing 'X' and 'O ...
- LEETCODE —— Surrounded Regions
Total Accepted: 43584 Total Submissions: 284350 Difficulty: Medium Given a 2D board containing 'X' a ...
- LeetCode: Surrounded Regions [130]
[题目] Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is cap ...
- [LeetCode] 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 regi ...
- 【LeetCode】130. Surrounded Regions (2 solutions)
Surrounded Regions Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A ...
随机推荐
- BZOJ 2179: FFT快速傅立叶
2179: FFT快速傅立叶 Time Limit: 10 Sec Memory Limit: 259 MBSubmit: 2923 Solved: 1498[Submit][Status][Di ...
- Android利用HttpURLConnection实现模拟登录
最近在做一个APP,需要模拟登录教务处,之前曾经用HttpClient做过,点这里,但是发现最新的Android SDK已经不支持Httpclient了,所以只好在琢磨一下HttpURLConnect ...
- 【原】理解javascript中的闭包
闭包在javascript来说是比较重要的概念,平时工作中也是用的比较多的一项技术.下来对其进行一个小小的总结 什么是闭包? 官方说法: 闭包是指有权访问另一个函数作用域中的变量的函数.创建闭包的常见 ...
- .Net 中的反射(反射特性) - Part.3
反射特性(Attribute) 可能很多人还不了解特性,所以我们先了解一下什么是特性.想想看如果有一个消息系统,它存在这样一个方法,用来将一则短消息发送给某人: // title: 标题:author ...
- lodop打印控件
http://www.c-lodop.com/demolist/PrintSampIndex.html
- 界面通信之block传值
block传值有两种方式 ⽅式⼀: 使⽤block属性实现回调传值 ⽅式⼆: 在⽅法中定义block实现回调传值 方式一比较便于理解而且常用,下面介绍方式一是如何传值的 使用block属性传值和代理传 ...
- 一个类似宣传的H5页面
趁着闲置 做了一个H5的页面 感觉不错. 具体效果如下 框架上我选择 zepto(其实这个可有可无,推荐用原生的最好) FullPage (感觉挺好用的一个全屏滚动插件 ) pageResponse ...
- 2015.4.24 移动端,chrome不兼容或无法运行的一些具体问题
1.table内input,把它的边框和focus边框都变成透明,在ff可行,但是chrome会有样式,怎么解决? 解决方法:border:none;outline:0; 2.如下代码,css3动画在 ...
- maven项目常见问题
问题1:Maven项目,右键-update project后报错如下的解决办法: 1).DescriptionResourcePathLocationType Java compiler level ...
- jvm七种垃圾收集器
JVM_七种垃圾收集器介绍 本文中的垃圾收集器研究背景为:HotSpot+JDK7 一.垃圾收集器概述 如上图所示,垃圾回收算法一共有7个,3个属于年轻代.三个属于年老代,G1属于横跨年轻代和年老 ...