看板 cksh75th09 關於我們 聯絡資訊
※ 引述《mmx (瑞)》之銘言: : ※ 引述《berserk (鬍子)》之銘言: : : 我作業寫不出來啊 : : 事關畢業麻煩會的人幫一下吧=_= : There is a possibility that this may take quite some time, so the : pargram will prompt for the maximum number of steps the user wants : the robot to take. : 這一段話是啥意思?是說走完要告訴user總共走幾步嗎? 是說robot可能永遠走不出來...或走很久... 所以要設定步數的最大值 (由使用者設定) 超過此值則程式自動中斷 : //Random Walk Of A Robot : #include <iostream.h> : #include <stdlib.h> : int cur_row=7; //robot位置記錄用,初始位置為(7,0),出口在(4,19) : int cur_col=0; : int steps=0; //記錄總共走了幾步 : void move_up(char a[][20]); : void move_down(char a[][20]); : void move_left(char a[][20]); : void move_right(char a[][20]); : void main(){ : int direction; : char room[10][20]={'#','#',.....}; //設定房間 起始點設'r' : randomize(); : while(!(cur_row==4 && cur_col==19)){ : direction=rand()%4; : switch(direction){ : case 0 : move_up(room);break; : case 1 : move_down(room);break; : case 2 : move_left(room);break; : case 3 : move_right(room);break; : } : } : cout<< "\n 親愛的鬍子主人,我走出來了,共走了"<<steps<<"步 \n"; : for(int i=0;i<10;i++){ : cout<<"\n"; : for(int j=0;j<20;j++) : cout<<room[i][j]; : } : } : void move_up(char a[][20]){ : if(a[cur_row-1][cur_col]!='#'){ : a[cur_row][cur_col]='*'; : cur_row--; : a[cur_row][cur_col]='r'; : steps++; : } : } : void move_down(char a[][20]){ : if(a[cur_row+1][cur_col]!='#'){ : a[cur_row][cur_col]='*'; : cur_row++; : a[cur_row][cur_col]='r'; : steps++; : } : } : void move_left(char a[][20]){ : if(a[cur_row][cur_col-1]!='#' && cur_col-1 !=0){ : a[cur_row][cur_col]='*'; : cur_col--; : a[cur_row][cur_col]='r'; : steps++; : } : } : void move_right(char a[][20]){ : if(a[cur_row][cur_col+1]!='#'){ : a[cur_row][cur_col]='*'; : cur_col++; : a[cur_row][cur_col]='r'; : steps++; : } : } -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 211.74.98.63