Please help me solve this problem....urgent
Author |
Message |
rooste #1 / 9
|
 Please help me solve this problem....urgent
Hello everybody, I am despairing an urgently seeking for help. I must use and change the code (1) written in C, in order to solve the problem 2. I count on some kind programmer to help me. I would like that it is very urgent for me to solve this problem and you
Thank you in advance CODE (1) //---------------------------------------------------------- // // play // //---------------------------------------------------------- #include <stdio.h> #include <conio.h> #define true 1 #define false 0 #define STAMPAPASSI typedef int bool; void initMatrice(int m[10][10]); void stampaMatrice(int m[10][10]); bool convalida ( int riga, int colonna, int m[10][10] ); bool vinto(int m[10][10]); bool muovi(int riga, int colonna, int matrice[10][10], int numero); void main(void) { char x; int matrice[10][10]; initMatrice(matrice); if (muovi(0,0,matrice,0) == true) { printf("Game over"); Quote: }
stampaMatrice(matrice); x = getch(); Quote: }
//---------------------------------------------------------- // intialization of the matrix //---------------------------------------------------------- void initMatrice(int m[10][10]) { int i; int j; for(i=0;i<10;i++) { for(j=0;j<10;j++) { m[i][j] = 0; Quote: } } }
//---------------------------------------------------------- // print the matrix //---------------------------------------------------------- void stampaMatrice(int m[10][10]) { int i; int j; for(i=0;i<10;i++) { for(j=0;j<10;j++) { printf("%4d",m[i][j]); Quote: } printf("\n\n"); } }
//---------------------------------------------------------- // validate the coordinates //---------------------------------------------------------- bool convalida(int riga,int colonna,int m[10][10]) { if( riga < 0 || riga > 9 || colonna < 0 || colonna > 9) { return false; Quote: }
if (m[riga][colonna] != 0) { return false; Quote: } return true; }
/* The function choosing the first move: parametri: 1. position on the line, 2. position on the column 3. pointer to the matix 4. numeber of the move returned value True: the function arrived to 100 False: the function is not able to make a move */ bool muovi(int riga, int colonna, int matrice[10][10], int numero) { // first of all I must check whether the postion where I've been sent is inside the game field //and is free if (convalida(riga, colonna, matrice) == false) return false; // I define at which number of the recorsive I am // I write my position on the matrix matrice[riga][colonna] = numero; #ifdef STAMPAPASSI stampaMatrice(matrice); #endif // if I am at the recorsive function 100 the game is over and all the moves are correct if (numero == 100) return true; //I try to move nortwards if (muovi(riga-3,colonna, matrice, numero) == true){ //Game over I reached 100 return true; Quote: }
// I reach this point is the move to north was not successful // I try north east if (muovi(riga-2, colonna+2, matrice, numero) == true){ return true; Quote: }
// I try to east if (muovi(riga, colonna+3, matrice, numero) == true){ return true; Quote: }
// I try to south east if (muovi(riga+2, colonna+2, matrice, numero) == true){ return true; Quote: }
// southwards if (muovi(riga+3, colonna, matrice, numero) == true){ return true; Quote: }
// to south west if (muovi(riga+2, colonna-2, matrice, numero) == true){ return true; Quote: }
// to west if (muovi(riga, colonna-3, matrice, numero) == true){ return true; Quote: }
// to north west if (muovi(riga-2, colonna-2, matrice, numero) == true){ return true; Quote: }
/* If none of the moves is valid there is no solution. I let // the calling function try another cardinal direction and meanwhile I remove the wrong number //from the matrix. */ matrice[riga][colonna] = 0; return false; Quote: }
PROBLEMA N 2 given the matrix char 10x10: | 0123456789 -+----------- | 0| 000000*000 1| *****0***0 3| *00000*000 4| 00******0* 5| 0****0000* 6| 000000***0 7| ***0***000 8| 00*00*000* 9| ****000*00 starting from the position 0,0 I must try to find the position 10,10 moving only on the '0' considering the '*' as obstacles.
|
Mon, 25 Apr 2005 02:56:13 GMT |
|
 |
Emmanuel Delahay #2 / 9
|
 Please help me solve this problem....urgent
Quote: > PROBLEMA N 2 > given the matrix char 10x10: >| 0123456789 > -+----------- >| > 0| 000000*000 > 1| *****0***0
Have you noticed that the line 2 is missing? Quote: > 3| *00000*000 > 4| 00******0* > 5| 0****0000* > 6| 000000***0 > 7| ***0***000 > 8| 00*00*000* > 9| ****000*00
-- -ed- emdel at noos.fr ~]=[o FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/ C-library: http://www.dinkumware.com/htm_cl/index.html "Mal nommer les choses c'est ajouter du malheur au monde." -- Albert Camus.
|
Mon, 25 Apr 2005 04:14:34 GMT |
|
 |
rooste #3 / 9
|
 Please help me solve this problem....urgent
Quote:
> > PROBLEMA N 2 > > given the matrix char 10x10: > >| 0123456789 > > -+----------- > >| > > 0| 000000*000 > > 1| *****0***0 > Have you noticed that the line 2 is missing? > > 3| *00000*000 > > 4| 00******0* > > 5| 0****0000* > > 6| 000000***0 > > 7| ***0***000 > > 8| 00*00*000* > > 9| ****000*00 > -- > -ed- emdel at noos.fr ~]=[o > FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/ > C-library: http://www.dinkumware.com/htm_cl/index.html > "Mal nommer les choses c'est ajouter du malheur au monde." > -- Albert Camus.
Sure, that was a typo :-) Here is the matrix 0| 000000*000 1| *****0***0 2|***0***0*** 3| *00000*000 4| 00******0* 5| 0****0000* 6| 000000***0 7| ***0***000 8| 00*00*000* 9| ****000*00
|
Mon, 25 Apr 2005 04:53:29 GMT |
|
 |
Dave Near #4 / 9
|
 Please help me solve this problem....urgent
On Wed, 06 Nov 2002 19:56:13 +0100, rooster said: Quote: > Hello everybody, > I am despairing an urgently seeking for help. > I must use and change the code (1) written in C, in order to solve the > problem 2.
That's not C. It's some variant of C, but it's not C. Quote: > I would like that it is very urgent for me to solve this problem and you
By usenet convention, this is considered rude. If you ask a question in a public forum, you should expect the answer to also be given in a public forum. This is not only because others may benefit from the answer, but also because in the likely event that the answer is incomplete or partially incorrect, the peer review process will rectify that. So it's not just for others, but also for you. Quote: > //---------------------------------------------------------- > // > // play > // > //----------------------------------------------------------
It's a nit, given that // comments are in C99, and most compilers support them, but you should use /* */ for comments when posting here. Quote: > #include <stdio.h> > #include <conio.h>
This isn't a C header. It's a platform-specific header. Quote: > #define true 1 > #define false 0 > #define STAMPAPASSI > typedef int bool; > void initMatrice(int m[10][10]); > void stampaMatrice(int m[10][10]); > bool convalida ( int riga, int colonna, int m[10][10] ); > bool vinto(int m[10][10]); > bool muovi(int riga, int colonna, int matrice[10][10], int numero);
You know that all these functions do not get an array of arrays, they get a pointer to an array, right? Also, you should be aware that this is an English language forum, and a translation of function names and parameters would be useful (I don't know what vinto means, although I can guess what muovi means), and convalida means nothing to me...). Quote: > void main(void)
Please read the FAQ before posting here - main is defined to return int. Any other definition of main is wrong. Quote: > { > char x; > int matrice[10][10];
Some indentation would be nice... Quote: > initMatrice(matrice); > if (muovi(0,0,matrice,0) == true) { > printf("Game over"); > } > stampaMatrice(matrice); > x = getch();
Again, getch() is not a C function. Quote: > }
<snip> Quote: > bool muovi(int riga, int colonna, int matrice[10][10], int numero) > { > //I try to move nortwards > if (muovi(riga-3,colonna, matrice, numero) == true){ > //Game over I reached 100 > return true; > } > // I reach this point is the move to north was not successful > // I try north east > if (muovi(riga-2, colonna+2, matrice, numero) == true){ > return true; > }
I don't understand why these numbers are +/-3 for n, s, e, w and +/-2 for ne, nw, se and sw... <snip> Quote: > } > PROBLEMA N 2 > given the matrix char 10x10: >| 0123456789 > -+----------- >| > 0| 000000*000 > 1| *****0***0 > 3| *00000*000 > 4| 00******0* > 5| 0****0000* > 6| 000000***0 > 7| ***0***000 > 8| 00*00*000* > 9| ****000*00 > starting from the position 0,0 I must try to find the position 10,10 > moving only on the '0' considering the '*' as obstacles.
What problem are you having? The solution to this problem follows almost trivially from the program above. Cheers, Dave. -- David Neary, E-Mail: bolsh at gimp dot org CV: http://www.redbrick.dcu.ie/~bolsh/CV/CV.html
|
Mon, 25 Apr 2005 17:42:46 GMT |
|
 |
rooste #5 / 9
|
 Please help me solve this problem....urgent
Quote: > >| 0123456789 > > -+----------- > >| > > 0| 000000*000 > > 1| *****0***0 > > 3| *00000*000 > > 4| 00******0* > > 5| 0****0000* > > 6| 000000***0 > > 7| ***0***000 > > 8| 00*00*000* > > 9| ****000*00 > > starting from the position 0,0 I must try to find the position 10,10 > > moving only on the '0' considering the '*' as obstacles. > What problem are you having? The solution to this problem follows > almost trivially from the program above. > Cheers, > Dave.
Hi Dave, I do not understand why but I am not able to fit the first code to the second problem. I am am newbie....that could be an explanation !!! Could you please help me? :-) By the way the right matrix is: 0| 000000*000 1| *****0***0 2|***0***0*** 3| *00000*000 4| 00******0* 5| 0****0000* 6| 000000***0 7| ***0***000 8| 00*00*000* 9| ****000*00 Quote: > -- > David Neary, > E-Mail: bolsh at gimp dot org > CV: http://www.redbrick.dcu.ie/~bolsh/CV/CV.html
|
Tue, 26 Apr 2005 01:49:20 GMT |
|
 |
Emmanuel Delahay #6 / 9
|
 Please help me solve this problem....urgent
Quote: > Here is the matrix > 0| 000000*000 > 1| *****0***0 > 2|***0***0***
Have you noticed that there is an extra character here -- -ed- emdel at noos.fr ~]=[o FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/ C-library: http://www.dinkumware.com/htm_cl/index.html "Mal nommer les choses c'est ajouter du malheur au monde." -- Albert Camus.
|
Tue, 26 Apr 2005 02:44:29 GMT |
|
 |
rooste #7 / 9
|
 Please help me solve this problem....urgent
Quote:
> > Here is the matrix > > 0| 000000*000 > > 1| *****0***0 > > 2|***0***0*** > Have you noticed that there is an extra character here > -- > -ed- emdel at noos.fr ~]=[o > FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/ > C-library: http://www.dinkumware.com/htm_cl/index.html > "Mal nommer les choses c'est ajouter du malheur au monde." > -- Albert Camus.
You are right. That was a typo. The correct matrix is: 0123456789 -+----------- 0| 000000*000 1| *****0***0 2| ***0***0** 3| *00000*000 4| 00******0* 5| 0****0000* 6| 000000***0 7| ***0***000 8| 00*00*000* 9| ****000*00
|
Tue, 26 Apr 2005 07:43:33 GMT |
|
 |
E. Gibbo #8 / 9
|
 Please help me solve this problem....urgent
Quote:
>You are right. That was a typo. >The correct matrix is: >0123456789 >-+----------- >0| 000000*000 >1| *****0***0 >2| ***0***0** >3| *00000*000 >4| 00******0* >5| 0****0000* >6| 000000***0 >7| ***0***000 >8| 00*00*000* >9| ****000*00
Allow me to make yet one more slight correction to this "correct" matrix, to make the column numbers line up: 0123456789 -+----------- 0| 000000*000 1| *****0***0 2| ***0***0** 3| *00000*000 4| 00******0* 5| 0****0000* 6| 000000***0 7| ***0***000 8| 00*00*000* 9| ****000*00 IIRC, the original problem (corrected for 0-based indexing :)) was how to get from (0,0) to (9,9), moving only through "0" cells, considering "*" cells to be barriers to movement. With the addition of row 2, I don't see how that's possible; seems you can get as far as (5,1), and that's it. Is quantum tunneling allowed? :) Let me guess: there's another error waiting to be corrected. The problem itself may teach you something about programming, but be sure to also pay attention to the implicit lesson in the importance of accuracy, embodied in all these back-and-forth corrections! ;) --Ben --
|
Tue, 26 Apr 2005 08:30:03 GMT |
|
 |
Dave Near #9 / 9
|
 Please help me solve this problem....urgent
On Thu, 07 Nov 2002 18:49:20 +0100, rooster said: Quote: >> > starting from the position 0,0 I must try to find the position 10,10 >> > moving only on the '0' considering the '*' as obstacles. >> What problem are you having? The solution to this problem follows >> almost trivially from the program above. > I do not understand why but I am not able to fit the first code to the second > problem. I am am newbie....that could be an explanation !!! > Could you please help me? :-)
The algorithm is simple - the program you have implements it with some minor changes... while we're not finished, mark current square as visited check, in order, the squares n, ne, e, se, s, sw, w, nw for a square which has not yet been visited if we have an unvsited square, go there. otherwise, mark this square as a dead end check, in order, the squares n, ne, e, se, s, sw, w, nw for a square which has been visited and is not a dead end if we have a non-dead-end, go there. otherwise give up and come up with a better algorithm, or concede that there is no path to the finish. end if end if end while Now all you have to do is implement that algorithm with the code you have. Cheers, Dave. -- David Neary, E-Mail: bolsh at gimp dot org CV: http://www.redbrick.dcu.ie/~bolsh/CV/CV.html
|
Sat, 30 Apr 2005 19:19:44 GMT |
|
|
|