CAPTCHA zju 校赛 acm

CAPTCHA

Time Limit: 1 Second      Memory Limit: 32768 KB

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<algorithm>
using namespace std;
char map[305][305];
bool flag[305][305];
char arr[300];
int n,m,num,si,sj;

int dir[8][2]={0,1,0,-1,1,0,-1,0,-1,1,-1,-1,1,1,1,-1};
int ncount;

void dry(int i,int j)
{
	int k;
	flag[i][j]=true;
	ncount++;
	for(k=0;k<8;k++)
	{
		if(map[i+dir[k][0]][j+dir[k][1]]=='M'&&flag[i+dir[k][0]][j+dir[k][1]]==false)
			dry(i+dir[k][0],j+dir[k][1]);
	}
}

void clear(int i,int j)
{
	int k;
	map[i][j]='1';
	ncount++;
	for(k=0;k<8;k++)
	{
		if(map[i+dir[k][0]][j+dir[k][1]]=='M')
			clear(i+dir[k][0],j+dir[k][1]);
	}
}

int search()
{
	int j;
	for(j=0;j<16;j++)
	{
		if(map[si][sj+j]=='M') ;
		else break;
	}
	return j;
}
char select34()
{
	if(search()==2||search()==12)
		return 'T';
	else return 'C';
}

char select42()
{
	if(search()==11)
		return 'D';
	else if (search()==2)
		return 'M';
	else return 'N';
}

char select36()
{
	if(search()==13)
		return 'F';
	if(search()==4)
		return 'K';
	else 
	{
		if(map[si+3][sj+2]=='M')
			return 'K';
		else return 'F';
	}
}

char select38()
{
	if(search()==8||search()==9)
		return 'G';
	else if (search()==11)
		return 'P';
	else if(search()==10)
		return 'U';
	else if(search()==2)
	{
		if(map[si+4][sj+2]=='M')
			return 'U';
		else return 'P';
	}
}

char select40()
{
	if(search()==2)
		return 'W';
	else if (search()==6)
		return 'O';
	else if(search()==4)
		return 'V';
	else if(search()==3)
	{
		if(map[si+1][sj-1]=='M')
			return 'W';
		return 'X';
	}
}

char select43()
{
	if(search()==11||search()==3)
		return 'R';
	else return 'S';
}

void trans(int number)
{
	switch(number)
	{
	case 33:
		arr[num++]='A';
		break;
	case 49:
		arr[num++]='B';
		break;
	case 44:
		arr[num++]='E';
		break;
	case 37:
		arr[num++]='H';
		break;
	case 22:
		arr[num++]='I';
		break;
	case 27:
		arr[num++]='J';
		break;
	case 48:
		arr[num++]='Q';
		break;
	case 31:
		arr[num++]='Z';
		break;
	case 28:
		arr[num++]='Y';
		break;
	case 24:
		arr[num++]='L';
		break;
	case 34:
		arr[num++]=select34();
		break;
	case 42:
		arr[num++]=select42();
		break;
	case 36:
		arr[num++]=select36();
		break;
	case 38:
		arr[num++]=select38();
		break;
	case 40:
		arr[num++]=select40();
		break;
	case 43:
		arr[num++]=select43();
		break;
	}
}



int main()
{
	int i,j;
	while(scanf("%d%d",&n,&m)!=EOF)
	{
		num=0; 
		for(i=0;i<=300;i++)
		{
			for(j=0;j<=300;j++)
			{
				flag[i][j]=false;
				map[i][j]='1';
			}
		}
		for(i=1;i<=n;i++)
		{
			getchar();
			for(j=1;j<=m;j++)
				scanf("%c",&map[i][j]);
		}
		for(i=1;i<=n;i++)
		{
			for(j=1;j<=m;j++)
			{ 
				if(map[i][j]=='M')
				{
					ncount=0;
					dry(i,j);
					si=i;
					sj=j;
					trans(ncount);
					clear(i,j);
				}
			}
		}
		sort(arr,arr+num);
		for(i=0;i<num;i++)
		{
			printf("%c",arr[i]);
			while(i+1<num && arr[i+1] == arr[i])
				i ++;
		}
		printf("/n");
	}
	return 0;
}
 

你可能感兴趣的:(CAPTCHA zju 校赛 acm)