- 论坛徽章:
- 2
|
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <time.h>
- #define DIMENSION_X 11
- #define DIMENSION_Y 10
- int print_array(int *array)
- {
- int x, y;
- printf(" ");
- for (x = 0; x < DIMENSION_X; x++) {
- printf("%3d", x + 1);
- }
- printf("\n");
- for (y = 0; y < DIMENSION_Y; y++) {
- printf("%3d", y + 1);
- for (x = 0; x < DIMENSION_X; x++) {
- if (array[y * DIMENSION_X + x] == 0) {
- printf(" %c", ' ');
- }else {
- printf(" %c", array[y * DIMENSION_X + x]);
- }
- }
- printf("\n");
- }
- return 0;
- }
- int get_array_space_count(int *array, int x, int y)
- {
- int n;
- if (x < 0) {
- x += DIMENSION_X;
- }else if (x >= DIMENSION_X) {
- x -= DIMENSION_X;
- }
- if (y < 0) {
- y += DIMENSION_Y;
- }else if (y >= DIMENSION_Y) {
- y -= DIMENSION_Y;
- }
- if (array[y * DIMENSION_X + x] == 0) {
- array[y * DIMENSION_X + x] = '*';
- n = get_array_space_count(array, x, y - 1) + get_array_space_count(array, x, y + 1) + get_array_space_count(array, x - 1, y) + get_array_space_count(array, x + 1, y) + 1;
- return n;
- }
- return 0;
- }
- int clear_array_indicator(int *array)
- {
- int x, y;
- for (y = 0; y < DIMENSION_Y; y++) {
- for (x = 0; x < DIMENSION_X; x++) {
- if (array[y * DIMENSION_X + x] == '*') {
- array[y * DIMENSION_X + x] = 0;
- }
- }
- }
- return 0;
- }
- int get_array_count(int *array, int x, int y)
- {
- int count;
- count = get_array_space_count(array, x, y);
- clear_array_indicator(array);
- return count;
- }
- int main(void)
- {
- int array[10][DIMENSION_X];
- char cc;
- int step;
- int x, y;
- memset(array, 0, sizeof(array));
- srand(time(NULL));
- x = rand() % DIMENSION_X;
- y = rand() % DIMENSION_Y;
- cc = 'A';
- while (cc <= 'Z') {
- step = rand() % 4;
- switch (step) {
- case 0: /* up */
- if (get_array_count(&array[0][0], x, y - 1) <= ('Z' - cc)) {
- continue;
- }else {
- -- y;
- break;
- }
- case 1: /* down */
- if (get_array_count(&array[0][0], x, y + 1) <= ('Z' - cc)) {
- continue;
- }else {
- ++ y;
- break;
- }
- case 2: /* left */
- if (get_array_count(&array[0][0], x - 1, y) <= ('Z' - cc)) {
- continue;
- }else {
- -- x;
- break;
- }
- case 3: /* right */
- if (get_array_count(&array[0][0], x + 1, y) <= ('Z' - cc)) {
- continue;
- }else {
- ++ x;
- break;
- }
- }
- if (x < 0) {
- x += DIMENSION_X;
- }else if (x >= DIMENSION_X) {
- x -= DIMENSION_X;
- }
- if (y < 0) {
- y += DIMENSION_Y;
- }else if (y >= DIMENSION_Y) {
- y -= DIMENSION_Y;
- }
- array[y][x] = cc;
- ++cc;
- }
- print_array(&array[0][0]);
- return -1;
- }
复制代码 |
|