1.从外围搜索O,深度搜索出现了

Line 35: java.lang.StackOverflowError
Last executed input: ["OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO
 public class Solution {
public void solve(char[][] board) {
if(board.length==0) return;
int len1=board.length;
int len2=board[0].length;
for(int i=0;i<len1;i++)
{
if(board[i][0]=='O') dfs(board,i,0);
if(board[i][len2-1]=='O') dfs(board,i,len2-1); }
for(int i=0;i<len2;i++)
{ if(board[0][i]=='O')dfs(board,0,i);
if(board[len1-1][i]=='O')dfs(board,len1-1,i);
}
for(int i=0;i<len1;i++)
{
for(int j=0;j<len2;j++)
{
if(board[i][j]=='h') board[i][j]='O';
else board[i][j]='X';
}
} }
public void dfs(char b[][],int i,int j)
{
if(i<0||i>b.length-1||j<0||j>b[0].length-1) return;
if(b[i][j]!='O') return; if(b[i][j]=='O') b[i][j]='h';
dfs(b,i+1,j);//up
dfs(b,i-1,j);//down
dfs(b,i,j+1);//left
dfs(b,i,j-1);//right; }
}

2.广度搜索一定可以了。抽空在写,(可惜不行,我太水了超时代码)

class node
{
int x;
int y;
char c;
node(int x1,int y1,char c1)
{
x=x1;
y=y1;
c=c1; }
} public class Solution {
public void solve(char[][] board) {
if(board.length==0) return;
int len1=board.length;
int len2=board[0].length;
for(int i=0;i<len1;i++)
{
if(board[i][0]=='O') bfs(board,i,0);
if(board[i][len2-1]=='O') bfs(board,i,len2-1); }
for(int i=0;i<len2;i++)
{ if(board[0][i]=='O')bfs(board,0,i);
if(board[len1-1][i]=='O')bfs(board,len1-1,i);
}
for(int i=0;i<len1;i++)
{
for(int j=0;j<len2;j++)
{
if(board[i][j]=='h') board[i][j]='O';
else board[i][j]='X';
}
} }
public boolean is(int i,int j,int len1,int len2,char c[][])
{
if(i>=0&&i<len1&&j>=0&&j<len2&&c[i][j]=='O') return true;
return false;
} //put the o to h
public void bfs(char b[][],int i,int j)
{
int len1=b.length;
int len2=b[0].length;
Queue<node> queue=new LinkedList<node>(); queue.offer(new node(i,j,b[i][j]));
while(!queue.isEmpty())
{
node n1=queue.poll();
b[n1.x][n1.y]='h'; if(is(n1.x,n1.y+1,len1,len2,b))
{ queue.offer(new node(n1.x,n1.y+1,b[n1.x][n1.y+1]));
}
if(is(n1.x,n1.y-1,len1,len2,b))
{ queue.offer(new node(n1.x,n1.y-1,b[n1.x][n1.y-1]));
}
if(is(n1.x+1,n1.y,len1,len2,b))
{ queue.offer(new node(n1.x+1,n1.y,b[n1.x+1][n1.y]));
}
if(is(n1.x-1,n1.y,len1,len2,b))
{ queue.offer(new node(n1.x-1,n1.y,b[n1.x-1][n1.y]));
} } } }

leetcode shttps://oj.leetcode.com/problems/surrounded-regions/的更多相关文章

  1. leetcode https://oj.leetcode.com/problems/jump-game-ii/

    1.超时的,效率太低 public class Solution { public int jump(int[] A) { int len=A.length; int d[]=new int[len] ...

  2. 【LeetCode OJ】Surrounded Regions

    Problem Link: http://oj.leetcode.com/problems/surrounded-regions/ We can do follows in the 2D board. ...

  3. [LeetCode] Surrounded Regions 包围区域

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

  4. [leetcode]Surrounded Regions @ Python

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

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

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

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

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

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

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

  8. Leetcode之深度优先搜索(DFS)专题-130. 被围绕的区域(Surrounded Regions)

    Leetcode之深度优先搜索(DFS)专题-130. 被围绕的区域(Surrounded Regions) 深度优先搜索的解题详细介绍,点击 给定一个二维的矩阵,包含 'X' 和 'O'(字母 O) ...

  9. 【leetcode】Surrounded Regions

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

随机推荐

  1. MyEclipse激活失败,解决办法

    文章参考:http://www.cnblogs.com/dingyuanxin/p/4046356.html 失败可能是:systemid和exe破解出来的那个对应不上: 1.启动MyEclipse, ...

  2. gulp之css,js压缩合并加密替换

    为了防止客户端的静态资源缓存,我们需要每次更新css或js的时候,通过md5或时间戳等方式重新命名静态资源.让客户端可以重新请求资源,而不是从缓存里取.然后html模板里的src也要做相应的修改.当然 ...

  3. php session 自定义的设置测试

    <?php // ini_set('session.save_handler', 'user'); // 注意 set_session_save_handler() 一定要在 session_s ...

  4. js学习--DOM操作详解大全 前奏(认识DOM)

    一 . 节点属性 DOM 是树型结构,相应的,可以通过一些节点属性来遍历节点树: 方法 说明 nodeName 节点名称,相当于tagName.属性节点返回属性名,文本节点返回#text.nodeNa ...

  5. windows下面composer安装yii2

    1,安装composer "https://getcomposer.org/Composer-Setup.exe" 2,安装 composer-asset-plugin ,打开cm ...

  6. 【python】【转】python中isinstance判断变量类型用法

    来源 http://www.jb51.net/article/15696.htm 在Python中只需要使用内置的函数isinstance,使用起来非常简单,比如下面的例子: 复制代码 代码如下: c ...

  7. js实现中文转拼音

    首先需要注意ES6在严格模式下中常量太长会出问题,CHAR_DICT.FULL_DICT.POLYPHONE都是很大的常量,所以我都外部加载了,否则编译运行会有问题,先贴代码,常量在最后,如下: js ...

  8. WPF之 XAML集合项简单演示

    我们直接通过xaml文件演示一个简单的xaml集合项: <Window x:Class="WPF_XAML集合项.MainWindow" xmlns="http:/ ...

  9. Django设置TIME_ZONE和LANGUAGE_CODE为中国区域

    Django默认的timezone是 TIME_ZONE = 'America/Chicago' LANGUAGE_CODE = 'en-us' 设置为中国区域: TIME_ZONE = 'Asia/ ...

  10. Junit 源码剖析(二)

    junit4 下的所有的testcase都是在Runner下执行的, 可以将Runner理解为junit运行的容器, 默认情况下junit会使用JUnit4ClassRunner作为所有testcas ...