课程: Advanced Go Programming: Data Structures, Code Architecture, and Testing

Challenge introduction

(upbeat music) - [Instructor] The fourth challenge will give you the opportunity to practice everything you've learned in solving the counting paths problem. Given a matrix of size m by n, start at the Element at coordinates 0,0 and end at the element m,n. You can move either to the right or downwards by using the correct indices. Every time you land on a given square, you incur the costs specified by the matrix. Your challenge is to implement a function that counts the number of paths through a matrix with a specified cost. In this example, you have two paths that satisfy the specified cost of eight. Traveling along the other paths will incur a larger cost. Of course, I've written some code that will make it easier for you to solve this challenge. On lines 13 to 17, I've created a Maze struct for you. This should make it possible for you to solve this challenge by not having to manipulate matrix values directly. On lines 19 to 27, I initialize the Maze struct with a matrix parameter. The Maze also provides functionality for memoization through the computedPaths map. You're already familiar with the use of this map for storing computed values. Further down on lines 29 to 43, you have implemented methods for verifying the current row and column indices. You can check whether we have reached the destination whether we are on the last row or last column of the matrix using these methods. Finally, on lines 45 to 58, I've implemented methods for the memoization of results. I construct a key based on the row column and cost parameters and save the paths count in the computedPaths map. Your challenge is to implement the countPaths function which takes in the maze, the row and column indices, as well as the cost as parameters. You should implement this function using the methods provided by the Maze struct. This problem is a great fit for the dynamic programming techniques we have been learning about so far. So make sure to use them in your solution. Good luck.

内容