Question: 11198-dancing digits
Each group of data has eight numbers, which may be positive or negative. It is required that the eight numbers be sorted in ascending order of the absolute values of the numbers. The sorting rule is to let a number a invite another number B to dance, so that a can be inserted to the left or right of B, and a can invite B to dance, then a * B <0, and A + B is a prime number. The question asks if you can sort by invitation and dance in a group of data. If you can, the minimum number of invitations will be output; otherwise,-1 will be output.
Solution: At first I thought about DFS, but later I found that this decision tree can be infinitely large, because a can invite B, and B is inviting, in this way, the original data may be retained. Implicit Graph traversal should be used, because the same State cannot be repeated and the minimum number of times is required. The implicit graph is BFS traversal, the path is the shortest path. In this case, you need to consider each number from the beginning. If you can dance with other numbers, you have to choose to stand on the left or right after the invitation is successful.
Note: insert it to the left of a certain number. The right side should also be determined based on the positions of the two numbers for dancing. Be careful.
Code:
# Include <stdio. h> # include <string. h> # include <stdlib. h> # include <math. h ># include <algorithm> using namespace STD; const int maxn = 1000005; const int n = 8; int State [maxn] [N], head [maxn], next [maxn], DIST [maxn]; bool CMP (const Int & A, const Int & B) {If (ABS (a) <ABS (B) return true; return false;} bool is_prime (int n) {for (INT I = 2; I <= SQRT (n); I ++) if (N % I = 0) return false; return true;} int Hash (INT rear) {int sum = 0; For (INT I = 0; I <n; I ++) sum = sum * 10 + ABS (State [rear] [I]); Return sum % maxn;} bool trytoinsert (INT rear) {int P = hash (rear ); int u = head [p]; while (u) {If (memcmp (State [rear], State [u], sizeof (State [u]) = 0) return false; u = next [u];} next [rear] = head [p]; head [p] = rear; return true ;} // insert to the left of a number or the right position of the number to be inserted in P1, the position of the number inserted in P2, Dir = 0 to the left, Dir = 1 to the right
Void change (INT P1, int P2, int front, Int & Rear, int DIR) {int temp = State [Front] [P1]; memcpy (State [rear], state [Front], sizeof (State [Front]); If (P1 <P2) {If (DIR = 0) {for (INT I = p1 + 1; I <P2; I ++) State [rear] [I-1] = State [rear] [I]; State [rear] [P2-1] = temp ;} else {for (INT I = p1 + 1; I <= P2; I ++) State [rear] [I-1] = State [rear] [I]; state [rear] [P2] = temp;} If (P1> P2) {If (DIR = 1) {For (INT I = p1; I> P2; I --) State [rear] [I] = State [rear] [I-1]; state [rear] [p2 + 1] = temp;} else {for (INT I = p1; I> = P2; I --) state [rear] [I] = State [rear] [I-1]; State [rear] [P2] = temp ;}// check whether the status is repeated, if (trytoinsert (rear) {Dist [rear] = DIST [Front] + 1; Rear ++ ;}} int BFS () {int front, rear; front = 1; Rear = 2; memset (Dist, 0, sizeof (DIST); memset (Head, 0, sizeof (head); While (front <r Ear) {If (memcmp (State [0], State [Front], sizeof (State [0]) = 0) return Dist [Front]; for (INT I = 0; I <n; I ++) {for (Int J = 0; j <n; j ++) {if (I! = J & State [Front] [I] * State [Front] [J] <0 & is_prime (ABS (State [Front] [I]) + ABS (State [Front] [J]) {change (I, j, front, rear, 0); change (I, j, front, rear, 1) ;}}} front ++;} return-1 ;}int main () {int T = 0; while (scanf ("% d ", & State [1] [0]), State [1] [0]) {for (INT I = 1; I <n; I ++) scanf ("% d", & State [1] [I]); memcpy (State [0], State [1], sizeof (State [1]); sort (State [0], State [0] + N, CMP); printf ("case % d: % d \ n", ++ T, BFs ());} return 0 ;}