UVA - 11198 Dancing Digits(bfs+hash+线性表)
来源:互联网 发布:js 二维数组动态赋值 编辑:程序博客网 时间:2024/04/29 01:36
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 81 2 3 4 5 6 7 81 2 3 5 -4 6 7 81 2 3 5 4 6 7 82 -8 -4 5 6 7 3 -10
Output for the Sample Input
Case 1: 1Case 2: 0Case 3: 1Case 4: -1Case 5: 3
#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 - 11198 Dancing Digits(bfs+hash+线性表)
- uva 11198 - Dancing Digits 跳舞的数字 (BFS+HASH)
- 11198 - Dancing Digits(BFS + hash判重)
- UVA 11198 Dancing Digits (BFS + 判重)
- uva 11198 - Dancing Digits 隐式图 bfs
- uva 11198 - Dancing Digits
- UVA 11198 Dancing Digits
- UVA 11198 - Dancing Digits
- uva 11198 Dancing Digits
- UVA - 704 Colour Hash(双向bfs+hash+线性表)
- uva 11198 Dancing Digits(隐式图搜索+哈细判重)
- UVa 11198 - Dancing Digits,Rujia Liu的神题(二)
- UVa 11198 - Dancing Digits,Rujia Liu的神题(二)
- 11198 - Dancing Digits
- UVA - 310 L--system(bfs+hash)
- UVA 10651 Pebble Solitaire(bfs+hash)
- Colour Hash (Uva 704 双向bfs)
- uva 1341 - Different Digits(数论+bfs)
- UVA 11168 - Airport
- Implement a stack that pops out the mostfrequently added item. Stack supports 3 functions – push,
- 内核对象(VC_Win32) 挺好的~
- AJAX---jQuery的相关使用(一)
- java小九九的输出
- UVA - 11198 Dancing Digits(bfs+hash+线性表)
- html 的 meta 标签 简介及设定方式
- ruby byte转换bit
- java伪静态
- ubuntu vim中文乱码问题
- 文件描述符, 指针, 句柄,对象句柄
- linux下C获取文件的大小
- 项目日志(3)关于开漏、推挽、上拉的几点说明
- mysql统计一张表中条目个数的方法