【发布时间】:2019-03-22 20:53:15
【问题描述】:
我有一个作业,我必须编写一个 C++ 程序来使用 SIR 模型(易感性、传染性、恢复)来模拟疾病爆发。要求是使用 7x7 大小的 2D 数组,用户将在其中选择 X 和 Y 坐标来初始化感染者。如果附近有感染者,易感者 (S) 将被感染 (I)。如果附近有 Recover 人,则感染者将恢复 (R)。如果所有人都康复,该计划将结束。 示例输出:
Day 0 Day 1 Day 2
s s s s s s s s s s s s s s s s s s s s s
s s s s s s s s s s s s s s s i i i i i s
s s s s s s s s s i i i s s s i r r r i s
s s s i s s s s s i r i s s s i r r r i s
s s s s s s s s s i i i s s s i r r r i s
s s s s s s s s s s s s s s s i i i i i s
s s s s s s s s s s s s s s s s s s s s s
到目前为止,我只能检查位置 (1,1), (1,7), (7,1), (7,7) 的状态。如果它旁边的下三个位置有感染者,它会将状态更新为 nextDayState。 到目前为止,这是我的两个函数的代码,SpreadingDisease 和 RecoverState。
void recoverState(char currentDayState[SIZE][SIZE], char nextDayState[SIZE][SIZE], int sizeOfArray)//It will take in the currentState of Day 0. I also copy the elements in currentState to nextDayState so that it could work.
{
for (int i = 1; i < sizeOfArray + 1; ++i)
{
for (int j = 1; j <= sizeOfArray + 1; ++j)
{
if (currentDayState[i][j] == 'i')//If found any Infected, update it to Recover on the nextDayState array.
{
nextDayState[i][j] == 'r';
}
}
}
for (int i = 1; i < sizeOfArray + 1; ++i)
{
for (int j = 1; j <= sizeOfArray + 1; ++j)
{
currentDayState[i][j] = nextDayState[i][j];
//After all people are recover, update the currentState and output it to terminal.
}
}
}
void spreadDisease(const char currentDayState[SIZE][SIZE], char nextDayState[SIZE][SIZE], int sizeOfArray, int day = 1)
{
for (int i = 1; i < sizeOfArray + 1; ++i)
{
for (int j = 1; j <= sizeOfArray + 1; ++j)
{
if (currentDayState[i][j] == 's')
{
if (i == 1 && j == 1)
{
if (currentDayState[1][2] == 'i' || currentDayState[2][1] == 'i' || currentDayState[2][2] == 'i')
{
nextDayState[1][1] = 'i';
}
}
if (i == 1 && j == 7)
{
if (currentDayState[1][6] == 'i' || currentDayState[2][6] == 'i' || currentDayState[2][7] == 'i')
{
nextDayState[1][7] = 'i';
}
}
if (i == 7 && j == 1)
{
if (currentDayState[6][1] == 'i' || currentDayState[6][2] == 'i' || currentDayState[7][2] == 'i')
{
nextDayState[7][1] = 'i';
}
}
if (i == 7 && j == 7)
{
if (currentDayState[6][6] == 'i' || currentDayState[7][6] == 'i' || currentDayState[6][7] == 'i')
{
nextDayState[7][7] = 'i';
}
}
}
}
}
}
我发现如果我能以某种方式从用户那里获得 X 和 Y 坐标,那么我可以使用该坐标来更新第二天的状态。不幸的是,我不知道如何将 X 和 Y 坐标分配到函数中以开始它。
P/S:感谢您的所有回答。我非常感谢你的好意。但是,我应该在之前提到我的任务要求。因为我只学习到用户定义的函数部分,所以我不能使用除此之外的任何东西。所以我仅限于使用 2D-array、If-else、Looping 来解决这个问题。地图和矢量现在远远超出我的知识范围 xD。
【问题讨论】:
标签: c++