Unique_Paths_II

题目描述:

   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.
Note: m and n will be at most 100.
("Unique Paths"的后续:
现在考虑在网格中添加一些障碍物。
会有多少独特的路径?
在网格中,障碍物和空白分别被标记为1和0。
例如,
有一个障碍,在一个九宫格中如下图所示。
[
[0,0,0],
[0,1,0],
[0,0,0]
]
唯一路径的总数是2。
注:m和n最多为100。)
思路:和Unique Paths一样,只需要将障碍处的DP设置为0就可以.

public class Unique_Paths_II {
    public static int uniquePathsWithObstacles(int[][] obstacleGrid) 
    {
    	if(obstacleGrid.length==0||obstacleGrid[0].length==0)
    		return 0;
    	int DP[][] = new int[obstacleGrid.length][obstacleGrid[0].length];
    	DP[0][0] = 1;
    	for(int i=0;i<obstacleGrid.length;i++)
    	{
    		for(int j=0;j<obstacleGrid[0].length;j++)
    		{
    			if(obstacleGrid[i][j]==1)
    			{
    				DP[i][j] = 0;
    				continue;
    			}
    			if(i-1>=0)
					DP[i][j] += DP[i-1][j];
				if(j-1>=0)	
					DP[i][j] += DP[i][j-1];
    		}
    	}
        return DP[obstacleGrid.length-1][obstacleGrid[0].length-1];
    }
    public static void main(String[] args) {
    	int[][] obstacleGrid = {{0,0,0},{0,1,0},{0,0,0}};
    	System.out.println(uniquePathsWithObstacles(obstacleGrid));
	}
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面这段代码的作用是什么def setup_model(self): self.enumerate_unique_labels_and_targets() self.model = CasSeqGCN(self.args, self.number_of_features + self.args.number_of_hand_features, self.number_of_nodes) #给当前类中模型主体进行初始化,初始化为上面的模型 def create_batches(self): N = len(self.graph_paths) train_start, valid_start, test_start = \ 0, int(N * self.args.train_ratio), int(N * (self.args.train_ratio + self.args.valid_ratio)) train_graph_paths = self.graph_paths[0:valid_start] valid_graph_paths = self.graph_paths[valid_start:test_start] test_graph_paths = self.graph_paths[test_start: N] self.train_batches, self.valid_batches, self.test_batches = [], [], [] for i in range(0, len(train_graph_paths), self.args.batch_size): self.train_batches.append(train_graph_paths[i:i+self.args.batch_size]) for j in range(0, len(valid_graph_paths), self.args.batch_size): self.valid_batches.append(valid_graph_paths[j:j+self.args.batch_size]) for k in range(0, len(test_graph_paths), self.args.batch_size): self.test_batches.append(test_graph_paths[k:k+self.args.batch_size]) def create_data_dictionary(self, edges, features): """ creating a data dictionary :param target: target vector :param edges: edge list tensor :param features: feature tensor :return: """ to_pass_forward = dict() to_pass_forward["edges"] = edges to_pass_forward["features"] = features return to_pass_forward def create_target(self, data): """ Target createn based on data dicionary. :param data: Data dictionary. :return: Target size """ return torch.tensor([data['activated_size']])
05-18

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值