ZOJ-3314

CAPTCHA

Time Limit: 1000 MS Memory Limit: 32768 KB

64-bit integer IO format: %lld , %llu Java class name: Main

Description

A CAPTCHA (Completely Automated Public Turing Test to Tell Computers and Humans Apart) is a program that generates and grades tests that are human solvable, but intends to be beyond the capabilities of current computer programs. This technology is now almost a standard security mechanism for defending against undesirable or malicious Internet bot programs, such as those spreading junk emails and those grabbing thousands of free email accounts instantly.

It is hard for you to make a program to solve it. So, in this moment, you just need to solve a much easier problem.

111111MMM1111111   1MMMMMMMMMMM1111   11111MMMMMMMM111   1MMMMMMMMMMM1111   1MMMMMMMMMMMM111   1MMMMMMMMMMMMM11   
11111MM1MM111111   1MM11111111MM111   111MM1111111MM11   1MM111111111MM11   1MM1111111111111   1MM1111111111111   
1111MM111MM11111   1MM11111111MM111   11MM111111111MM1   1MM1111111111MM1   1MM1111111111111   1MM1111111111111   
111MMMMMMMMM1111   1MMMMMMMMMMM1111   11MM111111111111   1MM1111111111MM1   1MMMMMMMMMMMM111   1MMMMMMMMMMMMM11   
11MM1111111MM111   1MM11111111MM111   11MM111111111MM1   1MM1111111111MM1   1MM1111111111111   1MM1111111111111   
1MMM11111111MM11   1MM11111111MM111   111MM1111111MM11   1MM111111111MM11   1MM1111111111111   1MM1111111111111   
1MM1111111111MM1   1MMMMMMMMMMM1111   11111MMMMMMMM111   1MMMMMMMMMMM1111   1MMMMMMMMMMMM111   1MM1111111111111   

11111MMMMMMMM111   1MM111111111MM11   11111MMMMMM11111   1111MMMMMMMM1111   11MM111111MMM111   11MM111111111111   
111MM1111111MM11   1MM111111111MM11   1111111MM1111111   1111111MM1111111   11MM11111MMM1111   11MM111111111111   
11MM111111111MM1   1MM111111111MM11   1111111MM1111111   1111111MM1111111   11MM111MMM111111   11MM111111111111   
11MM111111111111   1MMMMMMMMMMMMM11   1111111MM1111111   1111111MM1111111   11MMMMM111111111   11MM111111111111   
11MM111111MMMMM1   1MM111111111MM11   1111111MM1111111   111MM11MM1111111   11MM111MMM111111   11MM111111111111   
111MM1111111MM11   1MM111111111MM11   1111111MM1111111   111MMM1MM1111111   11MM11111MMM1111   11MM111111111111   
11111MMMMMMMMM11   1MM111111111MM11   11111MMMMMM11111   11111MMMM1111111   11MM111111MMMM11   11MMMMMMMMMMMM11   

1MM1111111111MM1   1MMM111111111MM1   11111MMMMMM11111   1MMMMMMMMMMM1111   11111MMMMMM11111   1MMMMMMMMMMM1111   
1MMMM111111MMMM1   1MMMM11111111MM1   111MMM1111MMM111   1MM111111111MM11   111MMM1111MMM111   1MM111111111MM11   
1MM1MM1111MM1MM1   1MM1MM1111111MM1   11MMM111111MMM11   1MM1111111111MM1   11MMM111111MMM11   1MM1111111111MM1   
1MM11MMMMM111MM1   1MM11MM111111MM1   1MM1111111111MM1   1MM111111111MM11   1MM1111111111MM1   1MM111111111MM11   
1MM1111M11111MM1   1MM1111MM1111MM1   11MMM111111MMM11   1MMMMMMMMMMM1111   11MMM1MMMM1MMM11   1MMMMMMMMMMM1111   
1MM1111111111MM1   1MM111111MMM1MM1   111MMM1111MMM111   1MM1111111111111   111MMM11MMMMM111   1MM11111111MM111   
1MM1111111111MM1   1MM11111111MMMM1   11111MMMMMM11111   1MM1111111111111   111111MMMM1MMMM1   1MM111111111MMM1   

1111MMMMMMMM1111   11MMMMMMMMMMMM11   1MM1111111111MM1   1MMMM111111MMMM1   1MM1111111111MM1   11MMM111111MMM11   
111MM1111111MM11   11MMMMMMMMMMMM11   1MM1111111111MM1   11MMM111111MMM11   1MM1111111111MM1   111MMM1111MMM111   
11MMM1111111MMM1   1111111MM1111111   1MM1111111111MM1   11MMM111111MMM11   11MM111MM111MM11   1111MMM11MMM1111   
1111MMMMM1111111   1111111MM1111111   1MM1111111111MM1   111MMM1111MMM111   11MM111MM111MM11   111111MMMM111111   
1MMM111MMMM11111   1111111MM1111111   1MMM11111111MMM1   1111MMM11MMM1111   11MM111MM111MM11   1111MMM11MMM1111   
111MMM11111MMM11   1111111MM1111111   1MMM11111111MMM1   11111MM11MM11111   11MM1MM11MM1MM11   111MMM1111MMM111   
11111MMMMMMM1111   1111111MM1111111   111MMMMMMMMMM111   111111MMMM111111   111MMM1111MMM111   11MMM111111MMM11   

11MMM111111MMM11   111MMMMMMMMMM111   
111MMM1111MMM111   1111111111MM1111   
1111MMM11MMM1111   111111111MM11111   
111111MMMM111111   11111111MM111111   
1111111MM1111111   111111MM11111111   
1111111MM1111111   11111MM111111111   
1111111MM1111111   111MMMMMMMMMMM11   

Assume that the CAPTCHA only consists of upper letters. As you see above, we use a 7 * 16 matrix to represent a letter. The matrix only consists of characters 'M' and '1'. You can make sure that the 'M' elements in each letter matrix are connected. It means the all 'M' elements in each letter matrix belong to only ONE component. One element is connected to its 8 neighboring elements. That is, (x - 1, y), (x + 1, y), (x, y - 1), (x, y + 1), (x - 1, y - 1), (x + 1, y - 1), (x - 1, y + 1), (x + 1, y + 1) are (x, y)'s neighbors and they are connected.

And you will be given a bigger matrix, like this:

11111MMMMMM111
1111111MM11111
1MM1111MM11111
1MM1111MM11111
1MM1111MM11111
1MM1111MM11111
1MM11MMMMMM111
1MM11111111111
1MMMMMMMMMMMM1

Your task is to tell which letters appear in this matrix. The answer for this sample is 'I' and 'L'.

What's more, the letter appears in the bigger matrix may rotate 180 degrees, like this:

1MM1111111111MM11111111111111MM1
1MM1111111111MM11111111111111MM1
1MM1111111111MM11111111111111MM1
1MM1111111111MM111MMMMMMMMMMMMM1
1MMM11111111MMM11111111111111MM1
1MMM11111111MMM11111111111111MM1
111MMMMMMMMMM11111MMMMMMMMMMMMM1

'F' and 'U' is the answer for this sample.

OK, it's time for you to finish this work.

Input

There are multiple cases (no more than 10). In the first line, two integers n and m (7 <= n, m <= 300) will be given. Following n lines give the matrix. Each line contains m characters. Each character will be either 'M' or '1'. You may assume that 'M' characters between any pair of letters in the matrix won't be connected, and each 'M' in the matrix belongs to one valid letter.

There is a blank line between cases.

Output

Output the characters appear in the matrix. If a letter appears more than once, just output it ONE time. Sort the answer in alphabet order.

Sample Input

7 32
1MM1111111111MM11111111111111MM1
1MM1111111111MM11111111111111MM1
1MM1111111111MM11111111111111MM1
1MM1111111111MM111MMMMMMMMMMMMM1
1MMM11111111MMM11111111111111MM1
1MMM11111111MMM11111111111111MM1
111MMMMMMMMMM11111MMMMMMMMMMMMM1

15 19
11111MMMMMMMM111111
111MM1111111MM11111
11MM111111111MM1111
11MM111111111111111
11MM111111MMMMM1111
111MM1111111MM11111
11111MMMMMMMMM11111
1111111111111111111
1111MMM111111111MM1
1111MMMM11111111MM1
1111MM1MM1111111MM1
1111MM11MM111111MM1
1111MM1111MM1111MM1
1111MM111111MMM1MM1
1111MM11111111MMMM1

Sample Output

FU
GN
#include <iostream>
#include<string.h>
#include<stdio.h>
#include<queue>
#include<cmath>
#include<algorithm>
#include<set>
#include<stack>
#include<vector>
using namespace std;
int mmap[30] = {33,49,34,42,44,36,38,37,22,27,36,24,42,42,40,38,48,43,43,34,38,40,40,40,28,31};
int used[210][210];
char ch[210][210];

int move[8][2] = {-1,-1, -1,0, -1,1, 0,-1, 0,1, 1,-1, 1,0, 1,1};
int n,m;
int dfs(int x,int y)
{
    int ans = 1;
    for(int i=0; i<8; i++)
    {
        int tx = x + move[i][0];
        int ty = y + move[i][1];
        if(tx <0 || tx>=n || ty<0 || ty>=m || used[tx][ty]) continue;
        if(ch[tx][ty] == 'M')
        {
            used[tx][ty] = 1;
            ans += dfs(tx,ty);
        }
    }
    return ans;
}
int main()
{
#ifndef ONLINE_JUDGE
    freopen("in.txt","r",stdin);
#endif // ONLINE_JUDGE
    while(~scanf("%d %d",&n,&m))
    {
    memset(ch,'\0',sizeof(ch));
        for(int i=0; i<n; i++)
        {
            scanf("%s",ch[i]);
        }
        memset(used,0,sizeof(used));
        set<char>tt;
        int k;
        for(int i=0; i<n; i++)
        {
            for(int j=0; j<m; j++)
            {
                if(ch[i][j] == 'M' && used[i][j] == 0)
                {
                    used[i][j] = 1;
                    int res =  dfs(i,j);
                    if(res == 22) tt.insert('I');
                    else if(res == 24) tt.insert('L');
                    else if(res == 27) tt.insert('J');
                    else if(res == 28) tt.insert('Y');
                    else if(res == 31) tt.insert('Z');
                    else if(res == 33) tt.insert('A');
                    else if(res == 37) tt.insert('H');
                    else if(res == 44) tt.insert('E');
                    else if(res == 48) tt.insert('Q');
                    else if(res == 49) tt.insert('B');
                    else if(res == 34)            /// C T;
                    {
                        int tmp = 0;
                        for(k = j; ch[i][k] == 'M'; k++)
                        {
                            tmp++;
                        }
                        if(tmp == 8) tt.insert('C');
                        else tt.insert('T');
                    }
                    else if(res == 36)            ///F K
                    {
                        int tmp = 0;
                        for(k = j; ch[i][k] == 'M'; k++)
                        {
                            tmp++;
                        }
                        if(tmp == 4) tt.insert('K');
                        else if(tmp > 4) tt.insert('F');
                        else if(ch[i+3][j+2] == 'M') tt.insert('K');
                        else tt.insert('F');
                    }
                    else if(res == 38)              ///  G P U
                    {
                        int tmp = 0;
                        for(k = j; ch[i][k] == 'M'; k++)
                        {
                            tmp++;
                        }
                        if(tmp == 8 || tmp == 9) tt.insert('G');
                        else if(tmp == 11) tt.insert('P');
                        else if(tmp == 10) tt.insert('U');
                        else if(ch[i+6][j] == 'M') tt.insert('P');
                        else tt.insert('U');
                    }
                    else if(res == 40)            ///O V W X
                    {
                        int tmp = 0;
                        for(k = j; ch[i][k] == 'M'; k++)
                        {
                            tmp++;
                        }
                        if(tmp == 6) tt.insert('O');
                        else if(tmp == 4||tmp == 0) tt.insert('V');
                        else if(tmp == 2) tt.insert('W');
                        else if(ch[i+1][j] == 'M') tt.insert('W');
                        else tt.insert('X');
                    }
                    else if(res == 42)              /// D M N
                    {
                        int tmp = 0;
                        for(k = j; ch[i][k] == 'M'; k++)
                        {
                            tmp++;
                        }
                        if(tmp == 2) tt.insert('M');
                        else if(tmp == 11)tt.insert('D');
                        else  tt.insert('N');
                    }
                    else if(res == 43)             ///R  S
                    {
                        int tmp = 0;
                        for(k = j; ch[i][k] == 'M'; k++)
                        {
                            tmp++;
                        }
                        if(tmp == 7 || tmp == 8) tt.insert('S');
                        else tt.insert('R');
                    }
                }
            }
        }
        for(set<char>::iterator it = tt.begin(); it != tt.end(); ++it)
        {
            putchar(*it);
        }
        cout<<endl;
    }
    return 0;
}

 

posted on 2015-04-13 17:49  `Elaine  阅读(275)  评论(0编辑  收藏  举报

导航