求助P2730
  • 板块学术版
  • 楼主zzxk
  • 当前回复0
  • 已保存回复0
  • 发布时间2022/9/29 21:42
  • 上次更新2023/10/27 09:30:32
查看原帖
求助P2730
469352
zzxk楼主2022/9/29 21:42

题目

#include <cstdio>
#include <iostream>
#include <map>
#include <cstring>
#include <string>       // string
using namespace std;

const int N = 4e6 + 10;
string x[N];
int step[N][3];     // 0 父亲下标 1 步数 2 变换类型 
int hh, tt;
string x1;
map <string, int> m;
map<string, int>::iterator kk;
string ans;

void add (string s, int ff, int b) {  // s 新状态 ff 父亲 b 变化类型 
	m[s] = tt;
	step[tt][0] = ff;
	step[tt][1] = step[ff][1] + 1;
	step[tt][2] = b;
	x[tt ++] = s;
}

void print (int cnt) {
	for (int i = cnt; step[i][2] != 0; i = step[i][0]) {
		if (step[i][2] == 1) ans += 'A';
		else if (step[i][2] == 2) ans += 'B';
		else ans += 'C';
	}
}

void bfs () {
	hh = tt = 1;
	add ("12348765", 0, 0);
	while (hh < tt) {
		string sh = x[hh ++];
		if (sh == x1) {
			printf ("%d\n", step[hh - 1][1] - 1);
			print (hh - 1);
			for (int i = step[hh - 1][1] - 2; i >= 0; i -- ) printf ("%c", ans[i]);
			return;
		}
		string s1, s2, s3;
		for (int i = 4; i < 8; i ++ ) s1 += sh[i];
		for (int i = 0; i < 4; i ++ ) s1 += sh[i];
		s2 += sh[3];
		for (int i = 0; i < 3; i ++ ) s2 += sh[i];
		s2 += sh[7];
		for (int i = 4; i < 7; i ++ ) s2 += sh[i];
		s3 = sh; s3[1] = sh[5], s3[2] = sh[1], s3[6] = sh[2], s3[5] = sh[6];
		if (m.find(s1) == m.end ()) add (s1, hh - 1, 1);
		if (m.find(s2) == m.end ()) add (s2, hh - 1, 2);
		if (m.find(s3) == m.end ()) add (s3, hh - 1, 3);
		//cout << s1 << endl << s2 << endl << s3 << endl << endl;
	}
} 

int main () {
	char c;
	for (int i = 0; i < 8; i ++ ) {
		scanf (" %c", &c);
		x1 += c;
	}
	//cout << x1;
	bfs ();
	
	return 0;
}
2022/9/29 21:42
加载中...