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

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

HDU - 1430 魔板 (bfs預處理 + 康托)

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

       對于該題可以直接預處理初始狀態[0, 1, 2, 3, 4, 5, 6, 7]所有可以到達的狀態,保存到達的路徑,直接打印答案即可。

     關于此處的狀態轉換:假設有初始狀態為2,3,4,5,0,6,7,1,目標狀態為7,1,4,5,2,3,0,6,首先把初態看做0,1,2,3,4,5,6,7,那么目標狀態應該看做6,7,2,3,0,1,4,5直接查詢到達狀態6,7,2,3,0,1,4,5的路徑即可。

   PS:hdu3567與這題類似。

AC代碼:93ms

#include<cstdio>#include<cstring>#include<queue>#include<string>#include<iostream>#include<algorithm>using namespace std;typedef int state[8];const int maxn = 45000;int vis[maxn];string path[maxn];int op[][8] = {	4, 5, 6, 7, 0, 1, 2, 3,	3, 0, 1, 2, 7, 4, 5, 6,	0, 5, 1, 3, 4, 6, 2, 7};char dir[] = {'A', 'B', 'C'};int st[] = {0, 1, 2, 3, 4, 5, 6, 7};int fact[8];void deal() {  //1~8階乘打表,方便編碼 	fact[0] = 1;	for(int i = 1; i < 8; ++i) fact[i] = fact[i - 1] * i;}int KT(int *a) { //康托 	int code = 0;	for(int i = 0; i < 8; ++i) {		int cnt = 0;		for(int j = i + 1; j < 8; ++j) if(a[j] < a[i]) cnt++;		code += fact[7 - i] * cnt;	}	return code;} struct node{	int a[8];	int code;	node(){	}	node(int *b, int code):code(code) {		memcpy(a, b, sizeof(a));	}};void walk(int c, int *a) { //對數組進行第c種操作 	int b[8];	memcpy(b, a, sizeof(b));	for(int i = 0; i < 8; ++i) {		a[i] = b[op[c][i]];	}}void bfs() {  //預處理所有可以到達的狀態 	memset(vis, 0, sizeof(vis));	int code = KT(st);	vis[code] = 1;	path[code] = "";	queue<node>q;	q.push(node(st, code));	while(!q.empty()){		node p = q.front();		q.pop();		int a[8];		for(int i = 0; i < 3; ++i) { //三種操作 			memcpy(a, p.a, sizeof(a));			walk(i, a);			code = KT(a);			if(!vis[code]) {				vis[code] = 1;				path[code] = path[p.code] + dir[i];				q.push(node(a, code));			}		}	}}void deal(int *a) {	swap(a[4], a[7]);	swap(a[5], a[6]);}int main() {	deal();	bfs();	int a[8], b[8], ha[8];	char s[20];	while(scanf("%s", s) == 1) {		for(int i = 0; i < 8; ++i) {			a[i] = s[i] - '0' - 1;		}		deal(a);		for(int i = 0; i < 8; ++i) {			ha[a[i]] = i;		}		scanf("%s", s);		for(int i = 0; i < 8; ++i) {			b[i] = s[i] - '0' - 1;		}		deal(b);		for(int i = 0; i < 8; ++i) a[i] = ha[b[i]];		int code = KT(a);		cout << path[code] << endl;		//cout << "HELLO" <<endl;	}		return 0;}如有不當之處歡迎指出!


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 溆浦县| 英吉沙县| 北票市| 龙海市| 方山县| 乌拉特后旗| 桦川县| 广饶县| 伊春市| 讷河市| 塘沽区| 高陵县| 诸城市| 大名县| 河北区| 平乐县| 龙岩市| 永昌县| 高青县| 射洪县| 石景山区| 甘孜| 大洼县| 项城市| 六盘水市| 青浦区| 宿州市| 湛江市| 湄潭县| 马公市| 黔西| 贵定县| 双柏县| 云梦县| 阳泉市| 溧水县| 双柏县| 磐安县| 南昌市| 房山区| 沾化县|