Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
我就用从0123456开始搜索的,用的C语言程序如下In Reply To:bfs tle-_- Posted by:qywyh_scut at 2007-04-28 17:06:13 #include<stdio.h> #include<string.h> #define MAX 5040 struct { int deep; char s[8]; }a[MAX]; int head=0,tail=1; char ch,ss[8]; int you(char str[]) { int i; for(i=0;i<tail;i++) if(strcmp(a[i].s,str)==0) return 1; return 0; } void work(int x,int y) { strcpy(ss,a[head].s); ch=ss[x]; ss[x]=ss[y]; ss[y]=ch; if(you(ss)) return; strcpy(a[tail].s,ss); a[tail++].deep=a[head].deep+1; } int main() { int i,j,n; char sss[8]; strcpy(a[0].s,"0123456"); a[0].deep=0; while(tail>head) { for(i=0;;i++) if(a[head].s[i]=='0') break; if(i==0) { work(0,2); work(0,4); work(0,6); } if(i==1) {work(1,6);work(1,2);} if(i==3||i==5) {work(i,i+1);work(i,i-1);} if(i==2||i==4) {work(0,i);work(i,i+1);work(i,i-1);} if(i==6) {work(1,6);work(5,6);work(0,6);} head++; } scanf("%d",&n); while(n--) { int flag=1; scanf("%s",sss); for(i=0;i<tail;i++) if(strcmp(a[i].s,sss)==0) { printf("%d\n",a[i].deep); flag=0; break; } if(flag) printf("-1\n"); } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator