Description:
For a list of songs, you can perform the following three operations: 1. Select the previous song; 2. Select the next song; 3. Change the sorting method. After the sorting method is changed, the selected songs remain unchanged. It is sorted by title at the beginning, or can be switched between author sorting. The minimum number of operations required to select a specified song starts with a specified song.
Analysis:
It is also a question about the small number of AC users ~
If you can think of the breadth-first search, you can do it. We should also look at basic skills.
Nothing to write ~Send a post to commemorate~ :)
Paste the code, 0.05 s
----------------------------------------------------------------------------
Liu did not know how to deal with this question, and gave him a rough explanation of my code. I found out afterwards that I was surprised that I could come up with such a method to search for a wide variety of products ......
Generally, the wide search on a map has four directions?
I abstracted a song from one song to another into four directions, that is, two adjacent songs in the current sorting; two adjacent songs in the sorting order.
It takes 1 to move to the adjacent songs in the current sorting order, and 2 to move to the adjacent songs in the converted sorting order.
For (k = 0; k <4; k ++) tries to move in four directions, where 0 and 1 are the direction of the title sorting method, and 2 and 3 are the direction of the author sorting method.
F [x] [k] is the k/2 of the current song after X moves toward K, which means the K-direction sorting method, because the first two are both title directions, the last two are both author directions.
If (B [p] [2]! = K/2) s ++; if you need to convert the sorting method, add one for the number of moves
/*
Zju2031 song list
*/
# Include <stdio. h>
# Include <string. h>
# Include <stdlib. h>
# Define CLR (a) memset (A, 0, sizeof ())
# Define n 10005
Typedef struct {
Char * author;
Int num;
} Song;
Song [N];
Int f [N] [6];
Int B [N * 3] [3];
Int e [N], p, q;
Int CMP (const void * a, const void * B ){
Return strcmp (* (SONG *) a). Author, (* (SONG *) B). Author );
}
Int main ()
{
Int I, J, K, M, N, SF, St;
Int X, Y, S, flag;
For (I = 0; I <n; I ++ ){
Song [I]. Author = (char *) malloc (11 );
}
While (scanf ("% d", & n, & sf, & st )! = EOF)
{
// Input
For (I = 0; I <n; I ++ ){
Scanf ("% * s % s", song [I]. author );
Song [I]. num = I;
}
If (sf = st ){
Printf ("0/n ");
Continue;
}
Sf --; st --;
// Sort
Qsort (Song, N, sizeof (song [0]), CMP );
// Creat Map
For (I = 0; I <n; I ++ ){
F [I] [0] = I-1> = 0? I-1: n-1;
F [I] [1] = I + 1 <n? I + 1:0;
J = song [I]. Num;
F [J] [2] = I-1> = 0? Song [I-1]. Num: song [n-1]. Num;
F [J] [3] = I + 1 <n? Song [I + 1]. Num: song [0]. Num;
F [I] [4] = song [I]. Num;
F [song [I]. Num] [5] = I;
}
// Search
CLR (E );
P = 0; q = 1;
B [0] [0] = SF;
B [0] [1] = 0;
B [0] [2] = 0;
E [SF] = 1;
Flag = 1;
While (P <Q & flag ){
X = B [p] [0];
For (k = 0; k <4; k ++) {// each dir
Y = f [x] [k];
S = B [p] [1] + 1;
If (B [p] [2]! = K/2) s ++;
If (! E [y] | e [y]> s ){
E [y] = s;
B [q] [0] = y;
B [Q] [1] = s;
B [Q] [2] = K/2;
Q ++;
}
}
P ++;
}
Printf ("% d/N", E [st]);
}
Return 0;
}