-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0516043_hw4.c
71 lines (65 loc) · 1.04 KB
/
0516043_hw4.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
int main()
{
while(1)
{
int m=1,n=1,i=0,j=0;
printf("Please input two numbers:\n");
scanf("%d%d",&m,&n);
char a[m][n];
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
a[i][j]='.';
}
}
srand(time(NULL));
int letter= 65;
int cx=0,cy=0;
a[cx][cy]=letter;
while(letter<90)
{
int x= rand()%2;
int y= rand()%2;
if(x==0&&y==0&&a[cx][cy-1]=='.'&&cy-1>=0)
{
cy--;
letter++;
a[cx][cy]=letter;
}
if(x==1&&y==0&&a[cx+1][cy]=='.'&&cy+1<=m)
{cx++;
letter++;
a[cx][cy]=letter;
}
if(x==0&&y==1&&a[cx-1][cy]=='.'&&cx-1>=0)
{cx--;
letter++;
a[cx][cy]=letter;
}
if(x==1&&y==1&&a[cx][cy+1]=='.'&&cy-1<=n)
{cy++;
letter++;
a[cx][cy]=letter;
}
else if((a[cx][cy+1]!='.'||cy+1>n)&&(a[cx+1][cy]!='.'||cx+1>m)&&(a[cx-1][cy+1]!='.'||cx-1<0)&&(a[cx][cy-1]!='.'||cy-1<0))
{
printf("Trapped!\n");
break;
}
}
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
printf("%c",a[i][j]);
}
printf("\n");
}
}
system("pause");
return(0);
}