请帮忙看看书上一道习题
这是题目。
8.9.c.zip
(1.37 KB)
这是我写的代码,我这几天反反复复检查了十几遍,愣是没查出来错误在哪里。
请各位大神帮我看一看。
程序代码:
#include <stdio.h> #include <time.h> #include <stdlib.h> #include <stdbool.h> #define N 10 #define CURRENTLETTER ch[x][y] #define NEXTLETTER ch[x][y]+1 #define FILLER '.' /********************************** * used to judge whether the * * place is legal to put a letter * **********************************/ bool legal(int x, int y){ char ch[x][y]; if( ch[x][y]==FILLER && x>0 && x<=10 && y>0 && y<=10) return true; else return false; } int main(){ char ch[N][N]={0}; int x, y, movesTried = 0; for(x=1;x<=N;x++) for(y=1;y<=N;y++) ch[x][y]=FILLER; srand((unsigned)time(NULL)); x=1; y=1; CURRENTLETTER='A'; while(CURRENTLETTER < 'Z' && movesTried < 4){ switch(rand()%4){ case 0: if(legal(x+1,y)){ x++ ; CURRENTLETTER = NEXTLETTER ; movesTried = 0; }else movesTried++; break; case 1: if(legal(x-1,y)){ x-- ; CURRENTLETTER = NEXTLETTER ; movesTried = 0; }else movesTried++; break; case 2: if(legal(x,y-1)){ y-- ; CURRENTLETTER = NEXTLETTER ; movesTried = 0; }else movesTried++; break; case 3: if(legal(x,y+1)){ y++; CURRENTLETTER = NEXTLETTER ; movesTried = 0; }else movesTried++; break; } } for(x=1;x<=N;x++){ for(y=1;y<=N;y++){ printf("%c ",CURRENTLETTER); } printf("\n"); } return 0; }
[ 本帖最后由 姜倔倔 于 2013-11-20 18:00 编辑 ]