Robot moving in a grid

自古美人都是妖i 提交于 2021-02-07 14:32:19

问题


A robot is located at the top-left corner of a 4x4 grid. The robot can move either up, down, left, or right, but can not visit the same spot twice. The robot is trying to reach the bottom-right corner of the grid.The number of ways it can reach the bottom-right corner of the grid is?

Now i know that if the robot can only move down or right ,then the answer would be 8C4 because it has to go 4 squares to the right and 4 squares down, in any order.

But i am having difficulty in solving the problem when the robot can move both left and up!?

I just need a hint to solve the problem! How should i approach the problem?


回答1:


You can write a recursive program that calculates all possible paths, and whenever it arrives at the down right corner it increments the number of paths. I wrote something, but I didn't test it. (Think of it as pseudocode to give you a start). Basically what this does, is call the moveRobot function on the current position (0, 0) with an empty field (the robot hasn't moved yet). Then it tries to move up, down, left and right. This movement is described in the respective functions. If one of these movements succeds(or more than one), the new position is marked in the field with a 1 instead of a 0. 1 means the robot has passed through that position. Then you call moveRobot again. This because in the new position you want to try all four movements once more.

Main Function:

int field[4][4];
for (int i = 0; i < 4; i++)
    for (int j = 0; j < 4; j++)
        field[i][j] = 0;
field[0][0] = 1;
numPaths = 0;
moveRobot(0, 0, field);
print numPaths;

MoveRobot Function:

moveRobot(int row, int column, int[][] field)
{
    moveRobotUp(row, column, field);
    moveRobotDown(row, column, field);
    moveRobotLeft(row, column, field);
    moveRobotRight(row, column, field);
}

Other Functions:

moveRobotUp(int row, int column, int[][] field)
{
    if (row == 0) return;
    else 
    {
        if (field[row-1][column] == 1) return;
        field[row-1][column] = 1;
        moveRobot(row-1, column, field);
        field[row-1][column] = 0;
    }
}

moveRobotDown(int row, int column, int[][] field)
{
    if (row == 3 && column == 3) 
    {
        numPaths++;
        return;
    }
    else if (row == 3) return;
    else
    {
        if (field[row+1][column] == 1) return;
        field[row+1][column] = 1;
        moveRobot(row+1, column, field);
        field[row+1][column] = 0;
    }
}

moveRobotLeft(int row, int column, int[][] field)
{
    if (column == 0) return;
    else
    {
        if (field[row][column-1] == 1) return;
        field[row][column-1] = 1;
        moveRobot(row, column-1, field);
        field[row][column-1] = 0;
    }
}

moveRobotRight(int row, int column, int[][] field)
{
    if (column == 3 && row == 3) 
    {
        numPaths++;
        return;
    }
    else if (column == 3) return;
    else 
    {
        if (field[row][column+1] == 1) return;
        field[row][column+1] = 1;
        moveRobot(row, column+1, field);
        field[row][column+1] = 0;
    }
}



回答2:


This will work fine. The answer is 184.

public class RobotMovementProblem 
{
    public static void main(String[] args) 
    {
        int grid[][]=new int[4][4];
        System.out.println(countPaths(grid, 0, 0));
    }
    static int countPaths(int grid[][],int i,int j)
    {

        if ( i < 0 || j < 0 || i >= 4 || j >= 4 ) return 0;
        if ( grid[i][j] == 1 ) return 0;
        if ( i == 3 && j == 3 ) return 1;
        int arr[][]=new int[4][4];
        for(int m=0;m<4;m++)
        {
            for(int n=0;n<4;n++)
            {
                arr[m][n]=grid[m][n];
            }
        }

        arr[i][j] = 1;
        return countPaths(arr, i, j+1) + countPaths(arr, i, j-1) +  countPaths(arr, i+1, j) + countPaths(arr, i-1, j);  
    }
}



回答3:


/* building on red_eyes answer */

public static void main(String[] args) {
    Main m = new Main();
    boolean grid [][]= new boolean [4][4];
    sol=0;
    grid[0][0]= true;
    m.start(0,1,grid);
    System.out.println(sol);//output 184
}

    private void start(int x, int y,boolean [][]grid){
    grid[x][y]=true;
    moveUp(x,y,grid);
    moveDown(x,y,grid);     
    moveLeft(x,y,grid);     
    moveRight(x,y,grid);         
}

private void moveUp(int x, int y, boolean [][] grid){
    if(y==0) return;
    else{
        if (grid[x][y-1]) return;
        grid[x][y-1]=true;
        start(x,y-1,grid);
        grid[x][y-1]=false;
    }
}
   private void moveLeft(int x, int y, boolean [][] grid){
    if(x==0) return;
    else{
        if (grid[x-1][y]) return;
        grid[x-1][y]=true;
        start(x-1,y,grid);
        grid[x-1][y]=false; 
    }
}
private void moveDown(int x, int y, boolean [][] grid){
    if(x==3 && y==3){
        sol++;
        grid[x][y]=true;
        return;
    }
    else if(y==3) return;
    else{
        if (grid[x][y+1]) return;
        grid[x][y+1]=true;
        start(x,y+1,grid);
        grid[x][y+1]=false;
    }
}


private void moveRight(int x, int y, boolean [][] grid){
    if(x==3 && y==3){
        sol++;
        grid[x][y]=true;
        return;
    }else if(x==3) return;
    else{
        if (grid[x+1][y]) return;
        grid[x+1][y]=true;
        start(x+1,y,grid);
        grid[x+1][y]=false; 
    }
}



回答4:


Few lines of recursion will solve this.

    public static int WaysToExit(int x, int y)
    {
        if (x==0 || y == 0)
        {
            return 1;
        }
        else
        {
            return (WaysToExit(x - 1, y) + WaysToExit(x , y-1));
        }
    }


来源:https://stackoverflow.com/questions/17018312/robot-moving-in-a-grid

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!