n = int(input()) lines = [list(map(int, input().split())) for _ in range(n)] def getResult(): ans = [] matrix = [[0] * 64 for _ in range(64)] for line in lines: num = line[0] for i in range(1, len(line), 2): x = line[i] y = line[i + 1] matrix[x][y] = num offsets = ((-1, 0), (1, 0), (0, -1), (0, 1)) for line in lines: num = line[0] p = 0 for i in range(1, len(line), 2): x = line[i] y = line[i + 1] for offsetX, offsetY in offsets: newX = x + offsetX newY = y + offsetY if 64 > newX >= 0 and 64 > newY >= 0: if matrix[newX][newY] != num: p += 1 else: p += 1 ans.append(p) return " ".join(map(str, ans)) print(getResult())将python转成C++
时间: 2024-02-14 20:27:28 浏览: 114
你想将这段 Python 代码转换成 C++ 代码。以下是将 Python 代码转换为 C++ 代码的等效实现:
```cpp
#include <iostream>
#include <vector>
using namespace std;
int main() {
int n;
cin >> n;
vector<vector<int>> lines(n);
for (int i = 0; i < n; i++) {
int num;
cin >> num;
vector<int> line;
line.push_back(num);
int x, y;
while (cin >> x >> y) {
line.push_back(x);
line.push_back(y);
if (cin.get() == '\n') {
break;
}
}
lines[i] = line;
}
vector<int> ans;
vector<vector<int>> matrix(64, vector<int>(64, 0));
vector<pair<int, int>> offsets = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
for (auto line : lines) {
int num = line[0];
for (int i = 1; i < line.size(); i += 2) {
int x = line[i];
int y = line[i + 1];
matrix[x][y] = num;
}
}
for (auto line : lines) {
int num = line[0];
int p = 0;
for (int i = 1; i < line.size(); i += 2) {
int x = line[i];
int y = line[i + 1];
for (auto offset : offsets) {
int newX = x + offset.first;
int newY = y + offset.second;
if (newX >= 0 && newX < 64 && newY >= 0 && newY < 64) {
if (matrix[newX][newY] != num) {
p += 1;
}
} else {
p += 1;
}
}
}
ans.push_back(p);
}
for (int i = 0; i < ans.size(); i++) {
cout << ans[i];
if (i != ans.size() - 1) {
cout << " ";
}
}
return 0;
}
```
请注意,转换过程中还进行了一些简单的优化,以提高代码的可读性和性能。
阅读全文