fork download
  1. #include <stdio.h>
  2.  
  3. #define N 6
  4. #define ROWS 6
  5. #define COLS 6
  6. #define data_in_save 100
  7. #define MAX_COMMANDS 100
  8.  
  9. int count_data_save = 0;
  10. int start_end[ROWS][COLS] = {
  11. {0, 0, 1, 0, 0, 2}, // 0
  12. {0, 0, 0, 0, 0, 0}, // 1
  13. {0, 0, 0, 0, 0, 0}, // 2
  14. {0, 0, 0, 0, 0, 0}, // 3
  15. {0, 0, 0, 0, 0, 0}, // 4
  16. {0, 0, 0, 0, 0, 0} // 5
  17. };
  18.  
  19. int m[N][N] = {
  20. { 1,1,1,1,1,1 }, //0
  21. { 0,1,0,0,1,1 }, //1
  22. { 0,1,1,1,1,0 }, //2
  23. { 0,0,0,0,1,1 }, //3
  24. { 0,1,1,1,1,0 }, //4
  25. { 1,0,0,0,1,1 } //5
  26. };
  27.  
  28. char data_save_in[ROWS][COLS][data_in_save];
  29. int data_go_in[ROWS][COLS][data_in_save];
  30. char movement_commands[MAX_COMMANDS][4];
  31. int command_index = 0;
  32.  
  33. // ฟังก์ชันตรวจสอบความปลอดภัย
  34. int issafe(int m[N][N], int x, int y) {
  35. if (x < 0 || x >= N) return 0;
  36. if (y < 0 || y >= N) return 0;
  37. return (m[x][y] == 1);
  38. }
  39.  
  40. // ฟังก์ชันแสดงผลทางออก
  41. void print_solution(int m[N][N], char direction_grid[N][N], int data_go_in_current[N][COLS]) {
  42. static int nsol = 0;
  43. printf("Solution %d\n\n", ++nsol);
  44.  
  45. for (int i = 0; i < N; i++) {
  46. for (int j = 0; j < N; j++) {
  47. if (direction_grid[i][j] != ' ') {
  48. printf("%c ", direction_grid[i][j]);
  49. } else {
  50. printf("%d ", m[i][j]);
  51. }
  52. if (count_data_save < data_in_save) {
  53. data_save_in[i][j][count_data_save] = direction_grid[i][j];
  54. data_go_in[i][j][count_data_save] = data_go_in_current[i][j];
  55. }
  56. }
  57. printf("\n");
  58. }
  59. printf("\n");
  60. count_data_save++;
  61. }
  62.  
  63. // ฟังก์ชันค้นหาเส้นทาง
  64. int maze(int m[N][N], int x, int y, int end_x, int end_y, char direction_grid[N][N], int data_go_in_current[N][COLS], int step, char prev_direction) {
  65. int nsol = 0;
  66.  
  67. if (issafe(m, x, y)) {
  68. m[x][y] = 2;
  69. direction_grid[x][y] = prev_direction;
  70. data_go_in_current[x][y] = step;
  71.  
  72. if (x == end_x && y == end_y) {
  73. print_solution(m, direction_grid, data_go_in_current);
  74. nsol = 1;
  75. } else {
  76. nsol += maze(m, x, y + 1, end_x, end_y, direction_grid, data_go_in_current, step + 1, '>');
  77. nsol += maze(m, x, y - 1, end_x, end_y, direction_grid, data_go_in_current, step + 1, '<');
  78. nsol += maze(m, x + 1, y, end_x, end_y, direction_grid, data_go_in_current, step + 1, 'v');
  79. nsol += maze(m, x - 1, y, end_x, end_y, direction_grid, data_go_in_current, step + 1, '^');
  80. }
  81.  
  82. m[x][y] = 1;
  83. direction_grid[x][y] = ' ';
  84. data_go_in_current[x][y] = 0;
  85. }
  86.  
  87. return nsol;
  88. }
  89.  
  90. // ฟังก์ชันสร้างคำสั่งการเคลื่อนที่
  91. void generate_movement_commands(int solution) {
  92. int dx[] = {0, 1, 0, -1};
  93. int dy[] = {1, 0, -1, 0};
  94. int prev_direction = -1;
  95. command_index = 0;
  96.  
  97. int x = -1, y = -1;
  98. for (int i = 0; i < N; i++) {
  99. for (int j = 0; j < N; j++) {
  100. if (data_go_in[i][j][solution] == 1) {
  101. x = i;
  102. y = j;
  103. break;
  104. }
  105. }
  106. if (x != -1) break;
  107. }
  108.  
  109. while (1) {
  110. int current_step = data_go_in[x][y][solution];
  111. int next_step = 0;
  112. int next_x = -1, next_y = -1;
  113.  
  114. for (int d = 0; d < 4; d++) {
  115. int nx = x + dx[d];
  116. int ny = y + dy[d];
  117.  
  118. if (nx >= 0 && ny >= 0 && nx < N && ny < N && data_go_in[nx][ny][solution] == current_step + 1) {
  119. next_step = data_go_in[nx][ny][solution];
  120. next_x = nx;
  121. next_y = ny;
  122.  
  123. if (prev_direction == -1 || d == prev_direction) {
  124. snprintf(movement_commands[command_index++], sizeof(movement_commands[command_index]), "F");
  125. } else if ((prev_direction == 0 && d == 3) || (prev_direction == 1 && d == 0) ||
  126. (prev_direction == 2 && d == 1) || (prev_direction == 3 && d == 2)) {
  127. snprintf(movement_commands[command_index++], sizeof(movement_commands[command_index]), "L F");
  128. } else {
  129. snprintf(movement_commands[command_index++], sizeof(movement_commands[command_index]), "R F");
  130. }
  131.  
  132. prev_direction = d;
  133. break;
  134. }
  135. }
  136.  
  137. if (next_step == 0) break;
  138.  
  139. x = next_x;
  140. y = next_y;
  141. }
  142. }
  143.  
  144. // ฟังก์ชันคำนวณคะแนน
  145. int calculate_score() {
  146. int score = 0;
  147.  
  148. for (int i = 0; i < command_index; i++) {
  149. if (movement_commands[i][0] == 'F') {
  150. score += 1;
  151. } else if (movement_commands[i][0] == 'L' || movement_commands[i][0] == 'R') {
  152. score += 2;
  153. }
  154. }
  155.  
  156. return score;
  157. }
  158.  
  159. int scores[100]; // ตัวแปรเก็บคะแนนของแต่ละ solution
  160.  
  161. // ฟังก์ชันแสดงข้อมูลทั้งหมด รวมถึงเส้นทางที่ดีที่สุด
  162. void show_data_get() {
  163. int min_score = 99999; // เก็บคะแนนต่ำสุด
  164. int best_solution = -1; // เก็บ solution ที่ดีที่สุด
  165.  
  166. printf("Data saved:\n");
  167. for (int sol = 0; sol < count_data_save; sol++) {
  168. printf("Solution %d:\n", sol + 1);
  169. printf("Directions:\n");
  170. for (int i = 0; i < ROWS; i++) {
  171. for (int j = 0; j < COLS; j++) {
  172. printf("%c ", data_save_in[i][j][sol]);
  173. }
  174. printf("\n");
  175. }
  176. printf("Step Numbers:\n");
  177. for (int i = 0; i < ROWS; i++) {
  178. for (int j = 0; j < COLS; j++) {
  179. printf("%d ", data_go_in[i][j][sol]);
  180. }
  181. printf("\n");
  182. }
  183.  
  184. // สร้างและแสดงคำสั่งการเคลื่อนที่
  185. generate_movement_commands(sol);
  186. printf("Movement Commands: ");
  187. for (int i = 0; i < command_index; i++) {
  188. printf("%s ", movement_commands[i]);
  189. }
  190. printf("\n");
  191.  
  192. // คำนวณและแสดงคะแนน
  193. int score = calculate_score();
  194. scores[sol] = score; // บันทึกคะแนนของแต่ละ solution
  195. printf("Score: %d\n\n", score);
  196.  
  197. // ตรวจสอบหา solution ที่มีคะแนนต่ำสุด
  198. if (score < min_score) {
  199. min_score = score;
  200. best_solution = sol;
  201. }
  202. }
  203.  
  204. // แสดง solution ที่ดีที่สุด
  205. printf("Best Solution is Solution %d with the lowest score of %d:\n", best_solution + 1, min_score);
  206.  
  207. // แสดง Movement Commands ของทางเลือกที่ดีที่สุด
  208. printf("Best Movement Commands: ");
  209. generate_movement_commands(best_solution);
  210. for (int i = 0; i < command_index; i++) {
  211. printf("%s ", movement_commands[i]);
  212. }
  213. printf("\n");
  214.  
  215. // แสดงเส้นทางที่ดีที่สุด
  216. printf("Best Solution Path:\n");
  217.  
  218.  
  219. // แสดง Step Numbers ของทางเลือกที่ดีที่สุด
  220. printf("Step Numbers:\n");
  221. for (int i = 0; i < ROWS; i++) {
  222. for (int j = 0; j < COLS; j++) {
  223. printf("%d ", data_go_in[i][j][best_solution]);
  224. }
  225. printf("\n");
  226. }
  227. printf("Directions:\n");
  228. for (int i = 0; i < ROWS; i++) {
  229. for (int j = 0; j < COLS; j++) {
  230. printf("%c ", data_save_in[i][j][best_solution]);
  231. }
  232. printf("\n");
  233. }
  234. printf("\n\n");
  235. }
  236.  
  237. // ฟังก์ชันหลัก
  238. void Program_run() {
  239. int nsol;
  240. int start_x = -1, start_y = -1, end_x = -1, end_y = -1;
  241.  
  242. // ค้นหาจุดเริ่มต้นและจุดสิ้นสุด
  243. for (int i = 0; i < ROWS; i++) {
  244. for (int j = 0; j < COLS; j++) {
  245. if (start_end[i][j] == 1) {
  246. start_x = i;
  247. start_y = j;
  248. } else if (start_end[i][j] == 2) {
  249. end_x = i;
  250. end_y = j;
  251. }
  252. }
  253. }
  254.  
  255. // ตรวจสอบว่ามีจุดเริ่มต้นและจุดสิ้นสุด
  256. if (start_x == -1 || start_y == -1 || end_x == -1 || end_y == -1) {
  257. printf("Error: No start or end point defined in start_end array.\n");
  258. return;
  259. }
  260.  
  261. char direction_grid[N][N];
  262. for (int i = 0; i < N; i++) {
  263. for (int j = 0; j < N; j++) {
  264. direction_grid[i][j] = ' ';
  265. }
  266. }
  267.  
  268. int data_go_in_current[N][COLS];
  269. for (int i = 0; i < N; i++) {
  270. for (int j = 0; j < COLS; j++) {
  271. data_go_in_current[i][j] = 0;
  272. }
  273. }
  274.  
  275. nsol = maze(m, start_x, start_y, end_x, end_y, direction_grid, data_go_in_current, 1, 'S');
  276. printf("%d solutions.\n", nsol);
  277. }
  278.  
  279. int main() {
  280. Program_run();
  281. printf("Count data saved: %d\n", count_data_save);
  282. show_data_get();
  283. return 0;
  284. }
  285.  
Success #stdin #stdout 0.02s 26096KB
stdin
Standard input is empty
stdout
#include <stdio.h>

#define N 6
#define ROWS 6
#define COLS 6
#define data_in_save 100
#define MAX_COMMANDS 100

int count_data_save = 0;
int start_end[ROWS][COLS] = {
    {0, 0, 1, 0, 0, 2}, // 0
    {0, 0, 0, 0, 0, 0}, // 1
    {0, 0, 0, 0, 0, 0}, // 2
    {0, 0, 0, 0, 0, 0}, // 3
    {0, 0, 0, 0, 0, 0}, // 4
    {0, 0, 0, 0, 0, 0}  // 5
};

int m[N][N] = {
    { 1,1,1,1,1,1 }, //0
    { 0,1,0,0,1,1 }, //1
    { 0,1,1,1,1,0 }, //2
    { 0,0,0,0,1,1 }, //3
    { 0,1,1,1,1,0 }, //4
    { 1,0,0,0,1,1 }  //5
};

char data_save_in[ROWS][COLS][data_in_save];  
int data_go_in[ROWS][COLS][data_in_save];    
char movement_commands[MAX_COMMANDS][4];     
int command_index = 0;

// ฟังก์ชันตรวจสอบความปลอดภัย
int issafe(int m[N][N], int x, int y) {
    if (x < 0 || x >= N) return 0;
    if (y < 0 || y >= N) return 0;
    return (m[x][y] == 1);
}

// ฟังก์ชันแสดงผลทางออก
void print_solution(int m[N][N], char direction_grid[N][N], int data_go_in_current[N][COLS]) {
    static int nsol = 0;
    printf("Solution %d\n\n", ++nsol);
    
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
            if (direction_grid[i][j] != ' ') {
                printf("%c ", direction_grid[i][j]);
            } else {
                printf("%d ", m[i][j]);
            }
            if (count_data_save < data_in_save) {
                data_save_in[i][j][count_data_save] = direction_grid[i][j];
                data_go_in[i][j][count_data_save] = data_go_in_current[i][j];
            }
        }
        printf("\n");
    }
    printf("\n");
    count_data_save++;
}

// ฟังก์ชันค้นหาเส้นทาง
int maze(int m[N][N], int x, int y, int end_x, int end_y, char direction_grid[N][N], int data_go_in_current[N][COLS], int step, char prev_direction) {
    int nsol = 0;

    if (issafe(m, x, y)) {
        m[x][y] = 2;  
        direction_grid[x][y] = prev_direction;  
        data_go_in_current[x][y] = step;        

        if (x == end_x && y == end_y) {
            print_solution(m, direction_grid, data_go_in_current);
            nsol = 1;
        } else {
            nsol += maze(m, x, y + 1, end_x, end_y, direction_grid, data_go_in_current, step + 1, '>');  
            nsol += maze(m, x, y - 1, end_x, end_y, direction_grid, data_go_in_current, step + 1, '<');  
            nsol += maze(m, x + 1, y, end_x, end_y, direction_grid, data_go_in_current, step + 1, 'v');  
            nsol += maze(m, x - 1, y, end_x, end_y, direction_grid, data_go_in_current, step + 1, '^');  
        }

        m[x][y] = 1;          
        direction_grid[x][y] = ' ';  
        data_go_in_current[x][y] = 0; 
    }

    return nsol;
}

// ฟังก์ชันสร้างคำสั่งการเคลื่อนที่
void generate_movement_commands(int solution) {
    int dx[] = {0, 1, 0, -1};  
    int dy[] = {1, 0, -1, 0};  
    int prev_direction = -1;    
    command_index = 0;          
    
    int x = -1, y = -1;
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
            if (data_go_in[i][j][solution] == 1) {  
                x = i;
                y = j;
                break;
            }
        }
        if (x != -1) break;
    }

    while (1) {
        int current_step = data_go_in[x][y][solution];
        int next_step = 0;
        int next_x = -1, next_y = -1;
        
        for (int d = 0; d < 4; d++) {
            int nx = x + dx[d];
            int ny = y + dy[d];
            
            if (nx >= 0 && ny >= 0 && nx < N && ny < N && data_go_in[nx][ny][solution] == current_step + 1) {
                next_step = data_go_in[nx][ny][solution];
                next_x = nx;
                next_y = ny;

                if (prev_direction == -1 || d == prev_direction) {
                    snprintf(movement_commands[command_index++], sizeof(movement_commands[command_index]), "F");
                } else if ((prev_direction == 0 && d == 3) || (prev_direction == 1 && d == 0) || 
                           (prev_direction == 2 && d == 1) || (prev_direction == 3 && d == 2)) {
                    snprintf(movement_commands[command_index++], sizeof(movement_commands[command_index]), "L F");
                } else {
                    snprintf(movement_commands[command_index++], sizeof(movement_commands[command_index]), "R F");
                }

                prev_direction = d;
                break;
            }
        }
        
        if (next_step == 0) break;
        
        x = next_x;
        y = next_y;
    }
}

// ฟังก์ชันคำนวณคะแนน
int calculate_score() {
    int score = 0;

    for (int i = 0; i < command_index; i++) {
        if (movement_commands[i][0] == 'F') {
            score += 1;
        } else if (movement_commands[i][0] == 'L' || movement_commands[i][0] == 'R') {
            score += 2;
        }
    }

    return score;
}

int scores[100];  // ตัวแปรเก็บคะแนนของแต่ละ solution

// ฟังก์ชันแสดงข้อมูลทั้งหมด รวมถึงเส้นทางที่ดีที่สุด
void show_data_get() {
    int min_score = 99999;  // เก็บคะแนนต่ำสุด
    int best_solution = -1;  // เก็บ solution ที่ดีที่สุด

    printf("Data saved:\n");
    for (int sol = 0; sol < count_data_save; sol++) {
        printf("Solution %d:\n", sol + 1);
        printf("Directions:\n");
        for (int i = 0; i < ROWS; i++) {
            for (int j = 0; j < COLS; j++) {
                printf("%c ", data_save_in[i][j][sol]);
            }
            printf("\n");
        }
        printf("Step Numbers:\n");
        for (int i = 0; i < ROWS; i++) {
            for (int j = 0; j < COLS; j++) {
                printf("%d ", data_go_in[i][j][sol]);
            }
            printf("\n");
        }

        // สร้างและแสดงคำสั่งการเคลื่อนที่
        generate_movement_commands(sol);
        printf("Movement Commands: ");
        for (int i = 0; i < command_index; i++) {
            printf("%s ", movement_commands[i]);
        }
        printf("\n");

        // คำนวณและแสดงคะแนน
        int score = calculate_score();
        scores[sol] = score;  // บันทึกคะแนนของแต่ละ solution
        printf("Score: %d\n\n", score);

        // ตรวจสอบหา solution ที่มีคะแนนต่ำสุด
        if (score < min_score) {
            min_score = score;
            best_solution = sol;
        }
    }

    // แสดง solution ที่ดีที่สุด
    printf("Best Solution is Solution %d with the lowest score of %d:\n", best_solution + 1, min_score);
    
    // แสดง Movement Commands ของทางเลือกที่ดีที่สุด
    printf("Best Movement Commands: ");
    generate_movement_commands(best_solution);
    for (int i = 0; i < command_index; i++) {
        printf("%s ", movement_commands[i]);
    }
    printf("\n");

    // แสดงเส้นทางที่ดีที่สุด
    printf("Best Solution Path:\n");


    // แสดง Step Numbers ของทางเลือกที่ดีที่สุด
    printf("Step Numbers:\n");
    for (int i = 0; i < ROWS; i++) {
        for (int j = 0; j < COLS; j++) {
            printf("%d ", data_go_in[i][j][best_solution]);
        }
        printf("\n");
    }
    printf("Directions:\n");
    for (int i = 0; i < ROWS; i++) {
        for (int j = 0; j < COLS; j++) {
            printf("%c ", data_save_in[i][j][best_solution]);
        }
        printf("\n");
    }
    printf("\n\n");
}

// ฟังก์ชันหลัก
void Program_run() {
    int nsol;
    int start_x = -1, start_y = -1, end_x = -1, end_y = -1;

    // ค้นหาจุดเริ่มต้นและจุดสิ้นสุด
    for (int i = 0; i < ROWS; i++) {
        for (int j = 0; j < COLS; j++) {
            if (start_end[i][j] == 1) {
                start_x = i;
                start_y = j;
            } else if (start_end[i][j] == 2) {
                end_x = i;
                end_y = j;
            }
        }
    }

    // ตรวจสอบว่ามีจุดเริ่มต้นและจุดสิ้นสุด
    if (start_x == -1 || start_y == -1 || end_x == -1 || end_y == -1) {
        printf("Error: No start or end point defined in start_end array.\n");
        return;
    }

    char direction_grid[N][N];
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
            direction_grid[i][j] = ' ';
        }
    }

    int data_go_in_current[N][COLS];
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < COLS; j++) {
            data_go_in_current[i][j] = 0;
        }
    }

    nsol = maze(m, start_x, start_y, end_x, end_y, direction_grid, data_go_in_current, 1, 'S');
    printf("%d solutions.\n", nsol);
}

int main() {
    Program_run();
    printf("Count data saved: %d\n", count_data_save);
    show_data_get();
    return 0;
}