URL
链接:https://leetcode.cn/problems/cut-off-trees-for-golf-event/
题目



分析


源码
工程结构
.
└── main_675.c
源码文件
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
//#define DEBUG
typedef struct
{
int x;
int y;
int height;
} TREE;
static int dirs[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
static int cmp(const void *pa, const void *pd)
{
return ((TREE *)pa)->height - ((TREE *)pd)->height;
}
int bfs(int **forest, int row, int col, int sx, int sy, int tx, int ty)
{
if (sx == tx && sy == ty)
{
return 0;
}
int step = 0;
int *queue = (int *)malloc(sizeof(int) * row * col);
int *visited = (int *)malloc(sizeof(int) * row * col);
int head = 0, tail = 0;
memset(visited, 0, sizeof(int) * row * col);
queue[tail++] = sx * col + sy;
visited[sx * col + sy] = true;
while (head != tail)
{
step++;
int sz = tail - head;
for (int i = 0; i < sz; ++i)
{
int cx = queue[head] / col;
int cy = queue[head] % col;
head++;
for (int j = 0; j < 4; ++j)
{
int nx = cx + dirs[j][0];
int ny = cy + dirs[j][1];
if (nx >= 0 && nx < row && ny >= 0 && ny < col)
{ // !!! >=
if (!visited[nx * col + ny] && forest[nx][ny] > 0)
{
if (nx == tx && ny == ty)
{
free(queue);
free(visited);
return step;
}
queue[tail++] = nx * col + ny;
visited[nx * col + ny] = true;
}
}
}
}
}
free(queue);
free(visited);
return -1;
}
int cut_off_tree(int **forest, int forest_size, int *forest_colsize)
{
int row = forest_size;
int col = forest_colsize[0];
int tree_size = 0;
for (int i = 0; i < row; ++i)
{
// FIXME
if (col == 3)
{
printf("input: forest[%d] = {%d,%d,%d}\n", i, forest[i][0], forest[i][1], forest[i][2]);
}
for (int j = 0; j < col; ++j)
{
if (forest[i][j] > 1)
{
tree_size++;
}
}
}
#ifdef DEBUG
printf("DEBUG: row = %d, col = %d\n", row, col);
printf("DEBUG: tree_size = %d\n", tree_size);
#endif
TREE *trees = (TREE *)malloc(sizeof(TREE) * tree_size);
int pos = 0;
for (int i = 0; i < row; ++i)
{
for (int j = 0; j < col; ++j)
{
if (forest[i][j] > 1)
{
trees[pos].x = i;
trees[pos].y = j;
trees[pos].height = forest[i][j];
pos++;
}
}
}
#ifdef DEBUG
for (int i = 0; i < tree_size; ++i)
{
printf("DEBUG: tree[%d] = (%d,%d) %d\n", i, trees[i].x, trees[i].y, trees[i].height);
}
#endif
qsort(trees, tree_size, sizeof(TREE), cmp);
#ifdef DEBUG
printf("DEBUG: after qsort\n");
for (int i = 0; i < tree_size; ++i)
{
printf("DEBUG: tree[%d] = (%d,%d) %d\n", i, trees[i].x, trees[i].y, trees[i].height);
}
#endif
int cx = 0;
int cy = 0;
int ans = 0;
for (int i = 0; i < tree_size; ++i)
{
int steps = bfs(forest, row, col, cx, cy, trees[i].x, trees[i].y);
if (steps == -1)
{
printf("peace and love\n");
return -1;
}
ans += steps;
cx = trees[i].x;
cy = trees[i].y;
}
return ans;
}
#define ROW (3)
#define COL (3)
int main()
{
int col = COL;
// int data[][COL] = {{1,2,3},{0,0,4},{7,6,5}}; // ret step = 6
// int data[][COL] = {{1,2,3},{0,0,0},{7,6,5}}; // ret step = -1
int data[][COL] = {{2, 3, 4}, {0, 0, 5}, {8, 7, 6}}; // ret step = 6
int **forest = (int **)malloc(sizeof(int *) * ROW);
for (int i = 0; i < ROW; ++i)
{
forest[i] = (int *)malloc(sizeof(int) * COL);
}
for (int i = 0; i < ROW; ++i)
{
for (int j = 0; j < COL; ++j)
{
forest[i][j] = data[i][j];
}
}
int size = sizeof(data) / sizeof(data[0]);
int steps = cut_off_tree(forest, size, &col);
printf("Step(s) = %d\n", steps);
for (int i = 0; i < ROW; ++i)
{
free(forest[i]);
}
free(forest);
return 0;
}
LOG参考
input: forest[0] = {1,2,3}
input: forest[1] = {0,0,4}
input: forest[2] = {7,6,5}
Step(s) = 6
input: forest[0] = {1,2,3}
input: forest[1] = {0,0,0}
input: forest[2] = {7,6,5}
peace and love
Step(s) = -1
input: forest[0] = {2,3,4}
input: forest[1] = {0,0,5}
input: forest[2] = {8,7,6}
Step(s) = 6
源码概述
- 见源码
小结
peace and love

505

被折叠的 条评论
为什么被折叠?



