ZJU2058The Archaeologist's Trouble II

来源:互联网 发布:北京东京 知乎 编辑:程序博客网 时间:2024/05/18 09:15
Problem Description
Lazy Genius, a Archaelogist, recently found a lot of ancient steles, on which there are only two kinds of characters, '*'and '@'. The characters formed a equilateral triangle on each steles. The triangle has N lines.The ith line contain i characters.

e.g. a triangle with 4 lines:

                     *                    * @                   * @ *                   @ * @ *

Through hard researchs, he found some rules of arrange of characters. There are only 4 styles of arranges of any three adjacent characters:

       *    @    @    *      * @, @ *, * @, @ *

Through hundreds of thousands of years, most of steles have become hard to identify. With the rules above, Mr.Genius recovered many steles. However, there are too many steles to be recovered by hand. Now Mr.Genius turns to you, a gifted programming student. He believes that you could help him.

Your task is to find out the maximum number and the minimum number of '@' of a given stele.


Input

The input contains several test cases.

The first line of each test block is a positive integer, N (1 < N <= 100), which represents the size of the triangle.

After that follows N lines. the ith lines contains i characters. The charatcter could only be '@', '*' or '?', where '?' represents the unclear characters.

The input is terminated by a negative integer.


Output

output a line contain two integers for each test block, the maximum and the minimum.


Sample Input

2
*
??
3
?
??
???
5
?
*@
?*?
????
?????
-1


Sample Output

1 1
4 2
9 7


题目大意:

一个字符三角形可能出现'@' ,'*' ,'?'三种字符。三角形中只允许出现四种不同的@*的组合。'?'表示该字符未知。给出一个三角形,要求字符@出现最多和最少的次数分别是多少。

分析:

允许的字符排列有以下四种:

@   @   ×   ×
@×, ×@, ×@, @×

观察发现,一个规律:三角形的字符是否合法与前一行无关。也就是说,判断这个三角形是否合法,只需一行一行判断即可。

在继续观察可以得出另一个规律:某一行字符合法的充要条件是,@和×交替出现。

这两个结论都不难得出。有了这两个结论,可以想到用一个贪心的策略来构造未知的字符。

因为一行字符一定是@和×交替出现,那么要使得@最多,可以另该行第一个字符为@,然后向后递推。之后的每一个字符都可以确定下来。若出现矛盾的情况,说明假设不成立,只能修改第一个字符为×。求@出现最少也是一样的。

#include <iostream>#include <cstring>using namespace std;int mi,ma;char other(char ch){    if(ch=='@') return '*';    else return '@';}int fillline(char a[][101],int i){    for(int j=1;j<=i;j++)    {        if(a[i][j]=='?') a[i][j]=other(a[i][j-1]);        else if(a[i][j]==a[i][j-1]) return 0;    }    return 1;}void fill(char a[][101],int n,char c){    char b[101];    for(int i=0;i<n;i++)    {        strcpy(b,a[i]);        if(a[i][0]=='?') a[i][0]=c;         if(!fillline(a,i))        {            strcpy(a[i],b);            a[i][0]=other(c);            fillline(a,i);        }    }}int count(char a[][101],int n){    int m=0;    for(int i=0;i<n;i++)        for(int j=0;j<=i;j++)            if(a[i][j]=='@') m++;    return m;}int main(){    char a[101][101],b[101][101];    int n;    while(cin>>n)    {        if(n<0) break;        for(int i=0;i<n;i++)        {            for(int j=0;j<=i;j++)            {                cin>>a[i][j];                b[i][j]=a[i][j];            }        }       fill(a,n,'@');       fill(b,n,'*');       ma=count(a,n);       mi=count(b,n);       cout<<ma<<" "<<mi<<endl;    }    return 0;}


0 0
原创粉丝点击