[algogeeks] Re: Google Interview Question-Snake and Ladder Design
take this approach fill array of snakes starting position in snake[num_snake] for each snake[i] , take the end of snake and fill in some other array take random number gen and fill these arrays-- e.g. end_snake[i] = ran(start_snake[i]-10) // so that snake does not end up in same row same logic for ladders after filling check snake start/end /ladder start/end are not coinciding after that u need to make dice object and again ran will come to rescue :) now current pos of board can be controlled using player color and board array incr /decr using dice move and snake start/end, ladder start/end array On Sep 15, 2:38 am, Minotauraus anike...@gmail.com wrote: And please stop posting the same thing twice. It's been happening for the past couple of days at least. @Question: I think you can use graphs and flood fill algo for this. Every possible move can be represented with an edge. Flood fill will help you figure out possible moves from you current position. What do you think? On Sep 14, 2:51 am, ankur aggarwal ankur.mast@gmail.com wrote: @bittuu write your algo then code On Tue, Sep 14, 2010 at 1:36 PM, bittu shashank7andr...@gmail.com wrote: #includestdlib.h #includestdio.h #includemath.h #includeconio.h int turn, square; long game, totalgames; int seed; int chutehit[10], ladderhit[9]; float RunningTurnTotal; float average; char reply; void ChuteStats() {printf(Chute and Ladder Statistics:\n\n); printf(Chute0: %d Ladder0: %d\n, chutehit[0], ladderhit[0]); printf(Chute1: %d Ladder1: %d\n, chutehit[1], ladderhit[1]); printf(Chute2: %d Ladder2: %d\n, chutehit[2], ladderhit[2]); printf(Chute3: %d Ladder3: %d\n, chutehit[3], ladderhit[3]); printf(Chute4: %d Ladder4: %d\n, chutehit[4], ladderhit[4]); printf(Chute5: %d Ladder5: %d\n, chutehit[5], ladderhit[5]); printf(Chute6: %d Ladder6: %d\n, chutehit[6], ladderhit[6]); printf(Chute7: %d Ladder7: %d\n, chutehit[7], ladderhit[7]); printf(Chute8: %d Ladder8: %d\n, chutehit[8], ladderhit[8]); printf(Chute9: %d \n, chutehit[9]); } int main() { printf(Welcome to the Chutes and Ladders simulation \n); printf(...\n); srand(1); //printf(How many games would you like me to run? __ ); //scanf(%i,totalgames); ///printf(\n You have chosen to run %i games... thank you! \n, totalgames); totalgames+=2; RunningTurnTotal=0.0; game=1; do{ turn=0; square=0; /** Reset game **/ do /** Begin game loop **/ { ++turn; RunningTurnTotal = RunningTurnTotal + 1; square = square + 1 + rand()%6; /** Spin and move **/ printf(square =%d \n,square); if (square == 1) {square=23; ++ladderhit[0];} /** Ladders? **/ if (square == 4) {square=14; ++ladderhit[1];} if (square == 9) {square=31; ++ladderhit[2];} if (square == 21) {square=42; ++ladderhit[3];} if (square == 28) {square=84; ++ladderhit[4];} if (square == 36) {square=44; ++ladderhit[5];} if (square == 51) {square=67; ++ladderhit[6];} if (square == 71) {square=91; ++ladderhit[7];} if (square == 80) {square=100;++ladderhit[8];}/// so when 80 comes raech to our goal exit if (square == 98) {square=78; ++chutehit[0];} /** Chutes ? **/ if (square == 95) {square=75; ++chutehit[1];} if (square == 93) {square=73; ++chutehit[2];} if (square == 87) {square=24; ++chutehit[3];} if (square == 62) {square=19; ++chutehit[4];} if (square == 64) {square=60; ++chutehit[5];} if (square == 56) {square=53; ++chutehit[6];} if (square == 49) {square=11; ++chutehit[7];} if (square == 48) {square=26; ++chutehit[8];} if (square == 16) {square=6; ++chutehit[9];} } while (square100);//terminate if random no. is 100 printf(\n\n Game over after %d turns\n, turn); printf(\nSimulation complete... beginning statistical analysis...\n\n); printf(Total number of games played: %d \n, game); printf(Total number of turns: %f \n, RunningTurnTotal); average = RunningTurnTotal / game; printf(Avg number of turns per game: %f \n, average); printf(\n); ChuteStats(); printf(\n); ++game; printf(\n\n Would you like to run the simulation again? (1=Yes)...); scanf(%i,reply); if(reply==1)//e.g. reply==1 totalgames+=1; else exit(0);// exit } while
Re: [algogeeks] Re: Google Interview Question-Snake and Ladder Design
@bittu, we are here to discuss the way to solve it. Posting a code here will not do anything good. Anil Kumar S. R. http://sranil.googlepages.com/ The best way to succeed in this world is to act on the advice you give to others. On 14 September 2010 13:33, bittu shashank7andr...@gmail.com wrote: #includestdlib.h #includestdio.h #includemath.h #includeconio.h ///O(N^2) solution Does solution exits in O(n) or (nlogn)..? reply me sum1 git dis.. //i will post analysis of dsi program later int turn, square; long game, totalgames; int seed; int chutehit[10], ladderhit[9]; float RunningTurnTotal; float average; char reply; void ChuteStats() {printf(Chute and Ladder Statistics:\n\n); printf(Chute0: %d Ladder0: %d\n, chutehit[0], ladderhit[0]); printf(Chute1: %d Ladder1: %d\n, chutehit[1], ladderhit[1]); printf(Chute2: %d Ladder2: %d\n, chutehit[2], ladderhit[2]); printf(Chute3: %d Ladder3: %d\n, chutehit[3], ladderhit[3]); printf(Chute4: %d Ladder4: %d\n, chutehit[4], ladderhit[4]); printf(Chute5: %d Ladder5: %d\n, chutehit[5], ladderhit[5]); printf(Chute6: %d Ladder6: %d\n, chutehit[6], ladderhit[6]); printf(Chute7: %d Ladder7: %d\n, chutehit[7], ladderhit[7]); printf(Chute8: %d Ladder8: %d\n, chutehit[8], ladderhit[8]); printf(Chute9: %d\n, chutehit[9]); } int main() { printf(Welcome to the Chutes and Ladders simulation \n); printf(...\n); srand(1); //printf(How many games would you like me to run? __ ); //scanf(%i,totalgames); ///printf(\n You have chosen to run %i games... thank you! \n, totalgames); totalgames+=2; RunningTurnTotal=0.0; game=1; do{ turn=0; square=0; /** Reset game **/ do /** Begin game loop **/ { ++turn; RunningTurnTotal = RunningTurnTotal + 1; square = square + 1 + rand()%6; /** Spin and move **/ printf(square =%d \n,square); if (square == 1) {square=23; ++ladderhit[0];} /** Ladders? **/ if (square == 4) {square=14; ++ladderhit[1];} if (square == 9) {square=31; ++ladderhit[2];} if (square == 21) {square=42; ++ladderhit[3];} if (square == 28) {square=84; ++ladderhit[4];} if (square == 36) {square=44; ++ladderhit[5];} if (square == 51) {square=67; ++ladderhit[6];} if (square == 71) {square=91; ++ladderhit[7];} if (square == 80) {square=100;++ladderhit[8];}/// so when 80 comes raech to our goal exit if (square == 98) {square=78; ++chutehit[0];} /** Chutes ? **/ if (square == 95) {square=75; ++chutehit[1];} if (square == 93) {square=73; ++chutehit[2];} if (square == 87) {square=24; ++chutehit[3];} if (square == 62) {square=19; ++chutehit[4];} if (square == 64) {square=60; ++chutehit[5];} if (square == 56) {square=53; ++chutehit[6];} if (square == 49) {square=11; ++chutehit[7];} if (square == 48) {square=26; ++chutehit[8];} if (square == 16) {square=6; ++chutehit[9];} } while (square100);//terminate if random no. is 100 printf(\n\n Game over after %d turns\n, turn); printf(\nSimulation complete... beginning statistical analysis...\n\n); printf(Total number of games played: %d \n, game); printf(Total number of turns: %f \n, RunningTurnTotal); average = RunningTurnTotal / game; printf(Avg number of turns per game: %f \n, average); printf(\n); ChuteStats(); printf(\n); ++game; printf(\n\n Would you like to run the simulation again? (1=Yes)...); scanf(%i,reply); if(reply==1)//e.g. reply==1 totalgames+=1; else exit(0);// exit } while (gametotalgames); getch(); } -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algoge...@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.comalgogeeks%2bunsubscr...@googlegroups.com . For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en. -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algoge...@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
[algogeeks] Re: Google Interview Question-Snake and Ladder Design
#includestdlib.h #includestdio.h #includemath.h #includeconio.h ///O(N^2) solution Does solution exits in O(n) or (nlogn)..? reply me sum1 git dis.. //i will post analysis of dsi program later int turn, square; long game, totalgames; int seed; int chutehit[10], ladderhit[9]; float RunningTurnTotal; float average; char reply; void ChuteStats() {printf(Chute and Ladder Statistics:\n\n); printf(Chute0: %d Ladder0: %d\n, chutehit[0], ladderhit[0]); printf(Chute1: %d Ladder1: %d\n, chutehit[1], ladderhit[1]); printf(Chute2: %d Ladder2: %d\n, chutehit[2], ladderhit[2]); printf(Chute3: %d Ladder3: %d\n, chutehit[3], ladderhit[3]); printf(Chute4: %d Ladder4: %d\n, chutehit[4], ladderhit[4]); printf(Chute5: %d Ladder5: %d\n, chutehit[5], ladderhit[5]); printf(Chute6: %d Ladder6: %d\n, chutehit[6], ladderhit[6]); printf(Chute7: %d Ladder7: %d\n, chutehit[7], ladderhit[7]); printf(Chute8: %d Ladder8: %d\n, chutehit[8], ladderhit[8]); printf(Chute9: %d\n, chutehit[9]); } int main() { printf(Welcome to the Chutes and Ladders simulation \n); printf(...\n); srand(1); //printf(How many games would you like me to run? __ ); //scanf(%i,totalgames); ///printf(\n You have chosen to run %i games... thank you! \n, totalgames); totalgames+=2; RunningTurnTotal=0.0; game=1; do{ turn=0; square=0; /** Reset game **/ do /** Begin game loop **/ { ++turn; RunningTurnTotal = RunningTurnTotal + 1; square = square + 1 + rand()%6; /** Spin and move **/ printf(square =%d \n,square); if (square == 1) {square=23; ++ladderhit[0];} /** Ladders? **/ if (square == 4) {square=14; ++ladderhit[1];} if (square == 9) {square=31; ++ladderhit[2];} if (square == 21) {square=42; ++ladderhit[3];} if (square == 28) {square=84; ++ladderhit[4];} if (square == 36) {square=44; ++ladderhit[5];} if (square == 51) {square=67; ++ladderhit[6];} if (square == 71) {square=91; ++ladderhit[7];} if (square == 80) {square=100;++ladderhit[8];}/// so when 80 comes raech to our goal exit if (square == 98) {square=78; ++chutehit[0];} /** Chutes ? **/ if (square == 95) {square=75; ++chutehit[1];} if (square == 93) {square=73; ++chutehit[2];} if (square == 87) {square=24; ++chutehit[3];} if (square == 62) {square=19; ++chutehit[4];} if (square == 64) {square=60; ++chutehit[5];} if (square == 56) {square=53; ++chutehit[6];} if (square == 49) {square=11; ++chutehit[7];} if (square == 48) {square=26; ++chutehit[8];} if (square == 16) {square=6; ++chutehit[9];} } while (square100);//terminate if random no. is 100 printf(\n\n Game over after %d turns\n, turn); printf(\nSimulation complete... beginning statistical analysis...\n\n); printf(Total number of games played: %d \n, game); printf(Total number of turns: %f \n, RunningTurnTotal); average = RunningTurnTotal / game; printf(Avg number of turns per game: %f \n, average); printf(\n); ChuteStats(); printf(\n); ++game; printf(\n\n Would you like to run the simulation again? (1=Yes)...); scanf(%i,reply); if(reply==1)//e.g. reply==1 totalgames+=1; else exit(0);// exit } while (gametotalgames); getch(); } -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algoge...@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
Re: [algogeeks] Re: Google Interview Question-Snake and Ladder Design
Hi On 14 September 2010 13:33, bittu shashank7andr...@gmail.com wrote: #includestdlib.h #includestdio.h #includemath.h #includeconio.h ///O(N^2) solution Does solution exits in O(n) or (nlogn)..? reply me sum1 git dis.. //i will post analysis of dsi program later int turn, square; long game, totalgames; int seed; int chutehit[10], ladderhit[9]; float RunningTurnTotal; float average; char reply; void ChuteStats() {printf(Chute and Ladder Statistics:\n\n); printf(Chute0: %d Ladder0: %d\n, chutehit[0], ladderhit[0]); printf(Chute1: %d Ladder1: %d\n, chutehit[1], ladderhit[1]); printf(Chute2: %d Ladder2: %d\n, chutehit[2], ladderhit[2]); printf(Chute3: %d Ladder3: %d\n, chutehit[3], ladderhit[3]); printf(Chute4: %d Ladder4: %d\n, chutehit[4], ladderhit[4]); printf(Chute5: %d Ladder5: %d\n, chutehit[5], ladderhit[5]); printf(Chute6: %d Ladder6: %d\n, chutehit[6], ladderhit[6]); printf(Chute7: %d Ladder7: %d\n, chutehit[7], ladderhit[7]); printf(Chute8: %d Ladder8: %d\n, chutehit[8], ladderhit[8]); printf(Chute9: %d\n, chutehit[9]); } int main() { printf(Welcome to the Chutes and Ladders simulation \n); printf(...\n); srand(1); //printf(How many games would you like me to run? __ ); //scanf(%i,totalgames); ///printf(\n You have chosen to run %i games... thank you! \n, totalgames); totalgames+=2; RunningTurnTotal=0.0; game=1; do{ turn=0; square=0; /** Reset game **/ do /** Begin game loop **/ { ++turn; RunningTurnTotal = RunningTurnTotal + 1; square = square + 1 + rand()%6; /** Spin and move **/ printf(square =%d \n,square); if (square == 1) {square=23; ++ladderhit[0];} /** Ladders? **/ if (square == 4) {square=14; ++ladderhit[1];} if (square == 9) {square=31; ++ladderhit[2];} if (square == 21) {square=42; ++ladderhit[3];} if (square == 28) {square=84; ++ladderhit[4];} if (square == 36) {square=44; ++ladderhit[5];} if (square == 51) {square=67; ++ladderhit[6];} if (square == 71) {square=91; ++ladderhit[7];} if (square == 80) {square=100;++ladderhit[8];}/// so when 80 comes raech to our goal exit if (square == 98) {square=78; ++chutehit[0];} /** Chutes ? **/ if (square == 95) {square=75; ++chutehit[1];} if (square == 93) {square=73; ++chutehit[2];} if (square == 87) {square=24; ++chutehit[3];} if (square == 62) {square=19; ++chutehit[4];} if (square == 64) {square=60; ++chutehit[5];} if (square == 56) {square=53; ++chutehit[6];} if (square == 49) {square=11; ++chutehit[7];} if (square == 48) {square=26; ++chutehit[8];} if (square == 16) {square=6; ++chutehit[9];} } while (square100);//terminate if random no. is 100 printf(\n\n Game over after %d turns\n, turn); printf(\nSimulation complete... beginning statistical analysis...\n\n); printf(Total number of games played: %d \n, game); printf(Total number of turns: %f \n, RunningTurnTotal); average = RunningTurnTotal / game; printf(Avg number of turns per game: %f \n, average); printf(\n); ChuteStats(); printf(\n); ++game; printf(\n\n Would you like to run the simulation again? (1=Yes)...); scanf(%i,reply); if(reply==1)//e.g. reply==1 totalgames+=1; else exit(0);// exit } while (gametotalgames); getch(); } -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algoge...@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.comalgogeeks%2bunsubscr...@googlegroups.com . For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en. Can you please write an algo for your program ? -- Siddharth Srivastava When you have learned to snatch the error code from the trap frame, it will be time for you to leave. -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algoge...@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
[algogeeks] Re: Google Interview Question-Snake and Ladder Design
And please stop posting the same thing twice. It's been happening for the past couple of days at least. @Question: I think you can use graphs and flood fill algo for this. Every possible move can be represented with an edge. Flood fill will help you figure out possible moves from you current position. What do you think? On Sep 14, 2:51 am, ankur aggarwal ankur.mast@gmail.com wrote: @bittuu write your algo then code On Tue, Sep 14, 2010 at 1:36 PM, bittu shashank7andr...@gmail.com wrote: #includestdlib.h #includestdio.h #includemath.h #includeconio.h int turn, square; long game, totalgames; int seed; int chutehit[10], ladderhit[9]; float RunningTurnTotal; float average; char reply; void ChuteStats() {printf(Chute and Ladder Statistics:\n\n); printf(Chute0: %d Ladder0: %d\n, chutehit[0], ladderhit[0]); printf(Chute1: %d Ladder1: %d\n, chutehit[1], ladderhit[1]); printf(Chute2: %d Ladder2: %d\n, chutehit[2], ladderhit[2]); printf(Chute3: %d Ladder3: %d\n, chutehit[3], ladderhit[3]); printf(Chute4: %d Ladder4: %d\n, chutehit[4], ladderhit[4]); printf(Chute5: %d Ladder5: %d\n, chutehit[5], ladderhit[5]); printf(Chute6: %d Ladder6: %d\n, chutehit[6], ladderhit[6]); printf(Chute7: %d Ladder7: %d\n, chutehit[7], ladderhit[7]); printf(Chute8: %d Ladder8: %d\n, chutehit[8], ladderhit[8]); printf(Chute9: %d \n, chutehit[9]); } int main() { printf(Welcome to the Chutes and Ladders simulation \n); printf(...\n); srand(1); //printf(How many games would you like me to run? __ ); //scanf(%i,totalgames); ///printf(\n You have chosen to run %i games... thank you! \n, totalgames); totalgames+=2; RunningTurnTotal=0.0; game=1; do{ turn=0; square=0; /** Reset game **/ do /** Begin game loop **/ { ++turn; RunningTurnTotal = RunningTurnTotal + 1; square = square + 1 + rand()%6; /** Spin and move **/ printf(square =%d \n,square); if (square == 1) {square=23; ++ladderhit[0];} /** Ladders? **/ if (square == 4) {square=14; ++ladderhit[1];} if (square == 9) {square=31; ++ladderhit[2];} if (square == 21) {square=42; ++ladderhit[3];} if (square == 28) {square=84; ++ladderhit[4];} if (square == 36) {square=44; ++ladderhit[5];} if (square == 51) {square=67; ++ladderhit[6];} if (square == 71) {square=91; ++ladderhit[7];} if (square == 80) {square=100;++ladderhit[8];}/// so when 80 comes raech to our goal exit if (square == 98) {square=78; ++chutehit[0];} /** Chutes ? **/ if (square == 95) {square=75; ++chutehit[1];} if (square == 93) {square=73; ++chutehit[2];} if (square == 87) {square=24; ++chutehit[3];} if (square == 62) {square=19; ++chutehit[4];} if (square == 64) {square=60; ++chutehit[5];} if (square == 56) {square=53; ++chutehit[6];} if (square == 49) {square=11; ++chutehit[7];} if (square == 48) {square=26; ++chutehit[8];} if (square == 16) {square=6; ++chutehit[9];} } while (square100);//terminate if random no. is 100 printf(\n\n Game over after %d turns\n, turn); printf(\nSimulation complete... beginning statistical analysis...\n\n); printf(Total number of games played: %d \n, game); printf(Total number of turns: %f \n, RunningTurnTotal); average = RunningTurnTotal / game; printf(Avg number of turns per game: %f \n, average); printf(\n); ChuteStats(); printf(\n); ++game; printf(\n\n Would you like to run the simulation again? (1=Yes)...); scanf(%i,reply); if(reply==1)//e.g. reply==1 totalgames+=1; else exit(0);// exit } while (gametotalgames); getch(); } ///O(N^2) solution Does solution exits in O(n) or (nlogn)..? reply me sum1 git dis.. //i will post analysis of dsi program later i m solving usig OOPS (Java) to represnt everything as object //right me if i m wrong or hw we can improve dis alog. Regards Shashank Mani Don't Be Evil u Can Earn While U Learn BIT Mesra-2010 09166674831 -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algoge...@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.comalgogeeks%2bunsubscr...@googlegroups .com . For more options, visit this group at