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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值