Click here to Skip to main content
15,897,371 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Imagine a robot sitting on the upper left hand corner of an NxN grid. The robot can only move in three directions: right , down and diagonally down. The robot has to reach the lower right hand corner of the NxN grid. Imagine certain squares are “off limits” or “offsets”, such that the robot cannot step on them. Write a program to determine the number of possible paths for the robot.The entry of offset points ends when the user enters the coordinates as (-1,-1).
Input:
Enter the size of the grid
3
Enter the grid points that are offsets
0 1
-1 -1
output:
The paths for the robot are
( 0 , 0 ) - ( 1 , 0 ) - ( 2 , 0 ) - ( 2 , 1 ) - ( 2 , 2 )
( 0 , 0 ) - ( 1 , 0 ) - ( 1 , 1 ) - ( 2 , 1 ) - ( 2 , 2 )
( 0 , 0 ) - ( 1 , 0 ) - ( 1 , 1 ) - ( 1 , 2 ) - ( 2 , 2 )
( 0 , 0 ) - ( 1 , 0 ) - ( 1 , 1 ) - ( 2 , 2 )
( 0 , 0 ) - ( 1 , 0 ) - ( 2 , 1 ) - ( 2 , 2 )
( 0 , 0 ) - ( 1 , 1 ) - ( 2 , 1 ) - ( 2 , 2 )
( 0 , 0 ) - ( 1 , 1 ) - ( 1 , 2 ) - ( 2 , 2 )
( 0 , 0 ) - ( 1 , 1 ) - ( 2 , 2 )


What I have tried:

C++
#include <iostream>
using namespace std;
int numberOfPaths(int m, int n)
{
	int count[m][n];
	for (int i = 0; i < m; i++)
		count[i][0] = 1;
        for (int j = 0; j < n; j++)
		count[0][j] = 1;
	for (int i = 1; i < m; i++)
	{
		for (int j = 1; j < n; j++)
                   count[i][j] = count[i-1][j] + count[i][j-1]; 
	}
	return count[m-1][n-1];
}
int main()
{
	cout << numberOfPaths(3, 3);
	return 0;
}

//This is what I found to count the path if it can move only in 2 directions:right,down.
Posted
Updated 1-Jun-19 18:34pm
v2
Comments
PIEBALDconsult 2-Jul-17 12:32pm    
We won't do your homework for you.

Looks like to need to learn some analyze methods.
You need to proceed in order
Quote:
Imagine a robot sitting on the upper left hand corner of an NxN grid.
The robot has to reach the lower right hand corner of the NxN grid.
Imagine certain squares are “off limits” or “offsets”, such that the robot cannot step on them.

You need a grid of size NxN where you will remember positions that are off limits.
Quote:
The robot can only move in three directions: right , down and diagonally down.
Write a program to determine the number of possible paths for the robot.

You need to count all paths. which means that the robot can choose one of the 3 directions as long as the move do not hit a border of the grid or an off limit position.
This is a typical recursive problem: you need to write a function with actual position, if position is final position, it is a new path, otherwise for each moves, you check if move is allowed, and if allowed, call this same function with new position.
Either you do it with an explicit recursive function, or with implicit recursion using backtracking.
We do not do your HomeWork.
HomeWork is not set to test your skills at begging other people to do your work, it is set to make you think and to help your teacher to check your understanding of the courses you have taken and also the problems you have at applying them.
Any failure of you will help your teacher spot your weaknesses and set remedial actions.
Any failure of you will help you to learn what works and what don't, it is called 'trial and error' learning.

As programmer, your job is to create algorithms that solve specific problems and you can't rely on someone else to eternally do it for you, so there is a time where you will have to learn how to.
Creating an algorithm is basically finding the maths and make necessary adaptation to fit your actual problem.
 
Share this answer
 
( 0 , 0 ) - ( 1 , 1 ) - ( 1 , 2 ) - ( 2 , 2 )

( 0 , 0 ) - ( 1 , 1 ) - ( 2 , 2 )
 
Share this answer
 
#include <stdio.h>
int pospaths(int m, int n)
{
int count[m][n];
for (int i = 0; i < m; i++)
count[i][0] = 1;
for (int j = 0; j < n; j++)
count[0][j] = 1;
for (int i = 1; i < m; i++)
{
for (int j = 1; j < n; j++)
count[i][j] = count[i-1][j] + count[i][j-1] + count[i-1][j-1];
}
return count[m-1][n-1];
}
int main()
{
int size;
printf("Enter the size of the grid\n");
scanf("%d",&size);
printf("The number of paths is %d",pospaths(size, size));
return 0;
}
 
Share this answer
 
Comments
CHill60 3-Jun-19 6:00am    
Doing someone's homework for them does not help anyone. If they are still looking for the solution 2 years later then they probably deserved to fail the course

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900