コード例 #1
0
        private IList<Node> getListOfBuilding(int[][] grid)
        {
            IList<Node> list = new List<Node>();

            if (grid == null || grid.Length == 0)
                return list;

            int m = grid.Length;
            int n = grid[0].Length;

            for (int i = 0; i < m; i++)
                for (int j = 0; j < n; j++)
                {
                    if (grid[i][j] == BUILDING)
                    {
                        Node node = new Node(i, j);
                        list.Add(node);
                    }

                }
            return list;
        }
コード例 #2
0
        /*
         *  January 20, 2016
         *  Design concern:
         *  BFS - search from a building node, 4 directions.
         *
         *  Filter out those 3 cases:
         *  1. The boundary check of matrix is ok
         *  2. Do not visit more than once
         *  3. Only check empty land node
         *
         * Ask myself, can this function be more simple?
         * 4 arguements, but different types, so it is not possible to mix with them.
         */
        private bool okToMove(
            Node        node, 
            int[]       direction, 
            int[][]     grid, 
            bool[][]    visited)
        {
            int m = getRowNo(grid);
            int n = getColNo(grid);

            int x = node.x + direction[0];
            int y = node.y + direction[1];

            if (x < 0 || x >= m || y < 0 || y >= n )
                return false ;

            if(visited[x][y])
                return false;

            if (grid[x][y] != EMTPYLAND)
                return false;

            return true;
        }
コード例 #3
0
        /*
         * January 20, 2016
         *
         * Design a BFS function
         * 3 arguments are int[][], do not mix them
         * grid - do not allow the change
         * dist - need to be updated
         * reach - need to be updated
         *
         * reference:
         * ref, out parameter
         * https://msdn.microsoft.com/en-us/library/s6938f28.aspx
         *
         * array
         * https://msdn.microsoft.com/en-us/library/hyfeyz71.aspx
         *
         * C# const analog - readonly - interface
         * http://stackoverflow.com/questions/114149/const-correctness-in-c-sharp
         *
         * Book:
         * Effective C#: 50 Specific Ways to Improve Your C# 1st Edition
        by Bill Wagner  (Author)
         * Item 2: Prefer readonly to const
         */
        private void BFS(Node origNode,            
            int[][] dist,
            int[][] reach,
            int[][] grid                        
            )
        {
            int m = getRowNo(grid);
            int n = getColNo(grid);

            bool[][] visited = new bool[m][];
            for (int i = 0; i < m; i++)
                visited[i] = new bool[n];

            Queue<Node> queue = new Queue<Node>();

            // add first node into the queue
            queue.Enqueue(new Node(origNode.x, origNode.y, 0));

            // BFS - visit all nodes in the grid
            while (queue.Count > 0)
            {
                Node node = queue.Dequeue();

                int distance = node.distance;

                distance++;    // increment one !

                // four directions - breadth first search
                int[][] directions = {new int[2]{-1,  0},    // left
                                      new int[2]{ 1,  0},    // right
                                      new int[2]{ 0, -1},    // down
                                      new int[2]{ 0,  1}     // up
                                     };

                for (int i = 0; i < directions.Length; i++)
                {
                    if(okToMove(node, directions[i], grid, visited))
                    {
                        // Let us complete some tasks
                        int x = node.x + directions[i][0];
                        int y = node.y + directions[i][1];   // bug001 - node.x - should be node.y

                        // alphabetic order
                        dist[x][y]      += distance;
                        reach[x][y]++;
                        visited[x][y]   = true;

                        queue.Enqueue(new Node(x, y, distance));
                    }
                }
            }
        }