ACM summer training camp

Luo Guchun season camp (for me it is the same concentration camp life)

After playing with that big brother codeforces exchange, the valley also has original Los network class, so risking reported it, I feel Luo Valley teacher is very nice, I have to see a friend, _sys, chiefs, below oi is set to improve the inside of a question,

After the wrong change for a long time, to complete, ooo, ooo, my good food, child school students are

hdu1043

Eight digital problem, then live a good variety of solution, the teacher and I are not the same, is most people's online solution, Cantor expand + bfs

Well less nonsense, the codes

#include <the iostream>
#include <bits / STDC ++ H.>
the using namespace STD;
typedef struct NN
{
char Way; // record operation
int fath; // parent recording for recording the path
} node1;

NOD struct typedef
{
int aa [10];
int n-; // n-9 position is in the aa
int son; // record aa Concord expand open
} node2;

the dir int [. 4] [2] = {{0,1}, {0, -1}, {1,0}, {- 1,0}}, FAC [10];
node1 the Node [370000]; // node

void set_fac () // calculate the class 0-8
{
FAC [0] =. 1;
for (int I =. 1; I <=. 8; I ++)
FAC [I] FAC = [-I. 1] I *;
}

int cantor (int aa []) // Cantor expand, expanding health control method apart
{
int ANS = 0;
for (int I = 0; I <. 9; I ++)
{
int K = 0;
for (int J = I +. 1; J <. 9; J ++)
(AA IF [I]> AA [J])
K ++;
number after // i points from aa [i] smaller; ans + = k * fac [ 8-i] ! number * ((ni) -1) and the sum
}
return ANS;
}

void bfs(int a[])
{
queue<node2>Q;
node2 now,next;

for (int I = 0; I <. 9; I ++) now.aa [I] = A [I];
now.n =. 8; now.son = 0;
the Node [now.son] .fath = 0; // the ultimate parent node as 0, which is itself
Q.push (now);
(! Q.empty ()) the while
{
now Q.front = (); Q.pop ();
for (int = 0 K; K <. 4; K ++)
{
Next = now;
int = now.n TX / + the dir. 3 [K] [0];
int. 3% + TY = now.n the dir [K] [. 1];
IF (TY> = 0 && TX > = 0 && TY <&& TX. 3 <. 3)
{
next.n. 3 * TX + TY =; // get post-movement position x
int tem = next.aa [next.n]; // get the raw digital position after moving
next .aa [next.n] = next.aa [now.n];
next.aa [now.n] = TEM; // the two positions of the digital switching
next.son = cantor (next.aa);

if (Node [next.son] .fath == - 1) // indicates that the point is not accessed, is then placed in the queue -1
{
the Node [next.son] .fath = now.son; // current node the parent node is a node
if (k == 0) node [ next.son] .way = 'l'; // must pay attention, k = 0 is right away, but we are back from the ending state found, so a direct recording direction opposite
IF (K ==. 1) the Node [next.son] .way = 'R & lt';
IF (K == 2) the Node [next.son] .way = 'U';
IF ( ==. 3 K) the Node [next.son] .way = 'D';
Q.push (Next);
}
}
}
}
}
int main () {
int I, J, S, SS [10], A [10 ];
for (I = 0; I <. 9; I ++) // target state
a [i] = i + 1 ; // build certain one-dimensional matrix, as the x. 9
for (I = 0; I <370000; ++ I)
the Node [I] = -1 .fath;
set_fac (); // calculate stratum
bfs (a); // from the final state of all states and the corresponding path can extend out play table recorded in advance

STR char [50];
the while (the gets (STR))
{
for (I = 0, J = 0; STR [I] = '\ 0';! I ++) // number of sub-string into
{
IF (STR [ I] == 'x')
SS [J ++] =. 9; // the x becomes the number of sub. 9
the else IF (STR [I]> = '0' && STR [I] <= '. 8')
SS [J ++] = STR [I] - '0';
}
S = Cantor (SS); // initial state Cantor calculated value
if (Node [s] .fath == - 1) {printf ( "unsolvable \ n"); continue; } // can not become a target, because when the search from the start, fath represent the search to a certain
time // point, a point on the state, so if s.fath == - 1, it indicates that this matrix is simply It does not extend out, can not reach the target state of
the while (S = 0!)
{
the printf ( "% C", the Node [S] .way);
S = the Node [S] .fath;
}
the printf ( "\ n-");
}
}

Guess you like

Origin www.cnblogs.com/ssxzwwsjz/p/12222707.html