void Snake_Grow(body Head, int* len, body* rear, int* button) { (*len)++; body q = (body)malloc(sizeof(BODY)); //将蛇增加的结点插入蛇头后面 q->next = Head->next; if (q->next != NULL) { q->next->prex = q; } Head->next = q; q->prex = Head; //插入新的结点坐标 q->x = Head->x; q->y = Head->y; Show_Snake(Head); switch (*button) { case 72: Head->y = Head->y - 20; break; case 80: Head->y = Head->y + 20; break; case 75: Head->x = Head->x - 20; break; case 77: Head->x = Head->x + 20; break; default: break; } if ((*rear) == NULL) //达到尾指针的值 *rear = q; } void Snake_move(int* button, int* len, body Head, body* rear, food Food) { //把原来的蛇隐藏 Hide_Snake(Head); //移动蛇身 body p = (*rear); while (p && p != Head) { p->x = p->prex->x; p->y = p->prex->y; p = p->prex; } //移动蛇头 switch (*button) { case 72: Head->y = Head->y - 20; break; case 80: Head->y = Head->y + 20; break; case 75: Head->x = Head->x - 20; break; case 77: Head->x = Head->x + 20; default: break; } if (Head->x + 10 >= Food->x && Head->x + 10 < Food->x + 20 && Head->y + 10 >= Food->y && Head->y + 10 < Food->y + 20) { //隐藏食物 Hide_Food(Food); //更新食物位置 Rnew_Food(Head, Food); //蛇变长 Snake_Grow(Head, len, rear, button);}为什么蛇的第二个结点图形没有显示
时间: 2024-03-24 16:38:08 浏览: 23
根据提供的代码,蛇的第二个结点图形没有显示的原因可能是因为在Snake_move函数中,移动蛇身时没有将第二个结点的图形更新。
在while循环中,p指向的是蛇尾,而不是蛇头。因此,在移动蛇身时,需要将p指向的结点的图形更新为它前一个结点的图形。
可以在while循环中加入以下代码:
```
while (p && p != Head) {
p->x = p->prex->x;
p->y = p->prex->y;
Show_Body(p); // 显示每一个结点的图形
p = p->prex;
}
```
在每次移动一个结点的坐标之后,调用Show_Body函数显示这个结点的图形。
这样就可以保证每个蛇结点的图形都能够被正确地显示出来了。
相关问题
void BFS(ALGraph *G,int i);
好的,这是一个关于图的广度优先遍历的问题,需要实现一个函数BFS来进行广度优先遍历。以下是一段可能的C++代码实现:
```
struct QueueNode {
int data;
QueueNode* next;
};
struct Queue {
QueueNode* front;
QueueNode* rear;
};
void InitQueue(Queue* Q) {
Q->front = Q->rear = new QueueNode;
Q->front->next = nullptr;
}
void EnQueue(Queue* Q, int data) {
QueueNode* node = new QueueNode;
node->data = data;
node->next = nullptr;
Q->rear->next = node;
Q->rear = node;
}
int DeQueue(Queue* Q) {
if (Q->front == Q->rear) {
return -1;
}
QueueNode* node = Q->front->next;
int data = node->data;
Q->front->next = node->next;
if (Q->rear == node) {
Q->rear = Q->front;
}
delete node;
return data;
}
void BFS(ALGraph *G, int i) {
bool visited[G->vexnum]; //记录每个顶点是否被访问过
memset(visited, false, G->vexnum * sizeof(bool));
visited[i] = true; //标记起点i已被访问过
cout << G->adjlist[i].data << " "; //输出起点i的值
Queue Q;
InitQueue(&Q); //初始化队列Q
EnQueue(&Q, i); //将起点i入队
while (Q.front != Q.rear) {
int j = DeQueue(&Q); //取出队头元素,即当前遍历到的顶点
for (EdgeNode* p = G->adjlist[j].firstedge; p != nullptr; p = p->next) {
int k = p->adjvex; //取出当前顶点的邻接点
if (!visited[k]) { //如果该邻接点未被访问过
visited[k] = true; //标记该邻接点已被访问过
cout << G->adjlist[k].data << " "; //输出该邻接点的值
EnQueue(&Q, k); //将该邻接点入队
}
}
}
}
```
上述代码使用了队列来实现广度优先遍历,遍历到每个顶点时将其值输出。函数BFS的参数是一个指向图的指针G和一个起点i。函数内部使用了邻接表来表示图,visited数组来记录每个顶点是否被访问过,Queue结构体来实现队列。函数先将起点i标记为已访问,输出其值,然后将其入队。之后进入循环,每次取出队头元素j,遍历其所有邻接点,如果邻接点k未被访问过,则标记为已访问,输出其值,然后将其入队。直到队列为空,遍历结束。
void creatGraph(Graph* G, VertexType ver[], VertexType edge[][2]); void DFS(Graph* G, int k, int*_visit, char** res); void BFS(Graph* G, int*_visit, char res[]); void DestroyGraph(Graph*G); void Print(Graph*G);用c语言补全这些函数代码
下面是这些函数的代码实现:
```c
#define MAX_VERTEX_NUM 100 // 最大顶点数
typedef char VertexType; // 顶点类型
typedef struct {
VertexType vexs[MAX_VERTEX_NUM]; // 顶点数组
int arcs[MAX_VERTEX_NUM][MAX_VERTEX_NUM]; // 邻接矩阵
int vexnum, arcnum; // 顶点数,边数
} Graph;
// 创建图
void creatGraph(Graph* G, VertexType ver[], VertexType edge[][2]) {
int i, j, k;
G->vexnum = sizeof(ver) / sizeof(VertexType);
G->arcnum = sizeof(edge) / sizeof(VertexType) / 2;
for (i = 0; i < G->vexnum; i++) {
G->vexs[i] = ver[i]; // 顶点数组赋值
}
for (i = 0; i < G->vexnum; i++) {
for (j = 0; j < G->vexnum; j++) {
G->arcs[i][j] = 0; // 初始化邻接矩阵
}
}
for (k = 0; k < G->arcnum; k++) {
i = j = -1;
// 查找边的两个顶点在顶点数组中的下标
for (int l = 0; l < G->vexnum; l++) {
if (edge[k][0] == G->vexs[l]) {
i = l;
}
if (edge[k][1] == G->vexs[l]) {
j = l;
}
}
if (i != -1 && j != -1) {
G->arcs[i][j] = G->arcs[j][i] = 1; // 无向图,两个方向都要标记
}
}
}
// 深度优先遍历
void DFS(Graph* G, int k, int*_visit, char** res) {
int i;
res[*_visit] = &(G->vexs[k]); // 将遍历到的顶点存入 res 数组
*_visit += 1;
_visit[k] = 1; // 标记该顶点已被访问
for (i = 0; i < G->vexnum; i++) {
if (G->arcs[k][i] && !_visit[i]) {
DFS(G, i, _visit, res); // 递归访问邻接顶点
}
}
}
// 广度优先遍历
void BFS(Graph* G, int*_visit, char res[]) {
int i, j, k;
int queue[MAX_VERTEX_NUM], front = 0, rear = 0;
for (i = 0; i < G->vexnum; i++) {
if (!_visit[i]) {
res[rear++] = G->vexs[i]; // 将未被访问的顶点加入队列
_visit[i] = 1; // 标记该顶点已被访问
}
while (front < rear) {
k = queue[front++]; // 出队一个顶点
for (j = 0; j < G->vexnum; j++) {
if (G->arcs[k][j] && !_visit[j]) {
res[rear++] = G->vexs[j]; // 将邻接顶点加入队列
_visit[j] = 1; // 标记该顶点已被访问
}
}
}
}
}
// 销毁图
void DestroyGraph(Graph*G) {
G->vexnum = 0;
G->arcnum = 0;
}
// 打印图
void Print(Graph*G) {
int i, j;
printf("顶点数:%d,边数:%d\n", G->vexnum, G->arcnum);
printf("顶点数组:");
for (i = 0; i < G->vexnum; i++) {
printf("%c ", G->vexs[i]);
}
printf("\n邻接矩阵:\n");
for (i = 0; i < G->vexnum; i++) {
for (j = 0; j < G->vexnum; j++) {
printf("%d ", G->arcs[i][j]);
}
printf("\n");
}
}
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)