问题:
http://acm.pku.edu.cn/JudgeOnline/problem?id=3087思路:
类似于模拟题的BFS,判重使用的是字符串哈希,哈希函数: ELFHash(见PKU 2503)
代码:
1 int
2 shuffle()
3 {
4 int i, count = 0;
5 int hash_val;
6 memset(hash, 0, sizeof(hash));
7 while(1) {
8 for(i=0; i<2*len; i++) {
9 if(i%2==0)
10 queue[count][i] = tmp2[i/2];
11 else
12 queue[count][i] = tmp1[i/2];
13 }
14 queue[count][i] = '\0';
15 strncpy(tmp1, queue[count], len);
16 strncpy(tmp2, queue[count]+len, len);
17 if(strcmp(queue[count], desire) == 0)
18 return count+1;
19 if(search(queue[count]) != -1)
20 return -1;
21 hash_val = ELFHash(queue[count]);
22 insert(hash_val, count);
23 ++count;
24 }
25 }