寻友之旅Go、C++以及Java解决方法与比较

103 阅读4分钟

题目

image.png

实现思路:

这个题目是一道典型的求最短路径的题目,我们可以使用广度优先搜索来解决。

  1. 首先我们需要输入当前小青所在的位置N和小码所在的位置K
  2. 用队列来存储当前小青所在的位置,并将当前位置加入队列
  3. 创建一个visited数组来记录每个位置是否被访问过
  4. 使用循环遍历队列中的所有元素,每次遍历时将当前位置的step值+1,表示离小码家距离+1
  5. 判断当前位置是否已经访问过,如果没有访问过,则将其加入队列中,并将visited数组对应位置设置为true
  6. 分别判断当前位置-1,+1,*2是否在范围内,如果在范围内且没有访问过,则将其加入队列
  7. 如果当前位置为K,则输出step值-1,表示小青到小码家所需的最短时间

不同语言的对比

在Go中,我们使用了make()函数来创建队列和visited数组,还使用了append()函数来向队列和数组中添加元素。

如果用C++来写的话,我们可以使用STL中的queue来实现队列,用vector来实现visited数组。在C++中我们可以使用push()函数来向队列和数组中添加元素,使用front()函数来获取队列的队首元素。

需要注意的是,在 C++中没有内置的 bool 数组,我们可以使用一个vector<bool>来替代,但是这种方式在占用空间上略微有所增加。

在C++中,我们需要自己实现判重,并在遍历过程中手动维护队列。

总体来说,Go语言中的内置函数和数据结构更加方便使用,可以帮助我们简化代码。而C++中需要更多的手工实现和维护。

此外,Go语言中的代码风格也更加简洁,缩进统一,自动垃圾回收等特性使得Go语言更加易于编写和维护。而C++中的代码风格较为复杂,需要更多的手动维护。

在这道题目中,Go语言的简洁性和易用性帮助我们编写出了更加简洁的代码,而C++则需要更多的手动实现和维护。

综上所述,Go语言更加简洁易用,而C++则更加灵活,根据具体需求和开发场景来选择使用哪种语言是更加合理的。

在这道题目中,我们使用了广搜来解决问题,使用了队列来存储当前小青所在的位置,并使用visited数组来记录每个位置是否被访问过。在Go语言中,我们使用了内置的make()函数来创建队列和visited数组,还使用了append()函数来向队列和数组中添加元素。而在C++中,我们可以使用STL中的queue来实现队列,用vector来实现visited数组。需要自己实现判重,并在遍历过程中手动维护队列。

在Java中:我们使用了Java中的队列QueueScanner类来实现程序。使用了LinkedList来实现队列,使用visited数组来记录每个位置是否被访问过,使用while循环来遍历队列,每次遍历时将当前位置的step值+1,表示离小码家距离+1。我们判断当前位置是否等于k,如果等于k则输出step值并结束程序。

和之前的Go和C++代码相比,Java代码实现的思路是相似的,都是使用广搜来求解最短路径问题。在Java中我们使用了QueueLinkedList来实现队列,使用Scanner来输入数据。和Go语言类似,Java也提供了许多内置函数和数据结构来简化代码。在Java程序中,我们可以使用面向对象的思想来编写程序,这样可以更好地组织代码,增强代码的可重用性。

源码:

1、Go

package main

import (
    "fmt"
)

func main() {
    var n, k int
    fmt.Scan(&n, &k)
    queue := make([]int, 1)
    queue[0] = n
    visited := make([]bool, 100001)
    visited[n] = true
    step := 0
    for len(queue) > 0 {
        step++
        size := len(queue)
        for i := 0; i < size; i++ {
            node := queue[i]
            if node == k {
                fmt.Println(step - 1)
                return
            }
            if node-1 >= 0 && !visited[node-1] {
                queue = append(queue, node-1)
                visited[node-1] = true
            }
            if node+1 <= 100000 && !visited[node+1] {
                queue = append(queue, node+1)
                visited[node+1] = true
            }
            if node*2 <= 100000 && !visited[node*2] {
                queue = append(queue, node*2)
                visited[node*2] = true
            }
        }
        queue = queue[size:]
    }
}

2、C++

#include<iostream>
#include<queue>
using namespace std;
int main(){
    int n, k;
    cin >> n >> k;
    queue<int> q;
    vector<bool> visited(100001,false);
    q.push(n);
    int step = 0;
    while(!q.empty()){
        int qSize = q.size();
        while(qSize--){
            int curr = q.front();
            q.pop();
            if(curr == k){
                cout << step << endl;
                return 0;
            }
            if(curr-1>=0 && !visited[curr-1]){
                q.push(curr-1);
                visited[curr-1] = true;
            }
            if(curr+1<=100000 && !visited[curr+1]){
                q.push(curr+1);
                visited[curr+1] = true;
            }
            if(curr*2<=100000 && !visited[curr*2]){
                q.push(curr*2);
                visited[curr*2] = true;
            }
        }
        step++;
    }
    return 0;
}

3、Java

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int k = sc.nextInt();
        Queue<Integer> q = new LinkedList<>();
        boolean[] visited = new boolean[100001];
        q.offer(n);
        int step = 0;
        while (!q.isEmpty()) {
            int size = q.size();
            for (int i = 0; i < size; i++) {
                int curr = q.poll();
                if (curr == k) {
                    System.out.println(step);
                    return;
                }
                if (curr - 1 >= 0 && !visited[curr - 1]) {
                    q.offer(curr - 1);
                    visited[curr - 1] = true;
                }
                if (curr + 1 <= 100000 && !visited[curr + 1]) {
                    q.offer(curr + 1);
                    visited[curr + 1] = true;
                }
                if (curr * 2 <= 100000 && !visited[curr * 2]) {
                    q.offer(curr * 2);
                    visited[curr * 2] = true;
                }
            }
            step++;
        }
    }
}