Home  >  Q&A  >  body text

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

PHP中文网PHP中文网2765 days ago771

reply all(3)I'll reply

  • 巴扎黑

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

    Isn’t it just a simple cellular automaton model?

    reply
    0
  • PHPz

    PHPz2017-04-17 13:43:33

    You’re welcome, passed C++ code:

    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;

    }

    reply
    0
  • 阿神

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

    If you are willing to pay, you can add me Q343275968 to help you write an algorithm

    reply
    0
  • Cancelreply