1 条题解

  • 0
    @ 2023-7-6 14:27:19

    三维数组

    #include <bits/stdc++.h>
    using namespace std;
    int n, m;
    // [天数][行][列]
    char g[105][105][105];
    int main()
    {
        ios::sync_with_stdio(false);
        cin.tie(0);
        cin >> n;
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= n; j++)
                cin >> g[1][i][j];
        cin >> m;
        for (int day = 2; day <= m; day++)
        {
            for (int i = 1; i <= n; i++)
                for (int j = 1; j <= n; j++)
                {
                    if (g[day - 1][i][j] == '#')
                        g[day][i][j] = '#';
                    else if (g[day - 1][i - 1][j] == '@' ||
                             g[day - 1][i][j - 1] == '@' || g[day - 1][i][j] == '@' || g[day - 1][i][j + 1] == '@' ||
                             g[day - 1][i + 1][j] == '@')
                        g[day][i][j] = '@';
                }
        }
        int cnt = 0;
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= n; j++)
                if (g[m][i][j] == '@')
                    cnt++;
        cout << cnt << "\n";
        return 0;
    }
    

    两个二维数组滚动

    #include <bits/stdc++.h>
    using namespace std;
    int n, m;
    // [天数][行][列]
    char a[105][105];
    char b[105][105];
    int main()
    {
        ios::sync_with_stdio(false);
        cin.tie(0);
        cin >> n;
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= n; j++)
                cin >> a[i][j];
        cin >> m;
        for (int day = 2; day <= m; day++)
        {
            for (int i = 1; i <= n; i++)
                for (int j = 1; j <= n; j++)
                {
                    if (a[i][j] == '#')
                        b[i][j] = '#';
                    else if (a[i - 1][j] == '@' ||
                             a[i][j - 1] == '@' || 
    						 a[i][j] == '@' || 
    						 a[i][j + 1] == '@' ||
                             a[i + 1][j] == '@')
                        b[i][j] = '@';
                }
            for(int i=1;i<=n;i++)
            	for(int j=1;j<=n;j++)
            		a[i][j] = b[i][j];
    	}
        int cnt = 0;
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= n; j++)
                if (a[i][j] == '@')
                    cnt++;
        cout << cnt << "\n";
        return 0;
    }
    

    另一种滚动形式

    #include <bits/stdc++.h>
    using namespace std;
    int n, m;
    // [天数][行][列]
    char a[2][105][105];
    int now,last;//当前天、前一天 
    int main()
    {
        ios::sync_with_stdio(false);
        cin.tie(0);
        cin >> n;
        now = 1;
        last = 0;
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= n; j++)
                cin >> a[now][i][j];
        cin >> m;
        last = now;
        now = 1-last;
        for (int day = 2; day <= m; day++)
        {
            for (int i = 1; i <= n; i++)
                for (int j = 1; j <= n; j++)
                {
                    if (a[last][i][j] == '#')
                        a[now][i][j] = '#';
                    else if (a[last][i - 1][j] == '@' ||
                             a[last][i][j - 1] == '@' || 
    						 a[last][i][j] == '@' || 
    						 a[last][i][j + 1] == '@' ||
                             a[last][i + 1][j] == '@')
                        a[now][i][j] = '@';
                }
    	    last = now;
    	    now = 1-last;
    	}
        int cnt = 0;
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= n; j++)
                if (a[last][i][j] == '@')
                    cnt++;
        cout << cnt << "\n";
        return 0;
    }
    
    • 1

    信息

    ID
    411
    时间
    1000ms
    内存
    256MiB
    难度
    7
    标签
    (无)
    递交数
    85
    已通过
    19
    上传者