cari

Rumah  >  Soal Jawab  >  teks badan

c++ - 病毒感染算法问题

PHP中文网PHP中文网2809 hari yang lalu799

membalas semua(3)saya akan balas

  • 巴扎黑

    巴扎黑2017-04-17 13:43:33

    不就是简单的元胞自动机模型?

    balas
    0
  • PHPz

    PHPz2017-04-17 13:43:33

    不用谢,已经通过的C++代码:

    include <iostream>

    include <fstream>

    include <memory.h>

    using namespace std;

    int main()
    {

    int n, m, k, i, j;
    //ifstream cin("test.txt");
    
    while (cin>>n>>m)
    {
        cin >> k;
        int max = k;
        int x = 0, y = 0, time = 1;
        int area[12][12];
        memset(area, 0, sizeof(area));
    
        for (i = 0;i < k; i++)
        {
            cin >> x >> y;
            area[x][y] = 1;
        }
    
        while(time++)
        {
            int num = 0;
            for (i = 1;i <= n;i++)
            {
                for (j = 1;j <= m;j++)
                {
                    //为6永远不会感染别人
                    if (area[i][j] >=1 && area[i][j] <=5)
                        area[i][j]++;   //我靠,程序半天没调出来,这句写成了area[x][y]++; fuck!!!
    
                    if (area[i][j] == 4)
                    {
                        if (area[i - 1][j] == 0) area[i - 1][j] = 1;
                        if (area[i + 1][j] == 0) area[i + 1][j] = 1;
                        if (area[i][j + 1] == 0) area[i][j + 1] = 1;
                        if (area[i][j - 1] == 0) area[i][j - 1] = 1;
                    }
                }
            }
    
            for (i = 1;i <= n; i++)
            {
                for (j = 1;j <= m;j++)
                {
                    if (area[i][j] >= 1 && area[i][j] <= 5)
                        num++;
                }
            }
                        
            if (num > max) max = num;
            if (num < k) break;
        }
    
        cout << max << endl;
    }
    
    return 0;

    }

    balas
    0
  • 阿神

    阿神2017-04-17 13:43:33

    如果有付费意愿可以加我Q343275968帮你写个算法

    balas
    0
  • Batalbalas