POJ 2530 Tetris Alphabet

Description

The game of Tetris is played with four-connected blocks falling down the well having N rows and 20 columns. Each figure is marked with a unique English letter from 'A' to 'Z'. 
Your program must, given the state of the well, determine the order in which the blocks fell down.

Input

The first line of input file contains integer N (1 <= N <= 50) -- number of rows. Following N lines contain 20 characters each, with characters that are either a letter from 'A' to 'Z' or the dot character (ASCII 46), representing an empty cell.

Output

Output file must contain a string of letters indicating the order in which figures fell down. If there is more than one order, lexicographically smallest one must be printed. Input data will guarantee that at least one nonempty order exists.

Sample Input

6
...........XX.......
..........MMMM......
..........K.........
........KKK.........
.....ZAAA.FFF.......
.....ZZZA..F.B......

Sample Output

BFZAKMX


拓扑排序

#include<cstdio>
#include<iostream>
#include<cstring>
#include<map>
#include<queue>
#include<stack>
#include<algorithm>
#include<vector>
#include<cmath>
#include<string>
#include<functional>
using namespace std;
const int maxn=10005;
int T,n,f[30][30],c[30];
char s[55][30];

int main()
{
	while (cin>>n)
	{
		memset(f,0,sizeof(f));
		memset(c,-1,sizeof(c));
		for (int i=0;i<n;i++) 
		{
			scanf("%s",s[i]);
			for (int j=0;s[i][j];j++) 
				if (s[i][j]!='.'&&c[s[i][j]-'A']<0) c[s[i][j]-'A']=0;
			if (i) for (int j=0;s[i][j];j++)
				if (s[i][j]!='.'&&s[i-1][j]!='.')
					if (s[i][j]!=s[i-1][j])
					{
						 if (f[s[i][j]-'A'][s[i-1][j]-'A']==0)
						 {
							f[s[i][j]-'A'][s[i-1][j]-'A']=1;
							c[s[i-1][j]-'A']++;
						 }
					}
		}
		priority_queue<int,vector<int>,greater<int> > p;
		for (int i=0;i<26;i++)
			if (c[i]==0) p.push(i);
		while (!p.empty())
		{
			int k=p.top();
			printf("%c",k+'A');
			p.pop();
			for (int i=0;i<26;i++) 
				if (f[k][i]) 
				{
					c[i]--;
					if (c[i]==0) p.push(i);
				}	
		}
		printf("\n");
	}
	return 0;
}


你可能感兴趣的:(poj)