Az alábbi letöltési lehetőségek közül választhatsz: (
segítség)
Típus: text/plain
Tartalmaz szöveget
Karakterkódolás: us-ascii
Méret: 1 KB
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int m, n;
char map[12][12];
#define EMPTY 0
#define WALL 1
#define WAY 2
#define RAND 4
#define FIELD(x) ((x) & 3)
void dump()
{
int i, j;
for (i = 0; i < m + 2; i++) {
for (j = 0; j < n + 2; j++)
switch (FIELD(map[i][j])) {
case WALL:
printf("#");
break;
case WAY:
printf(" ");
break;
case EMPTY:
printf(" ");
break;
}
printf("\n");
}
}
void fill(int x, int y)
{
if (FIELD(map[x][y]))
return;
map[x][y] |= WAY;
fill(x - 1, y);
fill(x + 1, y);
fill(x, y - 1);
fill(x, y + 1);
}
int route(int x1, int y1, int x2, int y2)
{
int i, j;
int t;
for (i = 0; i < m + 2; i++)
for (j = 0; j < n + 2; j++)
if (FIELD(map[i][j]) == WAY) {
t = map[i][j];
map[i][j] &= ~WAY;
if (t >= 4 && map[i][j] < 4)
exit(1);
}
fill(x1, y1);
return FIELD(map[x2][y2]) == WAY;
}
int left_unrand()
{
int i, j;
for (i = 0; i < m + 2; i++)
for (j = 0; j < n + 2; j++)
if ((i + j) % 2 == 1)
if ((map[i][j] & RAND) == 0)
return 1;
return 0;
}
int main(int argc, char **argv)
{
int i, j;
int x, y;
if (argc != 3) {
fprintf(stderr, "Usage: %s m n\n", argv[0]);
return 1;
}
m = atoi(argv[1]) * 2 - 1;
n = atoi(argv[2]) * 2 - 1;
srand(time(NULL));
for (j = 0; j < n + 2; j++) {
map[0][j] = RAND|WALL;
map[m + 1][j] = RAND|WALL;
}
for (i = 1; i < m + 1; i++) {
map[i][0] = RAND|WALL;
map[i][n + 1] = RAND|WALL;
for (j = 1; j < n + 1; j++)
if (i % 2 == 0 && j % 2 == 0)
map[i][j] = RAND|WALL;
else
map[i][j] = EMPTY;
}
while (left_unrand()) {
x = rand() % m + 1;
y = rand() % n + 1;
if (map[x][y] & RAND)
continue;
if (x % 2 == 0 && y % 2 == 1) {
map[x][y] = RAND|WALL;
if (!route(x - 1, y, x + 1, y))
map[x][y] = RAND|EMPTY;
} else if (x % 2 == 1 && y % 2 == 0) {
map[x][y] = RAND|WALL;
if (!route(x, y - 1, x, y + 1))
map[x][y] = RAND|EMPTY;
}
}
dump();
return 0;
}