用C++写一个Priority-Flood算法
时间: 2023-03-26 20:01:45 浏览: 147
迪杰斯特拉算法c++.pdf
Priority-Flood算法是一种用于数字高程模型数据处理的算法,可以用于洪水模拟、地形分析等领域。具体实现可以参考以下代码:
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#define ROWS 10
#define COLS 10
typedef struct {
int row;
int col;
int priority;
} Cell;
int map[ROWS][COLS] = {
{5, 4, 4, 3, 2, 1, 1, 1, 1, 1},
{5, 5, 4, 3, 2, 2, 1, 1, 1, 1},
{5, 5, 4, 3, 3, 2, 2, 1, 1, 1},
{5, 5, 4, 4, 3, 3, 2, 2, 1, 1},
{5, 5, 5, 4, 4, 3, 3, 2, 1, 1},
{5, 5, 5, 4, 4, 3, 3, 2, 2, 1},
{5, 5, 5, 5, 4, 4, 3, 3, 2, 1},
{5, 5, 5, 5, 4, 4, 3, 3, 2, 2},
{5, 5, 5, 5, 5, 4, 4, 3, 3, 2},
{5, 5, 5, 5, 5, 4, 4, 3, 3, 3}
};
Cell queue[ROWS * COLS];
int front = , rear = ;
void enqueue(Cell c) {
queue[rear++] = c;
}
Cell dequeue() {
return queue[front++];
}
int is_empty() {
return front == rear;
}
void priority_flood(int map[][COLS], int row, int col) {
int i, j;
Cell c = {row, col, map[row][col]};
enqueue(c);
while (!is_empty()) {
c = dequeue();
i = c.row;
j = c.col;
if (i > && map[i - 1][j] < c.priority) {
Cell nc = {i - 1, j, map[i - 1][j]};
enqueue(nc);
map[i - 1][j] = c.priority;
}
if (i < ROWS - 1 && map[i + 1][j] < c.priority) {
Cell nc = {i + 1, j, map[i + 1][j]};
enqueue(nc);
map[i + 1][j] = c.priority;
}
if (j > && map[i][j - 1] < c.priority) {
Cell nc = {i, j - 1, map[i][j - 1]};
enqueue(nc);
map[i][j - 1] = c.priority;
}
if (j < COLS - 1 && map[i][j + 1] < c.priority) {
Cell nc = {i, j + 1, map[i][j + 1]};
enqueue(nc);
map[i][j + 1] = c.priority;
}
}
}
int main() {
int i, j;
printf("Before priority flood:\n");
for (i = ; i < ROWS; i++) {
for (j = ; j < COLS; j++) {
printf("%d ", map[i][j]);
}
printf("\n");
}
priority_flood(map, 5, 5);
printf("After priority flood:\n");
for (i = ; i < ROWS; i++) {
for (j = ; j < COLS; j++) {
printf("%d ", map[i][j]);
}
printf("\n");
}
return ;
}
阅读全文