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. Android开发笔记之《知识漏点纪录与学习》

    1. NDK的异常捕获方法 2. Andorid性能优化:http://www.cnblogs.com/yezhennan/p/5442557.html 3. 插件化与组件化:http://blog. ...

  2. AE开发使用内存图层

    AE开发中,有时需要从磁盘中读取一些文件信息如坐标点转为图层并进行分析,此过程并不需要坐标点入库之类的操作,就可以创建一个内存图层解决问题.创建内存图层需要用到InMemoryWorkspaceFac ...

  3. thinkphp 后台权限列表

    核心代码: // 检测用户权限权限 public function admin_priv($action){ $action_list = session('user.action_list'); i ...

  4. Solr学习总结(一)Solr介绍

       最近一直在搞Solr的问题,研究Solr 的优化,搜索引擎的bug修改等,这几天终于有时间,闲下来总结分享,以便大家参考,与大家一起来共同学习. Solr是一个基于Lucene的全文搜索引擎,同 ...

  5. C++11特性:decltype关键字

    decltype简介 我们之前使用的typeid运算符来查询一个变量的类型,这种类型查询在运行时进行.RTTI机制为每一个类型产生一个type_info类型的数据,而typeid查询返回的变量相应ty ...

  6. 统计SqlServer每张表内的数据量

    CREATE TABLE #temp (TableName VARCHAR (255), RowCnt INT)EXEC sp_MSforeachtable 'INSERT INTO #temp SE ...

  7. 最新的jQuery插件和JavaScript库

    每一个前端开发人员很清楚的重要性和功能的JavaScript库提供.它提供了一个简单的接口,用于构建快速动态的接口,而无需大量的代码. 谢谢你的超级从事jQuery开发者社区,人始终是创造新的和令人惊 ...

  8. golang笔记——struct

    1.定义一个结构体 type User struct { userid int username string password string } 2.初始化一个结构体 有两种情况,一是得到结构体的对 ...

  9. socket编程热身程序

    /*server.c*/ #include <stdio.h> #include <arpa/inet.h> #include <sys/types.h> /* S ...

  10. java中的等于

    数字的比较等于用“==” 不等于用“!=” 字符的比较等于用“.equals”不等于用”!s1.equals(s2)“