怎么把打表给调好啊
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <iomanip>
using namespace std;
const int smap_n = 30;
int smap[smap_n][smap_n] = {0}; //地图矩阵
void pd(int &x, int &y, int coord, int rows, int cols) {
bool unique;
do {
unique = true;
x = rand() % rows;
y = rand() % cols;
for (int i = 0; i < coord; ++i) {
if (smap[i][0] == x && smap[i][1] == y) {
unique = false;
break;
}
}
} while (!unique);
smap[coord][0] = x;
smap[coord][1] = y;
}
int main() {
int D_coordinate = 0, patient_x = 0, patient_y = 0, number = 0, kehidupan = 0; //number指3~5之间取随机值
double gailv = 0, num = 0, ans = 0;
cout << "你是一个边境小镇的镇长," << endl;
cout << "每天都会有暴乱,和受伤的" << endl;
cout << "民众,你需要派无人机解救" << endl;
cout << "他们!" << endl;
cout << "当前情况:" << endl;
srand((unsigned int)time(NULL)); // 设置随机种子
int t = rand() % 9 + 1;
number = rand() % 3 + 3;
for (int i = 0; i < t; i++) {
cout << "正常." << endl;
sleep(1); // 每次输出后延时1秒
}
cout << "发现受伤民众,无人机起飞!" << endl;
sleep(2);
int rows = rand() % smap_n + 1; // 1到10的随机行数
int cols = rand() % smap_n + 1; // 1到10的随机列数
// 初始化病人的生命倒计时数组
int grid[rows][cols] = {0};
int a[rows][cols] = {0};
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
grid[i][j] = 0;
a[i][j] = 0;
}
}
// for (int i = 0; i < rows; i++) {
// for (int j = 0; j < cols; j++) {
// a[i][j] = rand() % 11; // 生命值随机,包括0
// grid[i][j] = a[i][j];
// }
// }
for (int i = 0; i < number; i++) {
kehidupan = rand() % 6 + 15;
patient_x = rand() % rows ;
patient_y = rand() % cols ;
cout << patient_x << " " << patient_y << " " << kehidupan << endl;
if (kehidupan < 15 || kehidupan > 20) {
kehidupan = 15;
}
a[patient_x][patient_y] = kehidupan;
grid[patient_x][patient_y] = a[patient_x][patient_y];
}
// for (int i = 0; i < rows; i++) {
// for (int j = 0; j < cols; j++) {
// a[i][j] = rand() % 11; // 确保a被正确初始化
// grid[i][j] = a[i][j]; // 确保正确复制到grid
// cout << "grid[" << i << "][" << j << "] = " << grid[i][j] << endl;
// }
// }
// grid[2][6] = rand() % 11; // 生命值随机,包括0
// a[2][6] = grid[2][6];
// for (int i = 0; i < rows; i++) {
// for (int j = 0; j < cols; j++) {
// if (grid[i][j] == 0) {
// grid[i][j] = -1;
// }
// if(grid[i][j])
// }
// }
// 无人机起始位置
int x = rand() % rows;
int y = rand() % cols;
smap[0][0] = x;
smap[0][1] = y;
D_coordinate = 1;
cout << "准备移动无人机" << endl;
for (int i = 1; i <= rows * cols; i++) {
cout << "无人机起始位置:" << x << " " << y << endl;
cout << "初始病人范围:" << endl;
for (int j = 0; j < rows; j++) {
for (int k = 0; k < cols; k++) {
if (j == 0 && k == 0) {
cout << "┏ ";
} else if (j == rows - 1 && k == cols - 1) {
cout << "┛ " << endl;;
} else if (j == 0 && k == cols - 1) {
cout << "┗ ";
} else if (j == rows - 1 && k == 0) {
cout << "┓ " << endl;
} else if (k == 0 && j != 0 && j != rows - 1) {
cout << "┳ ";
} else if (j == 0 && k != 0 && k != cols - 1) {
cout << "┣ ";
} else if (j == rows - 1 && k != 0 && k != cols - 1) {
cout << "┫ " << endl;
} else if (k == cols - 1 && j != 0 && j != rows - 1) {
cout << "┻ ";
} else {
cout << "╋ ";
}
cout << setw(2) << grid[i][j] << " "; // 使用setw(3)设置宽度为3,并添加制表符
}
cout << endl;
}
// 无人机飞行过程中检查任务是否失败
bool missionFailed = false;
// 移动无人机遍历数组
for (int step = 0; step < rows * cols && !missionFailed; step++) {
// 每次移动,所有受伤民众的生命值减1
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (grid[i][j] > 0) {
grid[i][j]--;
if (grid[i][j] == 0) {
missionFailed = true;
}
}
}
}
// 如果无人机当前位置有民众,则救助
if (grid[x][y] > 0) {
grid[x][y] = -1;
}
// 移动无人机到下一个位置
y++;
if (y >= cols) {
y = 0;
x++;
if (x >= rows) {
x = 0; // 如果到达数组末尾,回到起始位置
}
}
cout << "无人机移动后:" << endl;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
// 调试输出
cout << "grid[" << i << "][" << j << "] = " << grid[i][j] << endl;
}
}
}
cout << "无人机飞行后的最终数组状态:" << endl;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
cout << grid[i][j] << " ";
}
cout << endl;
}
if (missionFailed) {
cout << "任务失败:有病人在无人机到达之前已经生命倒计时为零。" << endl;
ans++;
} else {
cout << "任务成功:所有病人已得到及时救助。" << endl;
num++;
ans++;
}
x = rand() % rows;
y = rand() % cols;
smap[D_coordinate][0] = x;
smap[D_coordinate][1] = y;
pd(x, y, D_coordinate, rows, cols);
smap[D_coordinate][0] = x; // 保存新的唯一坐标
smap[D_coordinate][1] = y;
D_coordinate++;
if (D_coordinate >= 10 ) { //smap_n - 1
break;
}
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
grid[i][j] = a[i][j];
}
}
// sleep(1);
}
gailv = num / ans;
cout << num << ans << endl;
cout << "任务成功概率:" << endl;
printf("%.2f", gailv * 100);
cout << "%" << endl;
return 0;
}