真想不出来了,卡在这道题好几天了,下面是题目,能不能帮我看看,感激不尽
题目:
下⾯是⼀个输出示例:
A . . . . . . . . .
B C D . . . . . . .
. F E . . . . . . .
H G . . . . . . . .
I . . . . . . . . .
J . . . . . . . Z .
K . . R S T U V Y .
L M P Q . . . W X .
. N O . . . . . . .
利⽤srand函数和rand函数(⻅程序deal.c)产⽣随机数,然后查看次数除以4的余数。余数⼀共有4种可能的值(0、1、2和3),指示下⼀次移动的4种可能⽅向。在执⾏移动之前,需要检查两项内容:⼀是不能⾛到数组外⾯,⼆是不能⾛到已有字⺟标记的位置。只要⼀个条件不满⾜,就得尝试换⼀个⽅向移动。如果4个⽅向都堵住了,程序就必须终⽌了。下⾯是提前结束的⼀个示例:
A B G H I . . . . .
. C F . J K . . . .
. D E . M L . . . .
. . W X Y P Q . . .
. . V U T S R . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
因为Y的4个⽅向都堵住了,所以没有地⽅可以放置下⼀步的Z了。
//下面是我写的代码,程序输出时只显示AB为什么呢
#include<stdio.h>
#include<stdlib.h>
#include<time.h>//rand,srand
int random(int x,int y){
srand((unsigned) (time(NULL)));
x=rand();
y=x%4;
return y;
}
int main(){
char a[10][10];
char *p;
p=&a[0][0];
for(;p<&a[10][10];p++){
*p='.';
}
int x1,x2,i,j;
srand((unsigned) (time(NULL)));
x1=rand();i=x1%4;
x2=rand();j=x2%4;
char *t;
t=&a[i][j];
*t='A';//for
int x,y,n1,q1;
int z=random(x,y);
int k=0;
while(k<25){
if(z==0){//up i-1,向上移动
if(i>0&&a[i-1][j]=='.'){
t=&a[i-1][j];
*t=(char)(k+66);
k++;
break;
}else if(i==0||a[i-1][j]!='.'){
z=random(n1,q1);
break;
}
break;
}
else if(z==1){//down i+1,向下移动
if(i<9&&a[i+1][j]=='.'){
t=&a[i-1][j];
*t=(char)(k+66);
k++;
break;
}else if(i==9&&a[i+1][j]!='.'){
z=random(n1,q1);
break;
}
break;
}
else if(z==2){//left j-1 左移
if(j>0&&a[i][j-1]=='.'){
t=&a[i][j-1];
*t=(char)(k+66);
k++;
break;
}else if(j==0&&a[i][j-1]!='.'){
z=random(n1,q1);
break;
}
break;
}
else if(z==3){//right j+1 右移
if(j<9&&a[i][j+1]=='.'){
t=&a[i][j+1];
*t=(char)(k+66);
k++;
break;
}else if(j==9&&a[i][j+1]!='.'){
z=random(n1,q1);
break;
}
break;
}
}
int i1,j1;//打印所有字符
for (i1=0;i1<10;i1++){
for(j1=0;j1<9;j1++){
printf("%c",a[i1][j1]);
}while(j1==9&&i1!=9){
printf("%c\n",a[i1][9]);
break;
}if (i1==9&&j1==9){
printf("%c",a[9][9]);
}
}
return 0;
}
被卡了好几天了,希望大家能帮我看看