-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDS_PE2-1.c
67 lines (56 loc) · 1.55 KB
/
DS_PE2-1.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
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(int argc, char *argv[]) {
FILE *out = fopen("D1150211_PE2.csv", "w+");
//初始化迷宮大小和起始位置
int n = atoi(argv[1]);
int m = atoi(argv[2]);
int starti = atoi(argv[3]);
int startj = atoi(argv[4]);
//初始化bug和移動方向
int bug[40][40] = {0};
int movei[8] = {-1, 0, 1, 1, 1, 0, -1, -1};
int movej[8] = {1, 1, 1, 0, -1, -1, -1, 0};
//初始化亂數生成器
srand(time(NULL));
//開始計算移動次數
int total = 0;
//初始也需要計數
bug[starti][startj] = 1;
total++;
//開始走
while (total < 50000) {
//生成隨機方向
int x = rand() % 8;
//計算新位置
int newi = starti + movei[x];
int newj = startj + movej[x];
//檢查是否在範圍內
if (newi >= 0 && newi < n && newj >= 0 && newj < m && bug[newi][newj] == 0) {
//更新bug的位置
starti = newi;
startj = newj;
//增加新儲存格的bug計數
bug[starti][startj]++;
total++;
}
else {
//都訪問過即結束
break;
}
}
//將結果寫入CSV文件
fprintf(out, "total moves = %d\n", total);
for (int i=0; i<n; i++){
for (int j=0; j<m; j++) {
fprintf(out, "%d", bug[i][j]);
if (j < m-1){
fprintf(out, ",");
}
}
fprintf(out, "\n");
}
fclose(out);
return 0;
}