国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

HDU - 3567 Eight II (bfs預處理 + 康托) [kuangbin帶你飛]專題二

2019-11-08 19:24:51
字體:
來源:轉載
供稿:網友

      類似HDU1430,不過本題需要枚舉X的九個位置,分別保存狀態,因為要保證最少步數。要保證字典序最小的話,在擴展節點時,方向順序為:down, left, right, up.

     我用c++提交1500ms, G++提交858ms.

   AC代碼

#include<cstdio>#include<cstring>#include<queue>#include<string>#include<iostream>#include<algorithm>using namespace std;typedef int state[9];const int maxn = 4e5 + 5;int vis[9][maxn];const int dx[] = {1,0,0,-1};const int dy[] = {0,-1,1,0};const char dir[] = {'d','l','r','u'};int fact[9];void deal() {  //1~8階乘打表,方便編碼 	fact[0] = 1;	for(int i = 1; i < 9; ++i) fact[i] = fact[i - 1] * i;}int KT(int *a) {	int code = 0;	for(int i = 0; i < 9; ++i) {		int cnt = 0;		for(int j = i + 1; j < 9; ++j) if(a[j] < a[i]) cnt++;		code += fact[8 - i] * cnt;	}	return code;} int find_pos(int *a) {	for(int i = 0; i < 9; ++i) {		if(a[i] == 0) return i;	}}struct node{	int a[9];	int code, step;	int pos;	node() {	}	node(int *b, int code, int step ,int pos):code(code), step(step), pos(pos) {		memcpy(a, b, sizeof(a));	}};struct Dirction {	char dir;	int step;	int PRe;	Dirction() {	}	Dirction(char dir, int step, int pre):dir(dir), step(step), pre(pre){	}}d[9][maxn];int st[9];void bfs(int c) {	queue<node>q;	int code = KT(st);	vis[c][code] = 1;	d[c][code] = Dirction('x', 0, -1);	int pp = find_pos(st);	q.push(node(st, code, 0, pp));	while(!q.empty()) {		node p = q.front();		q.pop();		state &a = p.a;		int pos = p.pos;		int x = pos / 3, y = pos % 3;		for(int i = 0; i < 4; ++i) {			int px = x + dx[i], py = y + dy[i];			if(px < 0 || py < 0 || px >= 3 || py >= 3) continue;			int pz = px * 3 + py;			swap(a[pz], a[pos]);			code = KT(a);			if(vis[c][code]) {				swap(a[pz], a[pos]);				continue;			}			vis[c][code] = 1;			d[c][code] = Dirction(dir[i], p.step + 1, p.code);			q.push(node(a, code, p.step + 1, pz));			swap(a[pz], a[pos]);		}	}}int op[][9] = {	0, 1, 2, 3, 4, 5, 6, 7, 8,	1, 0, 2, 3, 4, 5, 6, 7, 8,	1, 2, 0, 3, 4, 5, 6, 7, 8,	1, 2, 3, 0, 4, 5, 6, 7, 8,	1, 2, 3, 4, 0, 5, 6, 7, 8,	1, 2, 3, 4, 5, 0, 6, 7, 8,	1, 2, 3, 4, 5, 6, 0, 7, 8,	1, 2, 3, 4, 5, 6, 7, 0, 8,	1, 2, 3, 4, 5, 6, 7, 8, 0};void print(int code, int c) {	if(d[c][code].pre == -1) return;	print(d[c][code].pre, c);	printf("%c", d[c][code].dir);}int main() {	memset(vis, 0, sizeof(vis));	deal();	for(int i = 0; i < 9; ++i) {		memcpy(st, op[i], sizeof(st));		bfs(i);	}	int a[9], b[9];	int T, kase = 1, ha[9];	char s[20]; 	scanf("%d", &T);	while(T--) {		int pos;		scanf("%s", s);		for(int i = 0; i < 9; ++i) {			if(s[i] == 'X') {				pos = i;				a[i] = 0;			}			else a[i] = s[i] - '0';		}		for(int i = 0; i < 9; ++i) {			ha[a[i]] = op[pos][i];		}		scanf("%s", s);		for(int i = 0; i < 9; ++i) {			if(s[i] == 'X') b[i] = 0;			else b[i] = s[i] - '0';			a[i] = ha[b[i]];		}		int code = KT(a);		printf("Case %d: %d/n", kase++, d[pos][code].step);		print(code, pos);		printf("/n");	}		return 0;}如有不當之處歡迎指出!


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 商南县| 胶州市| 阳谷县| 满洲里市| 高青县| 平罗县| 崇礼县| 沂水县| 石河子市| 红原县| 德庆县| 万山特区| 介休市| 西城区| 凤翔县| 长海县| 阿克陶县| 凤庆县| 桐乡市| 溧阳市| 河池市| 台南县| 柞水县| 永年县| 溆浦县| 乡宁县| 嘉黎县| 祥云县| 杭锦后旗| 柳河县| 商城县| 青川县| 张掖市| 玉树县| 龙里县| 林口县| 阳谷县| 岳阳县| 凌云县| 敦煌市| 阿坝县|