Java高尔夫_LeetCode 675. Cut Off Trees for Golf Event 为高尔夫比赛砍树 (C++/Java)

题目:

You are asked to cut off trees in a forest for a golf event. The forest is represented as a non-negative 2D map, in this map:

0 represents the obstacle can't be reached.

1 represents the ground can be walked through.

The place with number bigger than 1 represents a tree can be walked through, and this positive number represents the tree's height.

You are asked to cut off all the trees in this forest in the order of tree's height - always cut off the tree with lowest height first. And after cutting, the original place has the tree will become a grass (value 1).

You will start from the point (0, 0) and you should output the minimum steps you need to walk to cut off all the trees. If you can't cut off all the trees, output -1 in that situation.

You are guaranteed that no two trees have the same height and there is at least one tree needs to be cut off.

Example 1:

Input:

[

[1,2,3],

[0,0,4],

[7,6,5]

]

Output: 6

Example 2:

Input:

[

[1,2,3],

[0,0,0],

[7,6,5]

]

Output: -1

Example 3:

Input:

[

[2,3,4],

[0,0,5],

[8,7,6]

]

Output: 6

Explanation: You started from the point (0,0) and you can cut off the tree in (0,0) directly without walking.

Hint: size of the given matrix will not exceed 50x50.

分析:

给定一个二维数组,其中的数字0表示障碍,1表示平地,大于等于2的数字表示树的高度,现在要求按照树高度由小到大进行砍伐,求最小步数。

先遍历二维数组,将树的高度保存起来并排序,同时记录相应的坐标。

从(0, 0)开始对树依次访问,查找是否有路径到达树,在这里使用BFS进行搜索,如果存在树无法进行访问,意味着我们无法砍伐掉所有的树,返回-1,当遍历完所有的树后,返回统计的步数即可。

程序:

C++

classSolution {public:int cutOffTree(vector>&forest) {

m=forest.size();

n= forest[0].size();

vector>trees;for(int i = 0; i < m; i++)for(int j = 0; j < n; ++j)if(forest[i][j] > 1)

trees.emplace_back(forest[i][j], i, j);

sort(trees.begin(), trees.end());int fx = 0;int fy = 0;int res = 0;for(int i = 0; i < trees.size(); i++){int sx = get<1>(trees[i]);int sy = get<2>(trees[i]);int steps =BFS(forest, fx, fy, sx, sy);if(steps == -1)return -1;

res+=steps;

fx=sx;

fy=sy;

}returnres;

}private:int BFS(vector>& forest, int fx, int fy, int sx, intsy){

vector> visited(m, vector(n, 0));

queue>q;static int mov[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};

q.emplace(fx, fy);int steps = 0;while(!q.empty()){int num =q.size();while(num--){

auto node=q.front();

q.pop();int cx =node.first;int cy =node.second;if (cx == sx && cy ==sy)returnsteps;for (int i = 0; i < 4; ++i) {int x = cx + mov[i][0];int y = cy + mov[i][1];if (x < 0 || x >= m || y < 0 || y >= n || !forest[x][y]||visited[x][y])continue;

visited[x][y]= 1;

q.emplace(x, y);

}

}

steps++;

}return -1;

}intm;intn;

};

Java

classSolution {public int cutOffTree(List>forest) {

m=forest.size();

n= forest.get(0).size();

ArrayList list = new ArrayList<>();

HashMap> map = new HashMap<>();for(int i = 0; i < m; ++i){for(int j = 0; j < n; ++j){int height =forest.get(i).get(j);if(height > 1){

list.add(height);

map.put(height,new Pair<>(i, j));

}

}

}

Collections.sort(list);int fx = 0;int fy = 0;int res = 0;for(Integer i:list){int sx =map.get(i).getKey();int sy =map.get(i).getValue();int steps =BFS(forest, fx, fy, sx, sy);if(steps == -1)return -1;

res+=steps;

fx=sx;

fy=sy;

}returnres;

}private int BFS(List> forest, int fx, int fy, int sx, intsy){int[][] mov = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};int[][] visited = new int[m][n];

ArrayDeque> arrayDeque = new ArrayDeque<>();

arrayDeque.add(new Pair<>(fx, fy));int steps = 0;while(!arrayDeque.isEmpty()){int num =arrayDeque.size();while(num-- > 0){

Pair p =arrayDeque.pollFirst();int cx =p.getKey();int cy =p.getValue();if(cx == sx && cy ==sy)returnsteps;for (int i = 0; i < 4; ++i) {int x = cx + mov[i][0];int y = cy + mov[i][1];if (x < 0 || x >= m || y < 0 || y >= n || forest.get(x).get(y) == 0 || visited[x][y] == 1)continue;

visited[x][y]= 1;

arrayDeque.addLast(new Pair<>(x, y));

}

}

steps++;

}return -1;

}private intm;private intn;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值