ZOJ 2058 The Archaeologist's Trouble II(贪心+模拟)

【题目大意】

一个n高的塔,由@ * ?三种字符组成。每行相邻两个字符不能相邻。

'?' 表示未确定是 '@' 还是 '*' 。

求'@' 可能出现的最多和最少次数。

 

【分析】

在可以填的情况下 先填'@' 就是最多,先填 ' * '就是最少。

 

【代码】

#include <iostream>
#include <cstring>
using namespace std;
#define MAXN 100 + 100

int main()
{
    int n;
    while(cin >> n && n > 0)
    {
        char b[MAXN][MAXN], c[MAXN][MAXN];

        memset(b, ' ', sizeof(b));
        memset(c, ' ', sizeof(c));

        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= i; j++)
            {
                cin >> b[i][j];
                c[i][j] = b[i][j];
            }

        int num1 = 0, num2 = 0;
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= i; j++)
            {
                if (b[i][j] == '?' && b[i][j-1] != '@' && b[i][j+1] != '@')
                {
                    num1++;
                    b[i][j] = '@';
                }
                else if(b[i][j] == '@') num1++;
    
                if (c[i][j] == '?' && c[i][j-1] != '*' && c[i][j+1] != '*')
                {
                    num2++;
                    c[i][j] = '*';
                }
                else if(c[i][j] == '*') num2++;
            }

        cout << num1 <<" " <<n*(n+1)/2 - num2 << endl;
    }
} 

 

posted @ 2018-04-04 13:58  jvruodejrLS  阅读(141)  评论(0编辑  收藏  举报

Contact with me