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的更多相关文章

  1. [LeetCode] Surrounded Regions 包围区域

    Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured ...

  2. 验证LeetCode Surrounded Regions 包围区域的DFS方法

    在LeetCode中的Surrounded Regions 包围区域这道题中,我们发现用DFS方法中的最后一个条件必须是j > 1,如下面的红色字体所示,如果写成j > 0的话无法通过OJ ...

  3. LeetCode: Surrounded Regions 解题报告

    Surrounded Regions Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A ...

  4. [leetcode]Surrounded Regions @ Python

    原题地址:https://oj.leetcode.com/problems/surrounded-regions/ 题意: Given a 2D board containing 'X' and 'O ...

  5. LEETCODE —— Surrounded Regions

    Total Accepted: 43584 Total Submissions: 284350 Difficulty: Medium Given a 2D board containing 'X' a ...

  6. LeetCode: Surrounded Regions [130]

    [题目] Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is cap ...

  7. [LeetCode] Surrounded Regions 广度搜索

    Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured ...

  8. [LeetCode] 130. Surrounded Regions 包围区域

    Given a 2D board containing 'X' and 'O'(the letter O), capture all regions surrounded by 'X'. A regi ...

  9. 【LeetCode】130. Surrounded Regions (2 solutions)

    Surrounded Regions Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A ...

随机推荐

  1. Sikuli图形脚本测试工具安装及介绍(适合小白的测试神器)

    sikuli简单安装以及介绍附图: 一.简单介绍 SikuliX官方网站:https://launchpad.net/sikuli/(官方的最新版本是SikuliX1.1.0更新于2015-10-06 ...

  2. 【BZOJ-3553】三叉神经树 树链剖分

    3553: [Shoi2014]三叉神经树 Time Limit: 160 Sec  Memory Limit: 256 MBSubmit: 347  Solved: 112[Submit][Stat ...

  3. Angular.js实现折叠按钮的经典指令.

    var expanderModule=angular.module('expanderModule',[]) expanderModule.directive('expander',function( ...

  4. web开发——写一个简单的表格导出操作

    一.前台页面: 主要是一个按钮和一个表格,表格有显示数据,按钮负责将表格中的数据选择性地导出.除此外,可以附加一个小窗口和进度条,用于显示下载进度. 1. 按钮:<a href="ja ...

  5. Java学习笔记10

    31.编写当年龄age大于13且小于18时结果为true的布尔表达式age > 13 && age < 18 32.编写当体重weight大于50或身高大于160时结果为t ...

  6. php5 数据类型

    一.PHP主要有一下几种数据类型 String(字符串), Integer(整型), Float(浮点型), Boolean(布尔型), Array(数组), Object(对象), NULL(空值) ...

  7. PHP安装模式cgi、fastcgi、php_mod比较

    先了解一下普通cgi的工作流程: web server收到用户请求,并把请求提交给cgi程序,cgi程序根据请求提交的参数作相应处理,然后输出标准的html语句返回给web server,web se ...

  8. 更改CentOS 6.3 yum源为国内163源

    CentOS5.x: http://mirrors.163.com/.help/CentOS5-Base-163.repo CentOS6.x: http://mirrors.163.com/.hel ...

  9. .htaccess 基础教程(二)

    今天要介绍的:.htaccess的第一个应用是自定义错误页面,这将使你可以拥有自己的.个性化的错误页面(例如找不到文件时),而不是你的服务商提供的错误页或没有任何页面.这会让你的网站在出错的时候看上去 ...

  10. 如何查看、修改Linux的系统时间

    如题: 如何修改Linux的系统时间? date -s 05/27/2011 date -s 10:24:00 clock -w 就这三条命令就OK了! 查看/修改Linux时区和时间 一.时区 1. ...