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;
}
时间: 2024-10-11 07:24:19

ZOJ-3314的相关文章

zoj 3314 CAPTCHA(纯模拟)

题目 有些人用深搜写的,当然我这弱弱的,只理解纯模拟... 纯模拟,第一次写了那么长的代码,我自己也是够坚韧不拔的,,,,必须留念啊!!! 注意,G包含C,E包含L,R包含P,(照图说O应该不包含C,但是不排除掉这种情况,就wa掉了,所以要排除O包含C的情况..) #include<stdio.h> #include<string.h> int n,m; char s2[330][330]; int vis[30]; void fun(){ //a if(vis[0]==0){ f

概率dp ZOJ 3640

Help Me Escape Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Status Practice ZOJ 3640 Appoint description:  System Crawler  (2014-10-22) Description Background     If thou doest well, shalt thou not be accepted? an

zoj 2156 - Charlie&#39;s Change

题目:钱数拼凑,面值为1,5,10,25,求组成n面值的最大钱币数. 分析:dp,01背包.需要进行二进制拆分,否则TLE,利用数组记录每种硬币的个数,方便更新. 写了一个 多重背包的 O(NV)反而没有拆分快.囧,最后利用了状态压缩优化 90ms: 把 1 cents 的最后处理,其他都除以5,状态就少了5倍了. 说明:貌似我的比大黄的快.(2011-09-26 12:49). #include <stdio.h> #include <stdlib.h> #include <

ZOJ 1718 POJ 2031 Building a Space Station 修建空间站 最小生成树 Kruskal算法

题目链接:ZOJ 1718 POJ 2031 Building a Space Station 修建空间站 Building a Space Station Time Limit: 2 Seconds      Memory Limit: 65536 KB You are a member of the space station engineering team, and are assigned a task in the construction process of the statio

ZOJ 3607 Lazier Salesgirl (贪心)

Lazier Salesgirl Time Limit: 2 Seconds      Memory Limit: 65536 KB Kochiya Sanae is a lazy girl who makes and sells bread. She is an expert at bread making and selling. She can sell the i-th customer a piece of bread for price pi. But she is so lazy

ZOJ - 2243 - Binary Search Heap Construction

先上题目: Binary Search Heap Construction Time Limit: 5 Seconds      Memory Limit: 32768 KB Read the statement of problem G for the definitions concerning trees. In the following we define the basic terminology of heaps. A heap is a tree whose internal n

ZOJ 2859 二维线段树

思路:自己写的第二发二维线段树1A,哈哈,看来对二维的push操作比较了解了:但是还没遇到在两个线段树中同时进行push操作的,其实这题我是想在x维和y维同时进行push操作的,但是想了好久不会,然后看到这题又给出10秒,然后想想在x维线段直接单点查询肯定也过了,然后在第二维就只有pushup操作,在第一维线段树没有pushup操作.要是在第一维也有pushup操作的话,那就不用单点查询那么慢了.不过也A了,想找题即在二维同时进行pushup和pushdown操作的. #include<iost

ZOJ 2588

求一个无向图的桥(可能存在重边),输出割边的数目,并按顺序输出割边的序号(输入的顺序). 由于内存的限制 , 无法使用邻接矩阵 , 只能用邻接表了 . 第一次用了邻接表,超内存了: 1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 #include <string.h> 5 using namespace std; 6 const int N=10002; 7 const i

ZOJ 2587 Unique Attack 判断最小割是否唯一

很裸的判断最小割是否唯一.判断方法是先做一遍最大流求最小割,然后从源点和汇点分别遍历所有能够到达的点,看是否覆盖了所有的点,如果覆盖了所有的点,那就是唯一的,否则就是不唯一的. #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <climits> #include <string> #include <iostr

ZOJ 3794 Greedy Driver spfa

题意: 给定n个点,m条有向边,邮箱容量. 起点在1,终点在n,开始邮箱满油. 下面m行表示起点终点和这条边的耗油量(就是长度) 再下面给出一个数字m表示有P个加油站,可以免费加满油. 下面一行P个数字表示加油站的点标. 再下面一个整数Q 下面Q行 u v 表示在u点有销售站,可以卖掉邮箱里的任意数量的油,每以单位v元. 问跑到终点能获得最多多少元. 先求个每个点的最大剩余油量 f[i], 再把边反向,求每个点距离终点的最短路 dis[i]. 然后枚举一下每个销售点即可,( f[i] - dis