tst12_5_1

按规矩先上图

0,0
1,0
2,0
3,0
2,1
4,0
2,2
4,1
2,3
1,2
4,2
2,4
0,2
3,4
1,4
0,3
4,4
0,4

环形队列的最少元素数量,即为时序上最宽出元素数量即可
因为探索完A层即可弹出探索下一层B, 只需要最宽一层满足,其他层级都满足

为了检验该想发,编写程序验证即可
环形队列的检测和空队、满队的测试警告用数量标记法

/*
 *  广度优先算法解决迷宫问题
 */

#include <stdio.h>

#define MAX_ROW 5
#define MAX_COL 5
#define MAX_LEN 3
/*
 * 迷宫图, 1表示障碍, 0表示路径, 只允许上下左右移动
 */
int maze[MAX_ROW][MAX_COL] = {
    0, 1, 0, 0, 0, //
    0, 1, 0, 1, 0, //
    0, 0, 0, 0, 0, //
    0, 1, 1, 1, 0, //
    0, 0, 0, 1, 0, //
};

struct point {
  int row;
  int col;
};

struct point queue[MAX_LEN];
int head = 0, tail = 0;
int queue_len = 0;

void enqueue(struct point p);
struct point dequeue(void);
void print_maze(void);
void visit(int row, int col);

int main(int argc, char *argv[]) {
  struct point p = {
      0,
      0,
  };

  maze[p.row][p.col] = 2;
  enqueue(p);

  while (queue_len != 0) {
    p = dequeue();
    if (p.row == MAX_ROW - 1 && p.col == MAX_COL - 1) {
      printf("there is a path!");
      break;
    }
    if (p.row + 1 <= MAX_ROW - 1 && maze[p.row + 1][p.col] == 0)
      visit(p.row + 1, p.col);
    if (p.row - 1 >= 0 && maze[p.row - 1][p.col] == 0)
      visit(p.row - 1, p.col);
    if (p.col + 1 <= MAX_COL - 1 && maze[p.row][p.col + 1] == 0)
      visit(p.row, p.col + 1);
    if (p.col - 1 >= 0 && maze[p.row][p.col - 1] == 0)
      visit(p.row, p.col - 1);
    print_maze();
  }
  return 0;
}

void enqueue(struct point p) {
  if (queue_len == MAX_LEN) {
    printf("full of queue, cannot enqueue!\n");
    return;
  }
  queue_len++;
  queue[tail++ % MAX_LEN] = p;
}

struct point dequeue(void) {
  if (queue_len == 0) {
    printf("empty queue, cannot dequeue!\n");
    return queue[head];
  }
  queue_len--;
  return queue[head++ % MAX_LEN];
}

void print_maze(void) {
  for (int i = 0; i < MAX_ROW; i++) {
    for (int j = 0; j < MAX_COL; j++)
      printf("%d", maze[i][j]);
    printf("\n");
  }
  printf("**********\n");
}

void visit(int row, int col) {
  struct point visit_point = {
      row,
      col,
  };
  maze[row][col] = 2;
  enqueue(visit_point);
}

结果


21000
21010
00000
01110
00010
**********
21000
21010
20000
01110
00010
**********
21000
21010
22000
21110
00010
**********
21000
21010
22000
21110
20010
**********
21000
21010
22200
21110
20010
**********
21000
21010
22200
21110
22010
**********
21000
21210
22220
21110
22010
**********
21000
21210
22220
21110
22210
**********
21200
21210
22220
21110
22210
**********
21200
21210
22222
21110
22210
**********
21200
21210
22222
21110
22210
**********
21220
21210
22222
21110
22210
**********
21220
21212
22222
21112
22210
**********
21222
21212
22222
21112
22210
**********
21222
21212
22222
21112
22212
**********
21222
21212
22222
21112
22212
**********
21222
21212
22222
21112
22212
**********
there is a path!

全程没有警告,OK成功

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
请帮我解释这段代码:#include "cmd_parse.h" static int bufed_uart_rcv_1B(void *ref, uint8_t *c) { BUFED_UART_T *h = ref; return bufed_uart_rcv(h, c, 1); } CMD_PARSE_T *cmd_ps_1; osThreadId rx_cmp_tst_hd; extern RNG_HandleTypeDef hrng; void uart1_fast_loopback_test(uint32_t fatfs_ok) { uint8_t *tx_buf, *rx_buf; tx_buf= pvPortMalloc(URT_TST_BUF_LEN); if(tx_buf == NULL){ GS_LOGPRT_ERR("tx_buf pvPortMalloc failed.\r\n"); goto err_00; } rx_buf= pvPortMalloc(URT_TST_BUF_LEN); if(rx_buf == NULL){ GS_LOGPRT_ERR("tx_buf pvPortMalloc failed.\r\n"); goto err_01; } FIL *fp = pvPortMalloc(sizeof(*fp)); if(fp==NULL){ GS_LOGPRT_ERR("tx_buf pvPortMalloc failed.\r\n"); goto err_02; } bfdurt_tst_01.rx_buf = rx_buf; bfdurt_tst_01.tx_buf = tx_buf; bfdurt_tst_01.buf_size = URT_TST_BUF_LEN; bfdurt_tst_01.err_cnt = 0; for(uint32_t i = 0; i < URT_TST_BUF_LEN; i++) tx_buf[i] = HAL_RNG_GetRandomNumber(&hrng); osThreadDef(rx_cmp_tst_tsk, uart_rx_cmp, osPriorityBelowNormal, 0, 200); rx_cmp_tst_hd = osThreadCreate(osThread(rx_cmp_tst_tsk), &(bfdurt_tst_ptr)); osDelay(120); uint32_t lp; cmdprs_init(&cmd_ps_1, 256, &RBFD_UART_GET_UART(urt2), bufed_uart_rcv_1B); uint32_t f_num = 0; size_t n; while(1){ GS_Printf("Input test data length\r\n"); cmdprs_read_1line(cmd_ps_1); char ch; int scn = sscanf((void*)cmd_ps_1->buf->data,"%lu%c", &lp, &ch); if(scn == 2){ if(ch == 'M' || ch == 'm') lp <<= 10U; else if(ch == 'G' || ch == 'g') lp <<= 20U; else if(ch == 'K' || ch == 'k') ; else lp >>= 10U; lp /= (URT_TST_BUF_LEN/1024); }else{ GS_Printf("ERROR\r\n"); break; }
07-14
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值