所以我想使用C语言中的2D数组来表示尺寸为5x4(行x列)的矩形迷宫。但是,我无法确定实际需要放入2D阵列的内容。
int a[5][4] = {
{},
{},
{},
{},
{},
};
这是2D数组的骨架,每行会有4个值,我假设这些值中的每一个都是一个整数,告诉我们迷宫中单元格的属性。我的问题是,这真的够了吗?单个值如何告诉机器人天气有3面墙,2面墙等
有人请赐教D:答案 0 :(得分:7)
为房间的特定属性使用特定位
#define ROOM_WALL_ABOVE (1 << 0)
#define ROOM_WALL_LEFT (1 << 1)
#define ROOM_WALL_BELOW (1 << 2)
#define ROOM_WALL_RIGHT (1 << 3)
#define ROOM_DOOR (1 << 4)
int a[5][4] = {0};
a[0][0] = ROOM_WALL_ABOVE | ROOM_WALL_LEFT;
if (a[x][y] & ROOM_WALL_RIGHT) printf("Cannot walk right.\n");
答案 1 :(得分:3)
您可以使用结构矩阵
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
struct walls
{
bool N; // true = wall false = no wall
bool S; // true = wall false = no wall
bool W; // true = wall false = no wall
bool E; // true = wall false = no wall
};
int main()
{
struct walls maze[5][4];
// reset
memset(maze, 0x00, sizeof(maze));
// init
maze[0][0].N = false;
maze[0][0].S = true;
maze[0][0].W = true;
maze[0][0].E = false;
// YOUR STUFF
return 0;
}