【算法】BFS套路 含队列代码队列

news/2024/7/5 19:29:51

1126  地图分析

#define MAX_NUMS 1000000
 

typedef struct Node_ {
    int x;
    int y;
}Node;

typedef struct Queue_ {
    Node arr[MAX_NUMS];
    int front;
    int rear;
}Queue;

void QueueInit(Queue* queue)
{
    queue->front = 0;
    queue->rear = 0;
}

bool Full(Queue* queue)
{
    if ((queue->rear + 1) % MAX_NUMS == queue->front) {
        return true;
    }
    return false;
}

bool Empty(Queue* queue)
{
    return queue->front == queue->rear;
}

void QueuePush(Queue* queue, Node node)
{
    if (!Full(queue)) {
        queue->arr[queue->rear] = node;
        queue->rear = (queue->rear + 1) % MAX_NUMS;
    }
}

void QueuePop(Queue* queue)
{
    if (!Empty(queue)) {
        queue->front = (queue->front + 1) % MAX_NUMS;
    }
}

void QueueClear(Queue* queue)
{
    queue->front = 0;
    queue->rear = 0;
}

Node QueueTop(Queue* queue)
{
    return queue->arr[queue->front];
}

int QueueLength(Queue que)
{
    return (que.rear - que.front + MAX_NUMS) % MAX_NUMS;
}


int maxDistance(int** grid, int gridSize, int* gridColSize){
   if (!grid) {
        return -1;
    }
    int row = gridSize;
    int col = *gridColSize;
    Queue q;
    QueueInit(&q);
    int pass[100][100] = {0};
    for (int i = 0; i < row; i++) {
        for (int j = 0; j < col; j++) {
            if (grid[i][j]) {
                Node land = {i, j};
                pass[i][j] = 1;
                printf(" pass push[%d][%d]\n",i,j);
                QueuePush(&q, land);
            }
        }   
    }
    int res = 0;
    while(!Empty(&q)) {
        int curLen = QueueLength(q);
        for (int i = 0; i < curLen; i++) {
            Node temp = QueueTop(&q);
            QueuePop(&q);
            if (((temp.x - 1) >= 0) && !pass[temp.x - 1][temp.y]) {  //向上
                pass[temp.x - 1][temp.y] = 1;
                Node land = {temp.x - 1, temp.y};
                printf("start from [%d][%d], push[%d][%d]\n", temp.x, temp.y, temp.x - 1, temp.y);
                QueuePush(&q, land);
            }
            if (((temp.x + 1) < row) && !pass[temp.x + 1][temp.y]) {  //向下
                pass[temp.x + 1][temp.y] = 1;
                Node land = {temp.x + 1, temp.y};
                printf("start from [%d][%d], push[%d][%d]\n", temp.x, temp.y, temp.x + 1, temp.y);
                QueuePush(&q, land);
            }
            if (((temp.y + 1) < col) && !pass[temp.x][temp.y + 1]) {  //向右
                pass[temp.x][temp.y + 1] = 1;
                Node land = {temp.x, temp.y + 1};
                printf("start from [%d][%d], push[%d][%d]\n", temp.x, temp.y, temp.x, temp.y + 1);
                QueuePush(&q, land);
            }
            if (((temp.y - 1) >= 0) && !pass[temp.x][temp.y - 1]) {  //向左
                pass[temp.x][temp.y - 1] = 1;
                Node land = {temp.x, temp.y - 1};
                printf("start from [%d][%d], push[%d][%d]\n", temp.x, temp.y, temp.x, temp.y - 1);
                QueuePush(&q, land);
            }
        }

        int i = 0;
        for (; i < row; i++) {
            int j = 0;
            for (; j < col; j++) {
                if (pass[i][j] == 0) {
                    printf("pass[%d][%d] is 0\n", i, j);
                    break;
                }
            }
            if (j < col) {break;}
        }
        if (i >= row) {
            return (res == 0) ? -1 : (res + 1);
        }
        res++;
    }
    return -1;
}

 

 


http://www.niftyadmin.cn/n/1426156.html

相关文章

取现在时间和用户IP

java.text.*,java.util.*" SimpleDateFormat formatnew SimpleDateFormat("yyyy-MM-dd hh:mm:ss");String nowdateformat.format(new Date());<%nowdate%> String iprequest.getRemoteAddr();

计数1

String iprequest.getRemoteAddr(); 获取用户ip 记录新用户 <jsp:useBean id"mycount" class"com.pp.db.CountOnline "></jsp:useBean> <% String iprequest.getRemoteAddr(); //获得用户ip地址 mycount.setUseri…

pv promotion video

pvPromotion Video&#xff08;有人说和我们所说的MV是一回事&#xff0c;其实不然&#xff0c;看名字就知道啦&#xff0c;是宣传推广用的VIDEO&#xff0c;大家也应该注意到了PV都是在单曲推出前10天左右就在电视上有播了&#xff0c;就是为了宣传即将到来的新单曲&#xff0…

JSP在线统计 源码 未研究

http://d.download.csdn.net/down/244777/wumaolin jsporacle

window.open参数一览

<script type"text/javascript">function StorePage(){ddocument;td.selection?(d.selection.type!None?d.selection.createRange().text:):(d.getSelection?d.getSelection():);void(keyitwindow.open(http://www.365key.com/storeit.aspx?tescape(d.titl…

使用弹窗代码生成器及各类弹窗浮动广告code

http://tool.admin5.com/tools/OpenWindows.html 弹窗代码生成器 http://top.admin5.com/daima/ 各类广告代码 (文字滚动消息广告两个焦点代码(就是像迅雷左上角的几组换图的广告一样) 影院模式幻灯广告(同焦点一样的) 10款JS广告代码 广告http://www.haofa.net/…

媒体查询简单应用——网页字体自适应窗口大小

什么是媒体查询&#xff1f; 答&#xff1a;媒体查询是向不同设备&#xff08;手机&#xff0c;平板&#xff0c;电脑&#xff09;提供不同样式的一种不错方式&#xff0c;它为每种类型的用户提供了最佳的体验。 举一个简单的例子&#xff1a;网页的字体大小随窗口的大小而改…