import java.util.Arrays; import java.util.LinkedList; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); Integer[] arr = Arrays.stream(sc.nextLine().split(",")).map(Integer::parseInt).toArray(Integer[]::new); System.out.println(getResult(arr)); } public static int getResult(Integer[] arr) { int n = (int) Math.sqrt(arr.length); int[][] matrix = new int[n][n]; LinkedList<Integer[]> queue = new LinkedList<>(); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { matrix[i][j] = arr[i * n + j]; if (matrix[i][j] == 1) queue.add(new Integer[] {i, j}); } } if (queue.size() == 0 || queue.size() == arr.length) { return -1; } int healthy = arr.length - queue.size(); int[][] offsets = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}}; int day = 0; while (queue.size() > 0 && healthy > 0) { LinkedList<Integer[]> newQue = new LinkedList<>(); for (Integer[] tmp : queue) { int x = tmp[0], y = tmp[1]; for (int[] offset : offsets) { int newX = x + offset[0]; int newY = y + offset[1]; if (newX >= 0 && newX < n && newY >= 0 && newY < n && matrix[newX][newY] == 0) { matrix[newX][newY] = 1; healthy--; newQue.add(new Integer[] {newX, newY}); } } } day++; queue = newQue; } return day; } }翻译成C++

时间: 2024-04-17 10:26:19 浏览: 18
以下是将给定的Java代码翻译为C++的版本: ```cpp #include <iostream> #include <vector> #include <queue> #include <cmath> using namespace std; int getResult(vector<int>& arr) { int n = sqrt(arr.size()); vector<vector<int>> matrix(n, vector<int>(n)); queue<pair<int, int>> q; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { matrix[i][j] = arr[i * n + j]; if (matrix[i][j] == 1) { q.push({i, j}); } } } if (q.empty() || q.size() == arr.size()) { return -1; } int healthy = arr.size() - q.size(); vector<vector<int>> offsets = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}}; int day = 0; while (!q.empty() && healthy > 0) { queue<pair<int, int>> newQ; while (!q.empty()) { pair<int, int> tmp = q.front(); q.pop(); int x = tmp.first, y = tmp.second; for (vector<int>& offset : offsets) { int newX = x + offset[0]; int newY = y + offset[1]; if (newX >= 0 && newX < n && newY >= 0 && newY < n && matrix[newX][newY] == 0) { matrix[newX][newY] = 1; healthy--; newQ.push({newX, newY}); } } } day++; q = newQ; } return day; } int main() { string input; getline(cin, input); vector<int> arr; size_t pos = 0; while ((pos = input.find(',')) != string::npos) { arr.push_back(stoi(input.substr(0, pos))); input.erase(0, pos + 1); } arr.push_back(stoi(input)); cout << getResult(arr) << endl; return 0; } ``` 请注意,上述代码只是翻译了给定的Java代码,并进行了一些必要的语法和数据结构的更改。如果您在使用时遇到任何问题,请提供更多信息以便我可以帮助您解决。

相关推荐

import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.LinkedList; import java.util.List; import java.util.Scanner; public class Main{ public static void main(String[] args) throws IOException { BufferedReader in=new BufferedReader(new InputStreamReader(System.in)); int n; double need; double sum=0; String s[]=in.readLine().split(" "); n=Integer.parseInt(s[0]); need=Double.parseDouble(s[1]); String amount[]=in.readLine().split(" "); String price[]=in.readLine().split(" "); ArrayList<Mooncake> cakes=new ArrayList<Mooncake>(); for(int i=0;i<n;i++){ Mooncake cake=new Mooncake(); cake.setAmount(Double.parseDouble(amount[i])); cake.setSales(Double.parseDouble(price[i])); cake.setValues(Double.parseDouble(price[i])/Double.parseDouble(amount[i])); cakes.add(cake); } Collections.sort(cakes); for(Mooncake c: cakes){ if(need>=c.amount){ need=need-c.amount; sum+=c.amount*c.values; } else{ sum+=need*c.values; need=0; } if(need==0){ break; } } System.out.printf("%.2f",sum); } static class Mooncake implements Comparable<Mooncake>{ double amount; double sales; double values; public double getAmount() { return amount; } public void setAmount(double amount) { this.amount = amount; } public double getSales() { return sales; } public void setSales(double sales) { this.sales = sales; } public double getValues() { return values; } public void setValues(double values) { this.values = values; } @Override public int compareTo(Mooncake arg0) { // TODO Auto-generated method stub return this.values>arg0.values?-1:1; } } }转c++

import java.io.*; import java.util.*; public class 1162 { static class Node { double r; double c; int j; int next; Node(int j, double r, double c, int next) { this.j = j; this.r = r; this.c = c; this.next = next; } } static final int INF = 0x3f3f3f3f; static final int N = 106; static int[] head = new int[N]; static int I; static Node[] side = new Node[N * 2]; static double[] dist = new double[N]; static void add(int i, int j, double r, double c) { side[I] = new Node(j, r, c, head[i]); head[i] = I++; } static boolean spfa(int s, double k, int n) { int[] num = new int[N]; boolean[] in = new boolean[N]; Arrays.fill(num, 0); Arrays.fill(in, false); Queue<Integer> qt = new LinkedList<>(); for (int i = 1; i <= n; ++i) { dist[i] = 0.0; } dist[s] = k; qt.offer(s); num[s] = 1; while (!qt.isEmpty()) { int x = qt.poll(); in[x] = false; if (x == s && dist[x] > k) { return true; } for (int t = head[x]; t != -1; t = side[t].next) { int j = side[t].j; if (dist[j] < (dist[x] - side[t].c) * side[t].r) { dist[j] = (dist[x] - side[t].c) * side[t].r; while (!in[j]) { ++num[j]; if (num[j] >= n) { return true; } in[j] = true; qt.offer(j); } } } } return false; } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n, m, s; double k; while (scanner.hasNext()) { n = scanner.nextInt(); m = scanner.nextInt(); s = scanner.nextInt(); k = scanner.nextDouble(); Arrays.fill(head, -1); I = 0; while (m-- > 0) { int a = scanner.nextInt(); int b = scanner.nextInt(); double rab = scanner.nextDouble(); double cab = scanner.nextDouble(); double rba = scanner.nextDouble(); double cba = scanner.nextDouble(); add(a, b, rab, cab); add(b, a, rba, cba); } if (spfa(s, k, n)) { System.out.println("YES"); } else { System.out.println("NO"); } } scanner.close(); } }解释这段代码并说他的计算复杂度

package LiuShuMa; import java.util.*; public class Test { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); // 测试数据组数 while (n-- > 0) { int[] start = new int[6]; // 初始布局 int[] end = new int[]{1, 2, 3, 4, 5, 6}; // 目标布局 for (int i = 0; i < start.length; i++) { start[i] = scanner.nextInt(); } int ans = BFS(start, end); System.out.println(ans); } } static int BFS(int[] start, int[] end) { Queue<int[]> queue = new LinkedList<>(); // 队列存储布局 Map<String, Integer> dist = new HashMap<>(); // 记录初始布局到目标布局的距离 String startStr = Arrays.toString(start); // 初始布局转为字符串 queue.offer(start); dist.put(startStr, 0); while (!queue.isEmpty()) { int[] cur = queue.poll(); String curStr = Arrays.toString(cur); if (curStr.equals(Arrays.toString(end))) { // 当前布局与目标布局相等 return dist.get(curStr); } // α变换 int[] next1 = cur.clone(); next1[1] = cur[0]; next1[4] = cur[1]; next1[3] = cur[4]; next1[0] = cur[3]; String nextStr1 = Arrays.toString(next1); if (!dist.containsKey(nextStr1)) { queue.offer(next1); dist.put(nextStr1, dist.get(curStr) + 1); } // β变换 int[] next2 = cur.clone(); next2[2] = cur[1]; next2[5] = cur[2]; next2[4] = cur[5]; next2[1] = cur[4]; String nextStr2 = Arrays.toString(next2); if (!dist.containsKey(nextStr2)) { queue.offer(next2); dist.put(nextStr2, dist.get(curStr) + 1); } } return -1; // 没有符合条件的变换序列 } }给代码的每一行加注释

最新推荐

recommend-type

k8s1.16的jenkins部署java项目cicd(cd手动)-kubernetes安装包和详细文档笔记整理

k8s1.16的jenkins部署java项目cicd(cd手动)-kubernetes安装包和详细文档笔记整理
recommend-type

sja1311.x86_64.tar.gz

SQLyong 各个版本,免费下载 SQLyog是业界著名的Webyog公司出品的一款简洁高效、功能强大的图形化MySQL数据库管理工具。使用SQLyog可以快速直观地让您从世界的任何角落通过网络来维护远端的MySQL数据库。
recommend-type

debugpy-1.1.0-cp34-cp34m-manylinux1_x86_64.whl

Python库是一组预先编写的代码模块,旨在帮助开发者实现特定的编程任务,无需从零开始编写代码。这些库可以包括各种功能,如数学运算、文件操作、数据分析和网络编程等。Python社区提供了大量的第三方库,如NumPy、Pandas和Requests,极大地丰富了Python的应用领域,从数据科学到Web开发。Python库的丰富性是Python成为最受欢迎的编程语言之一的关键原因之一。这些库不仅为初学者提供了快速入门的途径,而且为经验丰富的开发者提供了强大的工具,以高效率、高质量地完成复杂任务。例如,Matplotlib和Seaborn库在数据可视化领域内非常受欢迎,它们提供了广泛的工具和技术,可以创建高度定制化的图表和图形,帮助数据科学家和分析师在数据探索和结果展示中更有效地传达信息。
recommend-type

SQLyog-12.5.0-0.x64Community.exe

SQLyog-12.5.0-0.x64Community
recommend-type

debugpy-1.0.0b12-cp35-cp35m-manylinux1_i686.whl

Python库是一组预先编写的代码模块,旨在帮助开发者实现特定的编程任务,无需从零开始编写代码。这些库可以包括各种功能,如数学运算、文件操作、数据分析和网络编程等。Python社区提供了大量的第三方库,如NumPy、Pandas和Requests,极大地丰富了Python的应用领域,从数据科学到Web开发。Python库的丰富性是Python成为最受欢迎的编程语言之一的关键原因之一。这些库不仅为初学者提供了快速入门的途径,而且为经验丰富的开发者提供了强大的工具,以高效率、高质量地完成复杂任务。例如,Matplotlib和Seaborn库在数据可视化领域内非常受欢迎,它们提供了广泛的工具和技术,可以创建高度定制化的图表和图形,帮助数据科学家和分析师在数据探索和结果展示中更有效地传达信息。
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

MATLAB柱状图在信号处理中的应用:可视化信号特征和频谱分析

![matlab画柱状图](https://img-blog.csdnimg.cn/3f32348f1c9c4481a6f5931993732f97.png) # 1. MATLAB柱状图概述** MATLAB柱状图是一种图形化工具,用于可视化数据中不同类别或组的分布情况。它通过绘制垂直条形来表示每个类别或组中的数据值。柱状图在信号处理中广泛用于可视化信号特征和进行频谱分析。 柱状图的优点在于其简单易懂,能够直观地展示数据分布。在信号处理中,柱状图可以帮助工程师识别信号中的模式、趋势和异常情况,从而为信号分析和处理提供有价值的见解。 # 2. 柱状图在信号处理中的应用 柱状图在信号处理
recommend-type

用Spring boot和vue写一个登录注册界面

好的,这是一个比较常见的技术问题,我可以为您提供一些参考资料和思路。首先,您可以使用Spring Boot作为后端框架来处理用户认证和注册的逻辑,比如使用Spring Security实现用户登录认证。同时,您还需要设计相应的数据模型和数据库表结构来存储用户信息。在前端方面,您可以使用Vue.js作为框架来构建登录注册页面,使用Axios来发起API请求并和后端进行交互。当然,在实现过程中,还需要考虑一些具体细节,比如数据校验、安全性和用户体验等方面。希望这些信息能够帮助到您。
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。