??
by 老子是北瓜 @ 2020-05-19 10:41:56
代码放上来看看?
by iMya_nlgau @ 2020-05-19 10:42:08
?
by Implicit @ 2020-05-19 10:44:03
代码
by 一只书虫仔 @ 2020-05-19 10:45:04
目测手写队列写错了
by rui_er @ 2020-05-19 10:51:56
为啥我用 `queue` 就能 A ……
by 一只书虫仔 @ 2020-05-19 10:52:57
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
int n, m, x, y;
int board[405][405];
int direction[8][2] = { {2, 1},{1, 2},{-1, -2},{-2, -1},{1, -2},{2, -1},{-1, 2}, {-2, 1} };
int visit[405][405]; // 默认为0
typedef struct STRUCTURE {
int x[405 * 405]; // 数组范围!
int y[405 * 405];
int rear;
int front;
}Queue;
Queue* Initial() {
Queue* queue = (Queue*)malloc(sizeof(Queue));
if (queue != NULL) { // 分配成功
queue->rear = queue->front = 0; // 默认值对结构体指针对象无效,对结构体实体对象有效
return queue;
}
else {
return NULL;
}
}
void In(Queue* queue, int x, int y) {
int index = queue->rear;
queue->x[index] = x;
queue->y[index] = y;
queue->rear += 1;
}
void Out(Queue* queue, int& x, int& y) {
//printf("%d %d\n", queue->front, queue->rear);
if (queue->rear != queue->front) {
x = queue->x[queue->front];
y = queue->y[queue->front];
queue->front += 1;
}
else {
//printf("OVER\n");
x = -1;
y = -1;
}
}
void bfs(Queue* queue) {
int x1, y1, x2, y2;
while (true) {
Out(queue, x1, y1);
if (x1 == -1 && y1 == -1) {
break;
}
for (int i = 0; i < 8; i++) {
x2 = x1 + direction[i][0];
y2 = y1 + direction[i][1];
if (x2 >= 1 && x2 <= n && y2 >= 1 && y2 <= m && visit[x2][y2] == 0) {
board[x2][y2] = board[x1][y1] + 1;
visit[x2][y2] = 1;
In(queue, x2, y2);
}
}
}
}
int main()
{
scanf_s("%d%d%d%d", &n, &m, &x, &y);
memset(board, -1, sizeof(board));
board[x][y] = 0;
visit[x][y] = 1;
Queue* queue = Initial();
In(queue, x, y);
bfs(queue);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
printf("%-5d", board[i][j]);
}
printf("\n");
}
system("pause");
return 0;
}
```
大家帮忙看一下,我是在本地完全能运行的,谢谢了
by 自律一点少年 @ 2020-05-19 11:03:21
@[自律一点少年](/user/344790) STL不香吗?
by Veranda @ 2020-05-19 11:05:50
@[CodeEmperor](/user/231800) 这没错吧
by 自律一点少年 @ 2020-05-19 11:10:41
@[CodeEmperor](/user/231800) 自己实现起来也不麻烦,以后也可以粘贴继续用,差不多
by 自律一点少年 @ 2020-05-19 11:12:20