zoj Frame Stacking 1083

拓扑排序

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stack>
#include <queue>
#include <math.h>
#include <cstdlib>
#include <cmath>
#include <map>
#include <algorithm>
using namespace std;
int h, w;
int all;
char frame[40][40];
vector<string> res;
vector<int> to[25];
map<int, int> in;
struct node {
	int lr = -1;
	int lc = -1;
	int rr = -1;
	int rc = -1;
};
map<int, node> mp;


void inti() {
	in.clear();
	res.clear();
	mp.clear();
	for (int i = 0; i < 25; i++) {
		to[i].clear();
	}

}

void bulid() {



	for (int i = 1; i <= h; i++) {
		for (int j = 1; j <= w; j++) {
			if (frame[i][j] == '.') continue;
			int c = frame[i][j] - 'A';
			if (mp.find(c) == mp.end()) {
				node d;
				mp[c] = d;
			}
			node nd = mp[c];
			if (nd.lr == -1) {
				nd.lr = i;
				nd.lc = j;
			} else {
				if (nd.lr > i) {
					nd.lr = i;
				}
				if (nd.lc > j) {
					nd.lc = j;
				}
			}

			if (nd.rr == -1) {
				nd.rr = i;
				nd.rc = j;
			} else {
				if (nd.rr < i) {
					nd.rr = i;
				}
				if (nd.rc < j) {
					nd.rc = j;
				}
			}
			mp[c] = nd;
		}
	}


	map<int, node>::iterator it = mp.begin();
	for (; it != mp.end(); it++) {
		int c = it->first;
		node nd = it->second;
		int lr = nd.lr;
		int lc = nd.lc;
		int rr = nd.rr;
		int rc = nd.rc;

		map<int, int> dir;
		//left
		for (int i = lr; i <= rr; i++) {
			int ch = frame[i][lc] - 'A';
			if (c != ch) {
				if (dir.find(ch) == dir.end()) {
					dir[ch] = 1;
					to[c].push_back(ch);
					in[ch]++;
				}
			}
		}

		//up
		for (int i = lc; i <= rc; i++) {
			int ch = frame[lr][i] - 'A';
			if (c != ch) {
				if (dir.find(ch) == dir.end()) {
					dir[ch] = 1;
					to[c].push_back(ch);
					in[ch]++;
				}
			}
		}

		//right
		for (int i = lr; i <= rr; i++) {
			int ch = frame[i][rc] - 'A';
			if (c != ch) {
				if (dir.find(ch) == dir.end()) {
					dir[ch] = 1;
					to[c].push_back(ch);
					in[ch]++;
				}
			}
		}

		//down
		for (int i = lc; i <= rc; i++) {
			int ch = frame[rr][i] - 'A';
			if (c != ch) {
				if (dir.find(ch) == dir.end()) {
					dir[ch] = 1;
					to[c].push_back(ch);
					in[ch]++;
				}
			}
		}

	}


}

void tpsort(vector<int> cur, map<int, int> newin, string ans) {
	sort(cur.begin(), cur.end());

	if (cur.size() == 0) {
		res.push_back(ans);
		return;
	}

	map<int, int> curin;
	for (int i = 1; i <= cur.size(); i++) {
		vector<int> t = cur;
		int coun = 1;
		vector<int>::iterator it = t.begin();
		for (; it != t.end(); it++) {
			if (coun == i) {
				t.erase(it);
				break;
			}
			coun++;
		}
		curin = newin;
		vector<int> des = to[cur[i - 1]];
		for (int j = 0; j < des.size(); j++) {
			if (--curin[des[j]] == 0) {
				t.push_back(des[j]);
			}
		}
		string nans = ans;
		nans += 'A' + cur[i - 1];
		tpsort(t, curin, nans);

	}
}

int main() {
	string str;
	while (scanf("%d%d", &h, &w) != EOF) {
		inti();
		for (int i = 1; i <= h; i++) {
			cin >> str;
			for (int j = 0; j < str.length(); j++) {
				frame[i][j + 1] = str[j];
				if (str[j] == '.') continue;
				int key = str[j] - 'A';
				if (in.find(key) == in.end()) {
					in[key] = 0;
				}
			}
		}


		bulid();

		vector<int> v;
		map<int, int>::iterator it = in.begin();
		for (; it != in.end(); it++) {
			if (it->second == 0) {
				v.push_back(it->first);
			}
		}

		tpsort(v, in, "");

		for (int i = 0; i < res.size(); i++) {
			cout << res[i] << endl;
		}

	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值