UVa 704 - Colour Hash, 双向bfs,很给力

2023-12-10 04:58
文章标签 bfs 双向 hash uva 704 colour

本文主要是介绍UVa 704 - Colour Hash, 双向bfs,很给力,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

链接:

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=110&page=show_problem&problem=645


类型:隐式图搜索,双向bfs


原题:

This puzzle consists of two wheels. Both wheels can rotate both clock and counter-clockwise. They contain 21 coloured pieces, 10 of which are rounded triangles and 11 of which are separators. Figure 1 shows the final position of each one of the pieces. Note that to perform a one step rotation you must turn the wheel until you have advanced a triangle and a separator.


Figure 1. Final puzzle configuration

Your job is to write a program that reads the puzzle configuration and prints the minimum sequence of movements required to reach the final position. We will use the following integer values to encode each type of piece:


0grey separator
1yellow triangle
2yellow separator
3cyan triangle
4cyan separator
5violet triangle
6violet separator
7green triangle
8green separator
9red triangle
10red separator


A puzzle configuration will be described using 24 integers, the first 12 to describe the left wheel configuration and the last 12 for the right wheel. The first integer represents the bottom right separator of the left wheel and the next eleven integers describe the left wheel clockwise. The thirteenth integer represents the bottom left separator of right wheel and the next eleven integers describe the right wheel counter-clockwise.

The final position is therefore encoded like:

0 3 4 3 0 5 6 5 0 1 2 1 0 7 8 7 0 9 10 9 0 1 2 1

If for instance we rotate the left wheel clockwise one position from the final configuration (as shown in Figure 2) the puzzle configuration would be encoded like:

2 1 0 3 4 3 0 5 6 5 0 1 0 7 8 7 0 9 10 9 0 5 0 1


Figure 2. The puzzle after rotating the left wheel on step clockwise from the final configuration.



题目大意:

有一种拼盘有两个轮子, 而这两个轮子都可以顺时针转和逆时针转. 拼盘有21 片, 10 片是有点圆的三角形, 而另外11 片则是骨头型. 图1 是所要拼成的目标. 记住一个"步骤"是你必须转动其中一个轮子往顺时针或逆时针的方向直到拼盘的每一片的边缘都能和他旁边的那几片的边缘重合在一起.

你的任务是写一个程式读入拼盘一开始的状态并且输出一个能够达成目标的最短转动序列,我们会用底下的数字来表示每一片:



0 灰色(骨头型)
1 黄色(三角形)
2 黄色(骨头型)
3 蓝色(三角形)
4 蓝色(骨头型)
5 紫色(三角形)
6 紫色(骨头型)
7 绿色(三角形)
8 绿色(骨头型)
9 红色(三角形)
10 红色(骨头型)


我们可以用24个整数来描述一个拼盘的状态,前12个数描述左边的轮子而后12个数描述右边的轮子
我们可以把最终的目标状态用以下这24个数字的序列的来表示:

                  0 3 4 3 0 5 6 5 0 1 2 1 0 7 8 7 0 9 10 9 0 1 2 1

而如果我们把最终的盘面的左轮往顺时针方向转一格(如图2), 那它的状态序列就会变成这样:

                  2 1 0 3 4 3 0 5 6 5 0 1 0 7 8 7 0 9 10 9 0 5 0 1




分析与总结:

这题挺给力的。 首先,根据题意可以看出有四种旋转可以改变状态, 旋转之后序列就会根据旋转而改变。所以为了方便起见,专门写一个旋转函数来处理, 旋转后,对于左右两个盘,改变的只有开始和结尾的几个数字, 所以有大部分可以用memcpy整块复制而节约时间, 对于 改变的部分手动给它赋值。友情提示:写旋转函数时需要保持清醒的头脑, 否则很容易转晕而写错。

本来我想说写完旋转函数, 这题就算完成了一半,但是我错了。

由于我就没有注意到有16层状态所以很天真的直接单向bfs搜索, 程序也 很快很开心就写完了,但是问题随之而来,由于第二个测试样例
就需要16步,也就是说要搜到16层,需要的状态数量是十分巨大的!! 所以在自己的电脑上一直出不来, 运行错误。。。

一般出现这个情况大多是数组开小了,所以我一直不断地加大数组, 结果还是无法出结果。

于是才注意到层数太多,状态也太多了,根本无法单向搜出来。。。

最终,借鉴了双向bfs的思想,先逆向从结果状态搜索8层, 然后再从正向搜索,一旦正向搜索“碰到”了逆向搜索搜出来的状态,那么即得出答案。如果正向搜到第8层还没有“碰到”逆向搜到的状态,那么就表示无解。 由于目标状态只有一个,所以逆向搜索只要搜一次就可以了。 

然后就是正向搜索部分和逆向搜索部分的路径输出,分别写两个函数来输出。 注意逆向搜索时保存的路径方向, 应该是相反的。

对于判重,因为偷懒,直接用map来做了。。。

提交后一次AC, 而且用map的速度还不错,这题的数据应该比较水。

/*  UVa  704 - Colour Hash*  Time : 0.140s (UVA)*  双向搜索 + map判重*/#include<iostream>
#include<cstring>
#include<cstdio>
#include<map>
#include<string>
#define MAXN 1000000
using namespace std;
map<string, int>vis;
map<string, int>back_vis;
typedef int State[24];
State goal = {0, 3, 4, 3, 0, 5, 6, 5, 0, 1, 2, 1, 0, 7, 8, 7, 0 ,9, 10,9 ,0, 1, 2, 1}, start; 
State que[MAXN];
int step[MAXN], father[MAXN], path[MAXN], ans;
int back_father[MAXN], back_path[MAXN];// 旋转函数
void rotate(int *next, int *origin, int dir){if(dir==1){ // 左盘顺时针转next[0] = origin[10], next[1] = origin[11];memcpy(next+2, origin, sizeof(int)*10);  // 大块的,连续数组元素的用memcpy节约时间memcpy(next+12, origin+12, sizeof(int)*9);next[21]=origin[7], next[22] = origin[8], next[23] = origin[9];}else if(dir==2){ // 右盘顺时针转memcpy(next+12, origin+14, sizeof(int)*10);next[22] = origin[12], next[23] = origin[13];memcpy(next, origin, sizeof(int)*9);next[9] = next[21], next[10] = next[22], next[11] = next[23];   }else if(dir==3){ // 左盘逆时针转memcpy(next, origin+2, sizeof(int)*10);next[10] = origin[0], next[11] = origin[1];memcpy(next+12, origin+12, sizeof(int)*9);next[21]=next[9], next[22]=next[10], next[23]=next[11];}else if(dir==4){ // 右盘逆时针转next[12] = origin[22],  next[13] = origin[23];memcpy(next+14, origin+12, sizeof(int)*10);memcpy(next, origin, sizeof(int)*9);next[9] = next[21], next[10] = next[22], next[11] = next[23];    }
}
// 把状态转换成字符串
inline string change_state(State &s){ string str;for(int i=0; i<24; ++i)str += s[i]+'0';return str;
}inline int try_to_insert(int s){string str = change_state(que[s]);if(!vis[str]){vis[str]=1; return true;}return false;
} inline int back_try_to_insert(int s){string str = change_state(que[s]);if(!back_vis[str]){back_vis[str]=s; //保存在队列在队列中的位置,而不是1或者true,方便后面输出路径return true;}return false;    
}bool back_bfs(){ //逆向搜索memset(back_father, 0, sizeof(back_father));step[0] = step[1] = 0;back_vis.clear();back_vis[change_state(goal)] = 1;int front=0, rear=1;memcpy(que[0], goal, sizeof(goal));while(front < rear){State &s = que[front];if(step[front] > 8){ // 超过8步不再搜索++front; continue;}for(int i=1; i<=4; ++i){State &next = que[rear];rotate(next, s, i);step[rear] = step[front]+1;if(back_try_to_insert(rear)){back_father[rear] = front; switch(i){ // 因为是逆向搜索,所以方向保存的也要是相反的case 1:back_path[rear] = 3;break;case 2:back_path[rear] = 4;break;case 3:back_path[rear] = 1;break;case 4:back_path[rear] = 2;break;}rear++;}}++front;}return false;
}bool bfs(){ // 正向搜索memset(father, 0, sizeof(father));step[0] = 0;vis.clear();vis[change_state(start)] = 1;int front=0, rear=1;memcpy(que[0], start, sizeof(start));while(front < rear){State &s = que[front];if(step[front] > 8) {++front; continue;}if(memcmp(s, goal, sizeof(goal))==0){ans = front;return false;}       if(back_vis[change_state(s)]){ans = front;return true;}for(int i=1; i<=4; ++i){State &next = que[rear];rotate(next, s, i);step[rear] = step[front]+1;if(try_to_insert(rear)){father[rear]=front; path[rear]=i;rear++;}}++front;}ans = -1;
}void print_path(int cur){ // 打印出正向搜索部分if(cur){print_path(father[cur]);printf("%d",path[cur]);}
}
void print_back(){  // 打印出逆向搜索部分string str = change_state(que[ans]);int cur = back_vis[str];while(cur){printf("%d", back_path[cur]);cur = back_father[cur];}
}int main(){freopen("input.txt","r",stdin);int T;back_bfs();  // 逆向搜索只需要一次就够了scanf("%d", &T);while(T--){for(int i=0; i<24; ++i)scanf("%d", &start[i]);if(memcmp(start, goal, sizeof(goal))==0) printf("PUZZLE ALREADY SOLVED\n");else{bfs();if(ans!=-1){print_path(ans);print_back();printf("\n");}elseprintf("NO SOLUTION WAS FOUND IN 16 STEPS\n");}}  return 0;
}


——  生命的意义,在于赋予它意义。

 

          
     原创 http://blog.csdn.net/shuangde800 , By   D_Double  (转载请标明)












这篇关于UVa 704 - Colour Hash, 双向bfs,很给力的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/476069

相关文章

hdu1254(嵌套bfs,两次bfs)

/*第一次做这种题感觉很有压力,思路还是有点混乱,总是wa,改了好多次才ac的思路:把箱子的移动当做第一层bfs,队列节点要用到当前箱子坐标(x,y),走的次数step,当前人的weizhi(man_x,man_y),要判断人能否将箱子推到某点时要嵌套第二层bfs(人的移动);代码如下:

学习hash总结

2014/1/29/   最近刚开始学hash,名字很陌生,但是hash的思想却很熟悉,以前早就做过此类的题,但是不知道这就是hash思想而已,说白了hash就是一个映射,往往灵活利用数组的下标来实现算法,hash的作用:1、判重;2、统计次数;

hdu1043(八数码问题,广搜 + hash(实现状态压缩) )

利用康拓展开将一个排列映射成一个自然数,然后就变成了普通的广搜题。 #include<iostream>#include<algorithm>#include<string>#include<stack>#include<queue>#include<map>#include<stdio.h>#include<stdlib.h>#include<ctype.h>#inclu

康拓展开(hash算法中会用到)

康拓展开是一个全排列到一个自然数的双射(也就是某个全排列与某个自然数一一对应) 公式: X=a[n]*(n-1)!+a[n-1]*(n-2)!+...+a[i]*(i-1)!+...+a[1]*0! 其中,a[i]为整数,并且0<=a[i]<i,1<=i<=n。(a[i]在不同应用中的含义不同); 典型应用: 计算当前排列在所有由小到大全排列中的顺序,也就是说求当前排列是第

hdu1496(用hash思想统计数目)

作为一个刚学hash的孩子,感觉这道题目很不错,灵活的运用的数组的下标。 解题步骤:如果用常规方法解,那么时间复杂度为O(n^4),肯定会超时,然后参考了网上的解题方法,将等式分成两个部分,a*x1^2+b*x2^2和c*x3^2+d*x4^2, 各自作为数组的下标,如果两部分相加为0,则满足等式; 代码如下: #include<iostream>#include<algorithm

csu1329(双向链表)

题意:给n个盒子,编号为1到n,四个操作:1、将x盒子移到y的左边;2、将x盒子移到y的右边;3、交换x和y盒子的位置;4、将所有的盒子反过来放。 思路分析:用双向链表解决。每个操作的时间复杂度为O(1),用数组来模拟链表,下面的代码是参考刘老师的标程写的。 代码如下: #include<iostream>#include<algorithm>#include<stdio.h>#

usaco 1.2 Milking Cows(类hash表)

第一种思路被卡了时间 到第二种思路的时候就觉得第一种思路太坑爹了 代码又长又臭还超时!! 第一种思路:我不知道为什么最后一组数据会被卡 超时超了0.2s左右 大概想法是 快排加一个遍历 先将开始时间按升序排好 然后开始遍历比较 1 若 下一个开始beg[i] 小于 tem_end 则说明本组数据与上组数据是在连续的一个区间 取max( ed[i],tem_end ) 2 反之 这个

uva 10055 uva 10071 uva 10300(水题两三道)

情歌两三首,水题两三道。 好久没敲代码了为暑假大作战热热身。 uva 10055 Hashmat the Brave Warrior 求俩数相减。 两个debug的地方,一个是longlong,一个是输入顺序。 代码: #include<stdio.h>int main(){long long a, b;//debugwhile(scanf("%lld%lld", &

poj 3259 uva 558 Wormholes(bellman最短路负权回路判断)

poj 3259: 题意:John的农场里n块地,m条路连接两块地,w个虫洞,虫洞是一条单向路,不但会把你传送到目的地,而且时间会倒退Ts。 任务是求你会不会在从某块地出发后又回来,看到了离开之前的自己。 判断树中是否存在负权回路就ok了。 bellman代码: #include<stdio.h>const int MaxN = 501;//农场数const int

poj 2349 Arctic Network uva 10369(prim or kruscal最小生成树)

题目很麻烦,因为不熟悉最小生成树的算法调试了好久。 感觉网上的题目解释都没说得很清楚,不适合新手。自己写一个。 题意:给你点的坐标,然后两点间可以有两种方式来通信:第一种是卫星通信,第二种是无线电通信。 卫星通信:任何两个有卫星频道的点间都可以直接建立连接,与点间的距离无关; 无线电通信:两个点之间的距离不能超过D,无线电收发器的功率越大,D越大,越昂贵。 计算无线电收发器D