zoukankan      html  css  js  c++  java
  • Unique Paths II

    package cn.edu.xidian.sselab.array;

    /**
     *
     * @author zhiyong wang
     * title:    Unique Paths II
     * content:
     * Follow up for "Unique Paths":
     * Now consider if some obstacles are added to the grids. How many unique paths would there be?
     * An obstacle and empty space is marked as 1 and 0 respectively in the grid.
     * For example,
     * There is one obstacle in the middle of a 3x3 grid as illustrated below.
     *
     *        [
     *          [0,0,0],
     *          [0,1,0],
     *          [0,0,0]
     *        ]
     *
     *    The total number of unique paths is 2.
     *
     */
    public class UniquePathsII {

        //递归没有想出怎么做来,不过这个是类似PascalTriangle
        /**
         * 1 1 1 1
         * 1 2 3 4
         * 1 3 6 10
         * 1 4 10 20
         * 即4*4的表,最多有20条路线,如果有障碍的话,把障碍换成0,把非障碍换成1
         * 自己没有想出解决办法,最后看的答案:思路是这样的,首先判断(0,0)的值,然后初始化第一行与第一列的值,
         * 第一个值遇0变1,遇1变0,对行进行初始化的时候,如果当前值是1,则置为0,如果是0,则前一个值赋予它
         * 对列 进行初始化的时候也是,当前值是1,则置为0,如果是0,则将上一个值赋予它
         * 初始化完成之后,就是对剩余所有的值开始进行判断,如果遇到1,则置为0,如果遇到0,则去上面值与前面值的和作为该点的值
         */
        
        public int uniquePathsWithObstacles(int[][] obstacleGrid){
            int length = obstacleGrid.length;
            int width = obstacleGrid[0].length;
            //第一步先初始化第一行与第一列
            obstacleGrid[0][0] ^= 1;
            for(int i=1;i<width;i++)
                obstacleGrid[0][i] = obstacleGrid[0][i]==1?0:obstacleGrid[0][i-1];
            for(int i=1;i<length;i++)
                obstacleGrid[i][0] = obstacleGrid[i][0]==1?0:obstacleGrid[i-1][0];
            //第二步开始求最后路径的和
            for(int i=1;i<length;i++){
                for(int j=1;j<width;j++){
                    obstacleGrid[i][j] = obstacleGrid[i][j]==1?0:obstacleGrid[i-1][j]+obstacleGrid[i][j-1];
                }
            }
            return obstacleGrid[length-1][width-1];
        }
        
    }

  • 相关阅读:
    时间相关概念
    如何查询Oracle EBS接口表
    生成随机数
    web.config/app.config敏感数据加/解密的二种方法
    浅谈.NET下的多线程和并行计算
    禁用浏览中的Tab键
    主从表关联,判断子表全部符合条件后主有才有效
    windows xp 中的administrator帐户不在用户登录内怎么解决?
    WebResourceCompression压缩模块以及.Net 程序脱离.net framework框架运行
    使用架构(XSD)验证XML文件
  • 原文地址:https://www.cnblogs.com/wzyxidian/p/5068629.html
Copyright © 2011-2022 走看看