i wrote a program for our c++ (not quiet c++!!) university project which has a big Bug!
the question is this:
we want to move in a square (with the measure of n×n)from the point zero to n.for example we have a 3×3 square and we want to move from 0×0 to 3×3.the program has to calculate all the ways possible and write them like RRUU or UURR or RURU and such.the problem is when i want to move from 0×0 to 3×3 cell there is no way like RRR or UUU and aside the problem it show the way like RRU which is wrong because we should have 4 characters like RRUU not RRU! so what is exactly the problem?!
here is the code which was written in bloodshed dev-c++ program:
#include <cstdlib>
#include <iostream>
#include <cstring>
using namespace std;
#define N 10
char ways[N+1];
int C ;
int print_ways(int x, int y) {
if (x > 0) {
ways[N - (x+y)] = 'R';
print_ways(x-1,y);
}
if (y > 0) {
ways[N - (x+y)] = 'U';
print_ways(x,y-1);
}
if (x==0&&y==0) {
ways[N] = 0;
printf ("%d %s\n", ++C, ways);
}
}
int main(void)
{
print_ways(N-1,N-1);
system("PAUSE");
return 0;
}
void printways( string path, int x, int y )
{
if ( x == 3 && y == 3 ) // arrived
{
cout << path << endl;
return;
}
if ( x < 3 ) // still room to go right
{
printways( path + "R", x + 1, y );
}
if ( y < 3 ) // still room to go up
{
printways( path + "U", x, y + 1 );
}
}
...
string path;
printways(path,1,1);