UVA - 11198 Dancing Digits(bfs+hash+线性表)

Problem D

Dancing Digits

Digits like to dance. One day, 1, 2, 3, 4, 5, 6, 7 and 8 stand in a line to have a wonderful party. Each time, a male digit can ask a female digit to dance with him, or a female digit can ask a male digit to dance with her, as long as their sum is a prime. Before every dance, exactly one digit goes to who he/she wants to dance with - either to its immediate left or immediate right.

For simplicity, we denote a male digit x by itself x, and denote a female digit x by -x. Suppose the digits are in order {1, 2, 4, 5, 6, -7, -3, 8}. If -3 wants to dance with 4, she must go either to 4's left, resulting {1, 2, -3, 4, 5, 6, -7, 8} or his right, resulting {1, 2, 4, -3, 5, 6, -7, 8}. Note that -3 cannot dance with 5, since their sum 3+5=8 is not a prime; 2 cannot dance with 5, since they're both male.

Given the initial ordering of the digits, find the minimal number of dances needed for them to sort in increasing order (ignoring signs of course).

Input

The input consists of at most 20 test cases. Each case contains exactly 8 integers in a single line. The absolute values of these integers form a permutation of {1, 2, 3, 4, 5, 6, 7, 8}. The last case is followed by a single zero, which should not be processed.

Output

For each test case, print the case number and the minimal number of dances needed. If they can never be sorted in increasing order, print -1.

Sample Input

1 2 4 5 6 -7 -3 8
1 2 3 4 5 6 7 8
1 2 3 5 -4 6 7 8
1 2 3 5 4 6 7 8
2 -8 -4 5 6 7 3 -1
0

Output for the Sample Input

Case 1: 1
Case 2: 0
Case 3: 1
Case 4: -1
Case 5: 3

题目大意:1,2,3,4,5,6,7,8去参加舞会,其中正数表示男性,负数表示女性,男性可以和女性进行搭配,即男性可以移动到女性的左边或者右边,女性也可以移动到男性的左边或者右边,但是男性和女性数字的绝对值相加必须为素数。问最少需要移动多少步,才可以移动到所有数字 绝对值满足单调递增。

解析:这题主要用到bfs+hash的方法,bfs用于枚举出所有的可能情况,而hash用于进行判重。
注意在bfs的过程用要对数字进行移动操作,这需要用到线性表的知识。

#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <set>
using namespace std;
const int MAXSTATE = 1000000;
typedef int State[8];
State st[MAXSTATE];
int dist[MAXSTATE];

int prime[20];
set<int> vis;
void isPrime() {
	memset(prime,0,sizeof(prime));
	prime[2] = 1;prime[3] = 1;
	prime[5] = 1;prime[7] = 1;
	prime[11] = 1;prime[13] = 1;
	prime[17] = 1;prime[19] = 1;
}
void init_look_table() {
	vis.clear();
}
bool try_to_insert(int s) {
	int v = 0;
	for(int i = 0; i < 8; i++) {
		v = v * 10 + abs(st[s][i]);
	}
	if(vis.count(v)) {
		return false;
	}
	vis.insert(v);
	return true;
}
bool judge(State& s) {
	for(int i = 0; i < 7; i++) {
		if(abs(s[i]) > abs(s[i+1])) {
			return false;
		}
	}
	return true;
}
bool move(State& s,int u,int v,int k) {
	int tmp;
	switch(k) {
		case 0: //将u移动到v的左边
			if(u == v-1) { //如果u恰好在v的左边,则不需要移动
				return false;
			}
			if(u == v+1) { //如果u在v的右边,则两个数字交换位置
				tmp = s[u];
				s[u] = s[v];
				s[v] = tmp;
				return true;
			}
			tmp = s[u];
			if(u > v) {
				for(int i = u-1; i >= v; i--) {
					s[i+1] = s[i];
				}
				s[v] = tmp;
			}else {
				for(int i = u; i < v-1; i++) {
					s[i] = s[i+1];
				}
				s[v-1] = tmp;
			}
			return true;
		case 1: //将u移动到v的右边
			if(u == v+1) {
				return false;
			}
			if(u == v-1) {
				tmp = s[u];
				s[u] = s[v];
				s[v] = tmp;
				return true;
			}
			tmp = s[u];
			if(u > v) {
				for(int i = u-1; i >= v+1; i--) {
					s[i+1] = s[i];
				}
				s[v+1] = tmp;
			}else {
				for(int i = u; i < v; i++) {
					s[i] = s[i+1];
				}
				s[v] = tmp;
			}
			return true;
	}
	return false;
}
int bfs() {
	init_look_table();
	int front = 1,rear = 2;
	dist[front] = 0;
	while(front < rear) {
		State& s = st[front];
		if(judge(s)) {
			return dist[front];
		}
		for(int i = 0; i < 8; i++) {
			for(int j = 0; j < 8; j++) {
				if(i == j || s[i] * s[j] > 0) {
					continue;
				}
				int sum = abs(s[i]) + abs(s[j]);
				if(!prime[sum]) {
					continue;
				}
				for(int k = 0; k < 2; k++) {
					State& t = st[rear];
					memcpy(&t,&s,sizeof(s));
					if(move(t,i,j,k)) {
						if(try_to_insert(rear)) {
							dist[rear] = dist[front] + 1;
							rear++;
						}
					}
				}
			}
		}
		front++;
	}
	return -1;
}
int main() {
	int cas = 1;
	isPrime();
	while(scanf("%d",&st[1][0]) != EOF && st[1][0]) {
		for(int i = 1; i < 8; i++) {
			scanf("%d",&st[1][i]);
		}
		int ans = bfs();
		printf("Case %d: %d\n",cas++,ans);
	}
	return 0;
}

你可能感兴趣的:(uva,digits,Dancing,11198)