-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalForward.cpp
More file actions
72 lines (62 loc) · 2.29 KB
/
calForward.cpp
File metadata and controls
72 lines (62 loc) · 2.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <iostream>
#include <vector>
#include <iomanip> // 用于设置输出精度
using namespace std;
// 函数:生成能量图
vector<vector<double>> generateEnergyMap(const vector<vector<int>>& A) {
int n = A.size(); // A的行数
int m = A[0].size(); // A的列数
vector<vector<double>> B(n, vector<double>(m, 0.0)); // 初始化B为全零矩阵
// 能量传播的偏移位置,8个方向(上下左右和四个对角线)
vector<pair<int, int>> directions = {
{-1, 0}, {1, 0}, {0, -1}, {0, 1}, // 上下左右
{-1, -1}, {-1, 1}, {1, -1}, {1, 1}, // 四个对角线
{-2,0},{2,0},{0,-2},{0,2}
};
// 遍历能量图A
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
if (A[i][j] == 1) { // 如果当前位置是1
// 为当前位置分配能量并向周围扩散
B[i][j]+=0.311;
for (const auto& dir : directions) {
int ni = i + dir.first;
int nj = j + dir.second;
if (ni >= 0 && ni < n && nj >= 0 && nj < m) { // 判断新位置是否在边界内
if (dir.first == 0 || dir.second == 0) { // 上下左右
if (abs(dir.first)==1 || abs(dir.second)==1){
B[ni][nj] += 0.125;
}
else if (abs(dir.first)==2 || abs(dir.second)==2){
B[ni][nj] += 0.05;
}
} else { // 对角线
B[ni][nj] += 0.05;
}
}
}
}
}
}
return B;
}
int main() {
// 示例输入矩阵A
vector<vector<int>> A = {
{0, 0, 0, 0, 0},
{0, 1, 1, 1, 0},
{0, 1, 1, 1, 0},
{0, 1, 1, 1, 0},
{0, 0, 0, 0, 0}
};
// 生成能量图B
vector<vector<double>> B = generateEnergyMap(A);
// 输出能量图B,保留三位小数
for (const auto& row : B) {
for (const auto& val : row) {
cout << fixed << setprecision(3) << val << " "; // 保留三位小数
}
cout << endl;
}
return 0;
}