找路径 带障碍

/**
 * Created by xialei on 15/10/9.
 * 也属于动态规划
 */
public class PathZhangai {
    public static void main(String[] args){

    }
    public int uniquePathsWithObstacles(int[][] obstacleGrid) {
        int m = obstacleGrid.length;     //获取数组行数
        int n = obstacleGrid[0].length;  //获取数组列数
        int[][] a = new int[m][n];       //存放路径
        a[0][0] = 1;
        if(obstacleGrid[0][0]==1){
            return 0;
        }
        if(m==0 || n==0){
            return 0;
        }
        for (int i = 1 ; i < m ; i++){
            a[i][0] = a[i-1][0] & (obstacleGrid[i][0]==0?1:0);
        }
        for(int j = 1 ; j < n ; j++ ){
            a[0][j] = a[0][j-1] & (obstacleGrid[0][j]==0?1:0);
        }
        for (int i = 1 ;i < m ;i++){
            for(int j = 1 ; j < n ;j++){
                if(obstacleGrid[i][j] == 0){
                    a[i][j]=a[i-1][j]+a[i][j-1];
                }
                else{
                    a[i][j]= 0;
                }
            }
        }
        return a[m-1][n-1];
    }
}

 

posted on 2015-10-11 15:46  上发条的姑娘  阅读(116)  评论(0编辑  收藏  举报

导航