Mars Canals

1287. Mars Canals

Time limit: 1.0 second
Memory limit: 64 MB
There is a quadrate area on the Mars surface wreathed in ideal net of canals. They plot the surface into equal squares (we neglect here the surface curvature). Each side of the quadrate area plotted into N square regions.
Archeological investigations showed that there was an ancient country Yatik in this area. The inhabitants cultivated a special grain — sir — that was a basis of their food ration. There is sir of two kinds: coarse-grained and small-grained. As a matter of fact, the end of Yatik empire started after the civil war between the fanciers of the sir sorts. But until recently noone new which of the parties won that time. The scientists look forward to guess the riddle on the grounds of the last voyage to Mars results. They found out which kind of sir was sowed the last in each square of Yatik. According to the ancient tradition sir was sowed in the sequence of squares (parallel to the north-south or east-west directions or at the angle 45° to them), one may suppose that the supporters of the party-winner made the longest sowings.

Input

The first input line contains a size of the square area — N (1 ≤ N ≤ 1400). Then there follow Nlines. Each of them consists of N symbols. A letter “s” in the i-th line and j-th row means that in the according square region small-grained sir was sowed the last, a letter “S” means that coarse-grained sir was sowed the last. You may assume that the inhabitants of the area sowed nothing but sir. Each square region was sowed with only one sort of sir.

Output

The first line should contain a symbol “s”, if the party of small-grained sir fanciers won in the civil war. And symbol “S”, if the winners were the fanciers of the coarse-grained sir. If it’s impossible to define a winner then the first line should contain one symbol “?”. The second line should contain integer number — the maximal length of the one sort of sir sowing.

Samples

input output
3
SsS
sSs
SsS
S
3
2
sS
Ss
?
2

/** 
   DP:
   沒找到翻譯,自己寫一個簡單的題意:
   input之前都是科普,表示一羣土著爲了種糧食而戰= =!
   只有一句話重要:
   他們種植的植物是一行,或者一列,或者與邊成45度夾角的斜線,當然,會出現間斷的情況(這是因爲對手後來又在相同位置種了別的東西)。
   
   勝利的條件:S或者s的一方所形成的一行,或者一列,或者斜線的長度最長,如果等長,就分不出勝負
**/
#include<iostream>
#include<cstdio>
#include<cstring>

using namespace std;
#define maxn 1444

int Map[maxn][maxn];
int dp[maxn][maxn][4]; //45 0 90
int ans0,ans1;
int N;

void deal()//以點作爲階段進行劃分,每一個點有橫 豎 斜三種狀態,但由於斜分兩種,因此,實際上爲四種狀態
{
    for(int j=1; j<=N; j++)//這是三種狀態的轉移
        for(int k=1; k<=N; k++)
        {
            if(Map[j][k]==Map[j-1][k-1])
                dp[j][k][0]=dp[j-1][k-1][0]+1;
            else
                dp[j][k][0]=1;
            if(Map[j][k]==Map[j][k-1])
                dp[j][k][1]=dp[j][k-1][1]+1;
            else
                dp[j][k][1]=1;
            if(Map[j][k]==Map[j-1][k])
                dp[j][k][2]=dp[j-1][k][2]+1;
            else
                dp[j][k][2]=1;
            if(Map[j][k]==0)//每轉移一次記錄一下最大值
            {
                ans0=max(ans0,dp[j][k][0]);
                ans0=max(ans0,dp[j][k][1]);
                ans0=max(ans0,dp[j][k][2]);
            }
            else
            {
                ans1=max(ans1,dp[j][k][0]);
                ans1=max(ans1,dp[j][k][1]);
                ans1=max(ans1,dp[j][k][2]);
            }
        }
    for(int j=1; j<=N; j++)//這是第四種
        for(int k=N; k>=1; k--)
        {
             if(Map[j][k]==Map[j-1][k+1])
                dp[j][k][3]=dp[j-1][k+1][3]+1;
            else
                dp[j][k][3]=1;
            if(Map[j][k]==0)
                ans0=max(ans0,dp[j][k][3]);
            else
                ans1=max(ans1,dp[j][k][3]);
        }
}

void print()
{
    if(ans0>ans1)//最後,最大值進行比較,按照題目要求輸出
        printf("s\n%d\n",ans0);
    else if(ans0<ans1)
        printf("S\n%d\n",ans1);
    else
        printf("?\n%d\n",ans0);
}

int main()
{
    scanf("%d",&N);
    memset(Map,-1,sizeof(Map));//初始化地圖,-1的地方就是邊界
    getchar();
    for(int i=1; i<=N; i++)
    {
        for(int j=1; j<=N; j++)
        {
            char x;
            scanf("%c",&x);
            if(x=='S')//如此構圖
                Map[i][j]=1;
            else
                Map[i][j]=0;
        }
        getchar();
    }

    deal();
    print();
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章