- 论坛徽章:
- 324
|
题目有点没说清楚,按我的理解写了一下- #define SIZE 10
- #define BLANK '.'
- char map[SIZE][SIZE];
- int x, y;
- bool Up()
- {
- if (y > 0 && map[y-1][x] == BLANK)
- {
- --y;
- return true;
- }
- return false;
- }
- bool Right()
- {
- if (x < SIZE - 1 && map[y][x+1] == BLANK)
- {
- ++x;
- return true;
- }
- return false;
- }
- bool Down()
- {
- if (y < SIZE - 1 && map[y+1][x] == BLANK)
- {
- ++y;
- return true;
- }
- return false;
- }
- bool Left()
- {
- if (x > 0 && map[y][x-1] == BLANK)
- {
- --x;
- return true;
- }
- return false;
- }
- int main()
- {
- typedef bool (*LPFN_ACTION)();
- LPFN_ACTION actions[7] = {Up, Right, Down, Left, Up, Right, Down};
- for (y = 0; y < SIZE; ++y)
- for (x = 0; x < SIZE; ++x)
- map[y][x] = BLANK;
- x = 0;
- y = 0;
- char c = 'A';
- map[y][x] = c;
- srand((unsigned)time(NULL));
- do
- {
- int v = rand() % 4;
- if ((*actions[v])() || (*actions[v+1])() || (*actions[v+2])() || (*actions[v+3])())
- {
- map[y][x] = ++c;
- }
- else
- {
- break;
- }
- }
- while (c < 'Z');
- for (y = 0; y < SIZE; ++y)
- {
- for (x = 0; x < SIZE; ++x)
- putchar(map[y][x]);
- putchar('\n');
- }
- return 0;
- }
复制代码 |
|