zoukankan      html  css  js  c++  java
  • 130. Surrounded Regions

    好傻逼。。这个题麻烦死了。

    简单的说,就是里面的O能想办法连到外面的O就不用被转换,否则就变为X。

    需要2个额外的BOARD 一个记录VISIT 一个记录最后剩下的O

    简单的说就是从最外面那圈开始,如果是O,就作为一个起点往上下左右找

    很多情况都不用再找了: 找到X,到边(除了一开始),VISIT过

    不是上述情况,就当前VisiT=TRUE 以免又循环回来,然后再四面八方。

    检测是不是边,要么专门设个FLAG,如果是一开始就不检测,不是一开始就检测。

    总之很麻烦,很容易就OVERFLOW

    public void solve(char[][] board)
    {

    	int row = board.length;
    	if(row == 0) return;
    	int col = board[0].length;
    	char[][] check = new char[row][col];
    	boolean[][] visit = new boolean[row][col];
    
    	//m = 0;
    
    	for(int n = 0; n < col; n++)
    	{
    		if(board[0][n] == 'O')
    		{
    			visit = new boolean[row][col];
    			helper(0,n,board,check,visit,true);
    			
    		}
    
    		if(board[board.length-1][n] == 'O')
    		{
    			visit = new boolean[row][col];
    			helper(board.length-1,n,board,check,visit,true);
    		}
    		
    	}
    
    	for(int m = 1; m < row-1; m++)
    	{
    		if(board[m][0] == 'O')
    		{
    			visit = new boolean[row][col];
    			
    			helper(m,0,board,check,visit,true);
    			
    		}
    		
    		if(board[m][board[0].length-1] == 'O')
    		{
    			visit = new boolean[row][col];
    			helper(m,board[0].length-1,board,check,visit,true);
    		}
    	}
       // System.out.println(board[1][1]);
    	for(int m = 1; m < row-1;m++)
    		for(int n = 1; n < col-1;n++)
    		{
    			if(board[m][n] == 'X') 
    			{
                    
    			}
    			else
    			{
    			    if(check[m][n] == 'O')
    			    {
    			        board[m][n] = 'O';
    			    }
    			    else board[m][n] = 'X';
    			}
    			
    			
    		}
    
    
        
    }
    
    public void helper(int m,int n,char[][] board, char[][] check, boolean[][] visit,boolean init)
    {
    	if(check[m][n] == 'X' ||  visit[m][n]) return;
    	if((!init) && (m == 0 || n == 0 || m == board.length-1|| n == board[0].length-1)) return;
        //System.out.println(m + " " +n);
    	check[m][n] = 'O';
    	visit[m][n] = true;
    
    	if(m>0 && board[m-1][n] == 'O') helper(m-1,n,board,check,visit,false);
    
    	if(n>0 && board[m][n-1] == 'O') helper(m,n-1,board,check,visit,false);
    
    	if(m < board.length-1 && board[m+1][n] == 'O') helper(m+1,n,board,check,visit,false);
    
    	if(n < board[0].length-1 && board[m][n+1] == 'O') helper(m,n+1,board,check,visit,false);
    
    }
    

    搞了好久。。。

    被自己蠢哭了。
    visit[i][j] = true写成了visit[j][j] = true 我DEBUG了1个多小时。。。。。。。

    image

    从最外面那圈的所有O开始顺着相邻的O走,里面那些能走到的O就是不用变成反革命分子的。否则最后变成X,反革命分子。

    主要是剪枝,乱七八糟一大堆情况,包括但不限于:

    走到边了,走出界了,已经接受过组织的审查了, 他本身就是反革命分子,etc..

    一开始4*4 test case没问题让我以为代码是正确的。结果后来各种stackoverflow,以为是不够好,改了依然如此,绝望中浪费了整整他妈的一个多小时。

    public class Solution
    {
    public void solve(char[][] board)
    {
    if(board.length == 0) return;
    boolean[][] moveVisit = new boolean[board.length][board[0].length];
    boolean[][] ok = new boolean[board.length][board[0].length];

        for(int i = 0; i < board.length;i++)
        {
            if(board[i][0] == 'O')
            {
    
                go(board,moveVisit,ok,i,0,true);
                moveVisit = new boolean[board.length][board[0].length];
            }
            if(board[i][board[0].length-1] == 'O')
            {
    
                go(board,moveVisit,ok,i,board[0].length-1,true);
                moveVisit = new boolean[board.length][board[0].length];
            }
        }
        
        for(int i = 1; i < board[0].length;i++)
        {
            if(board[0][i] == 'O')
            {
    
                go(board,moveVisit,ok,0,i,true);
                moveVisit = new boolean[board.length][board[0].length];
            }
            if(board[board.length-1][i]== 'O')
            {
    
                go(board,moveVisit,ok,board.length-1,i,true);
    
            }
        }
        
    
        
        
        for(int i = 1; i < board.length-1;i++)
        {
            for(int j = 1; j < board[0].length-1;j++)
            {
                if(board[i][j] == 'X') continue;
                
                if(!ok[i][j]) board[i][j] = 'X';
           
            }
        }
        
        return;
    }
    
    public void go(char[][] board, boolean[][] moveVisit, boolean[][] ok, int i, int j,boolean begin)
    {
        if(board[i][j] == 'X' || moveVisit[i][j]) return;
        if(!begin && (i == 0 || j == 0 || i == board.length-1 || j == board[0].length -1)) return;
        
        ok[i][j] = true;
        moveVisit[i][j] = true;
        
        if(i > 0 && board[i-1][j] == 'O') go(board,moveVisit,ok,i-1,j,false);
    
        
        
        if(j>0 && board[i][j-1] == 'O') go(board,moveVisit,ok,i,j-1,false);
    
        
        
        if(i < board.length-1 && board[i+1][j] == 'O') go(board,moveVisit,ok,i+1,j,false);
    
        
    
        if(j < board[0].length-1 && board[i][j+1] == 'O') go(board,moveVisit,ok,i,j+1,false);
    
        
        
        return;
    
    }
    

    }
    三刷。

    刚养成DFS开始CHECK的习惯,这个题瞬间打脸。。

    这个题不但开始要CHECK,最完美的情况是进入前再CHECK一下,保证不是最外面那圈,再进去……

    Time: O(mn)
    Space: O(1) 我好像一直把空间复杂度混淆了,我前面所指的空间复杂度其实指的是extra space...

    public class Solution {
    public void solve(char[][] board) {
    if (board.length == 0) return;
    int row = board.length;
    int col = board[0].length;

        for (int i = 0; i < row; i++) {
            if (board[i][0] == 'O')
                dfs(board, i, 0);
            if (board[i][col-1] == 'O')
                dfs(board, i, col-1);
        }
        
        for (int i = 1; i < col - 1; i++) {
            if (board[0][i] == 'O')
                dfs(board, 0, i);
            if (board[row - 1][i] == 'O')
                dfs(board,row-1, i);
        }
        
        for (int i = 0; i < board.length; i++) {
            for (int j = 0; j < board[0].length; j++) {
                if (board[i][j] == '1') {
                    board[i][j] = 'O';
                } else {
                    board[i][j] = 'X';
                }
            }
        }
        return;
        
    }
    public void dfs(char[][] board, int m, int n) {
        if (board[m][n] != 'O') return;
        board[m][n] = '1';
        if (m < board.length - 2)
            dfs(board, m + 1, n);
        if (m > 1)
            dfs(board, m - 1, n);
        if (n < board[0].length - 2)
            dfs(board, m, n + 1);
        if (n > 1)
            dfs(board, m, n - 1);
        
    }
    

    }

  • 相关阅读:
    python装饰器的wraps作用
    lambda函数和map函数
    python直接赋值、切片、浅拷贝和深拷贝
    ubuntu shell脚本出错 dash
    关于方法论和相关书籍
    如何安全的大数据量表在线进行DML操作
    mysql group by 查询非聚集列
    MongoTemplate进行增删改查
    Mockito 的用法
    一个人开始优秀的3种迹象
  • 原文地址:https://www.cnblogs.com/reboot329/p/6053028.html
Copyright © 2011-2022 走看看