UVA 11198 Dancing Digits (BFS + 判重)

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-8,8个数字。正数为男。 负数为女。 如果一个数和另一个数的绝对值之和为素数且两数性别不同,则可以把这个数插到另一个数的左边或右边。。要求的是序列变成1 2 3 4 5 6 7 8(不分正负)最少的步骤。。

思路:状态记录的BFS 。。我用map标记重复的状态。然后进行广搜。直到出现12345678.。中间性别是另外开了一个数组来进行标记的。。

代码:

#include <stdio.h>
#include <string.h>
#include <string>
#include <map>
#include <queue>
using namespace std;

int sb;
int ok;
int judge;
int sex[9];
char star[10];
int pri[20];
struct QUE {
    char status[10];
    int step;
} q, p;
map<string, int> vis;
queue<QUE> Q;

void tra(int n, int m) {
    char sb[30];
    int j = 0;
    for (int i = 0; i < 17; i ++) {
	if (i % 2) sb[i] = q.status[j ++];
	else sb[i] = '*';
    }
    sb[2 * m] = q.status[n];
    sb[2 * n + 1] = '*';
    j = 0;
    for (int i = 0; i < 17; i ++) 
	if (sb[i] != '*') 
	    q.status[j ++] = sb[i];
    q.status[j] = '\0';
}
void bfs() {
    vis.clear();
    while (!Q.empty()) {Q.pop();}
    q.step = 0;
    strcpy(q.status, star);
    vis[q.status] = 1;
    Q.push(q);
    while (!Q.empty()) {
	p = Q.front();
	if (strcmp(p.status, "12345678") == 0) {
	    judge = 1;
	    return;
	}
	Q.pop();
	for (int i = 0; i < 8; i ++)
	    for (int j = 0; j <= 8; j ++) {
		ok = 0;
		q = p;
		if (i == j || i + 1 == j) continue;
		if (j == 0) {
		    int num2 = q.status[i] - '0';
		    int num3 = q.status[0] - '0';
		    if (!pri[num2 + num3] || sex[num2] * sex[num3] > 0)
			continue;
		}
		if (j == 8) {
		    int num2 = q.status[i] - '0';
		    int num1 = q.status[7] - '0';
		    if (!pri[num2 + num1] || sex[num2] * sex[num1] > 0)
			continue;
		}
		int num2 = q.status[i] - '0';
		int num1 = q.status[j - 1] - '0';
		int num3 = q.status[j] - '0';
		if ((pri[num2 + num3] && sex[num2] * sex[num3] < 0) || (pri[num1 + num2] && sex[num1] * sex[num2] < 0))
		    ok = 1;
		if (!ok)
		    continue;
		tra(i, j);
		if (vis[q.status] == 0) {
		    vis[q.status] = 1;
		    q.step ++;
		    Q.push(q);
		}
	    }
    }
}
void start() {
    if (sb < 0) {
	sex[-sb] = -1;
	star[0] = -sb + '0';
    }
    else {
	sex[sb] = 1;
	star[0] = sb + '0';
    }
    for (int i = 1; i <= 7; i ++) {
	scanf("%d", &sb);
	if (sb < 0) {
	    sex[-sb] = -1;
	    star[i] = -sb + '0';
	}
	else {
	    sex[sb] = 1;
	    star[i] = sb + '0';
	}
    }
    star[8] = '\0';
    judge = 0;
}


int main() {
    int t = 1;
    pri[2] = pri[3] = pri[5] = pri[7] = pri[11] = pri[13] = 1;
    while (~scanf("%d", &sb) && sb) {
	start();
	bfs();
	if (judge)
	    printf("Case %d: %d\n", t ++, p.step);
	else printf("Case %d: -1\n", t ++, p.step);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值