usaco 4.4 Shuttle Puzzle(字符hash+搜索)
2012-09-20 14:22
435 查看
Shuttle Puzzle
Traditional
The Shuttle Puzzle of size 3 consists of 3 white marbles, 3 black marbles, and a strip of wood with 7 holes. The marbles of the same color are placed in the holes at the opposite ends of the strip, leaving the center
hole empty.
To solve the shuttle puzzle, use only two types of moves. Move 1 marble 1 space (into the empty hole) or jump 1 marble over 1 marble of the opposite color (into the empty hole). You may not back up, and you may
not jump over 2 marbles.
A Shuttle Puzzle of size N consists of N white marbles and N black marbles and 2N+1 holes.
Here's one solution for the problem of size 3 showing the initial, intermediate, and end states:
Write a program that will solve the SHUTTLE PUZZLE for any size N (1 <= N <= 12) in the minimum number of moves and display the successive moves, 20 per line.
Output the the solution that would appear first among the set of minimal solutions sorted numerically (first by the first number, using the second number for ties, and so on).
题意:给你2*n个黑白棋,一开始白棋在左黑棋在右,要求你计算出把黑白棋对调的最小步数,并输出方案
分析:一开始就想到爆搜加上字符hash记录状态。。。结果显然会超时,本地输入7就出不来了= =
但是分析了下前7个数据,发现时有规律的,这时候完全可以找规律,不过我懒了,只用了个规律,就是步数一定等于n*(n+2)
这样就可以剪枝了,一运行,快了不少,10也可以出来,就是慢了点,后来用了优化,就是黑棋只能往左移,白棋往右移,然后就秒掉了所有数据= =
貌似是只要一条这样的路径吧。。。。
代码:
Traditional
The Shuttle Puzzle of size 3 consists of 3 white marbles, 3 black marbles, and a strip of wood with 7 holes. The marbles of the same color are placed in the holes at the opposite ends of the strip, leaving the center
hole empty.
INITIAL STATE: WWW_BBB GOAL STATE: BBB_WWW
To solve the shuttle puzzle, use only two types of moves. Move 1 marble 1 space (into the empty hole) or jump 1 marble over 1 marble of the opposite color (into the empty hole). You may not back up, and you may
not jump over 2 marbles.
A Shuttle Puzzle of size N consists of N white marbles and N black marbles and 2N+1 holes.
Here's one solution for the problem of size 3 showing the initial, intermediate, and end states:
WWW BBB WW WBBB WWBW BB WWBWB B WWB BWB W BWBWB WBWBWB BW WBWB BWBW WB BWBWBW BWBWB W BWB BWW B BWBWW BB WBWW BBBW WW BBB WWW
Write a program that will solve the SHUTTLE PUZZLE for any size N (1 <= N <= 12) in the minimum number of moves and display the successive moves, 20 per line.
PROGRAM NAME: shuttle
INPUT FORMAT
A single line with the integer N.SAMPLE INPUT (file shuttle.in)
3
OUTPUT FORMAT
The list of moves expressed as space-separated integers, 20 per line (except possibly the last line). Number the marbles/holes from the left, starting with one.Output the the solution that would appear first among the set of minimal solutions sorted numerically (first by the first number, using the second number for ties, and so on).
SAMPLE OUTPUT (file shuttle.out)
3 5 6 4 2 1 3 5 7 6 4 2 3 5 4
题意:给你2*n个黑白棋,一开始白棋在左黑棋在右,要求你计算出把黑白棋对调的最小步数,并输出方案
分析:一开始就想到爆搜加上字符hash记录状态。。。结果显然会超时,本地输入7就出不来了= =
但是分析了下前7个数据,发现时有规律的,这时候完全可以找规律,不过我懒了,只用了个规律,就是步数一定等于n*(n+2)
这样就可以剪枝了,一运行,快了不少,10也可以出来,就是慢了点,后来用了优化,就是黑棋只能往左移,白棋往右移,然后就秒掉了所有数据= =
貌似是只要一条这样的路径吧。。。。
代码:
/* ID: 15114582 PROG: shuttle LANG: C++ */ #include<cstdio> #include<cstring> #include<iostream> using namespace std; const int mm=100007; struct hashTable { int h[mm],p[mm],d[mm],size; char s[mm][33]; int hash(char *a) { int h=0; while(*a)h=h*131+(*a++); return (h&0x7FFFFFFF)%mm; } int insert(char *a,int val) { int i,id=hash(a); for(i=h[id];i>=0;i=p[i]) if(!strcmp(s[i],a)) { if(d[i]>val) { d[i]=val; return 1; } return 0; } strcpy(s[size],a); d[size]=val,p[size]=h[id],h[id]=size++; return 1; } void clear() { size=0; memset(h,-1,sizeof(h)); } }g; char s[33],e[33]; int q[mm],out[mm]; int i,n,r,ans; bool dfs(int m) { if(r>=ans)return 0; if(m==n&&!strcmp(s,e)) { ans=r; for(int i=0;i<r;++i) out[i]=q[i]; return 1; } if(m>0&&s[m-1]=='W') { s[m]=s[m-1],s[m-1]=' '; q[r++]=m-1; if(g.insert(s,r)) if(dfs(m-1))return 1; --r,s[m-1]=s[m],s[m]=' '; } if(m<n+n+1&&s[m+1]=='B') { s[m]=s[m+1],s[m+1]=' '; q[r++]=m+1; if(g.insert(s,r)) if(dfs(m+1))return 1; --r,s[m+1]=s[m],s[m]=' '; } if(m>1&&s[m-1]!=s[m-2]&&s[m-2]=='W') { s[m]=s[m-2],s[m-2]=' '; q[r++]=m-2; if(g.insert(s,r)) if(dfs(m-2))return 1; --r,s[m-2]=s[m],s[m]=' '; } if(m<n+n&&s[m+1]!=s[m+2]&&s[m+2]=='B') { s[m]=s[m+2],s[m+2]=' '; q[r++]=m+2; if(g.insert(s,r)) if(dfs(m+2))return 1; --r,s[m+2]=s[m],s[m]=' '; } return 0; } int main() { freopen("shuttle.in","r",stdin); freopen("shuttle.out","w",stdout); while(~scanf("%d",&n)) { for(i=0;i<n;++i) e[n+i+1]=s[i]='W',e[i]=s[n+i+1]='B'; e =s =' '; e[n+n+1]=s[n+n+1]='\0'; g.clear(); g.insert(s,0); r=0; ans=n*(n+2)+1; dfs(n); for(i=0;i<ans;++i) printf("%d%c",out[i]+1,((i+1)%20)&&(i<ans-1)?' ':'\n'); } return 0; }
相关文章推荐
- USACO Section 4.4 Shuttle Puzzle - Hash都不用的DFS水题..
- USACO 4.4 shuttle puzzle
- USACO 4.4 Shuttle Puzzle
- usaco 4.4 Shuttle Puzzle 2010.8.8
- usaco 4.4 Shuttle Puzzle 找规律
- USACO-Section 4.4 Shuttle Puzzle (BFS)
- USACO 4.4 Shuttle Puzzle(dfs)
- usaco-4.4-milk6-passed
- 求割边最少的最小割(P1344 [USACO4.4]追查坏牛奶Pollutant Control)
- 洛谷 P1344 [USACO4.4]追查坏牛奶Pollutant Control
- [USACO 4.4.1 Shuttle Puzzle]
- C++_USACO_求一个字符串的连续相同字符的最大长度
- usaco training 4.4.1 Shuttle Puzzle 题解
- USACO4.4.1 Shuttle Puzzle (shuttle)
- usaco-4.4-frameup-passed
- USACO-Section1.2 palsquare[字符处理]
- usaco 4.4 Pollutant Control(最小割最少边数字典序最小)
- USACO 4.4 Pollutant Control
- Frame Up_usaco 4.4_拓扑排序
- usaco 4.4.1 Shuttle Puzzle