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 |
Re:麻烦牛人纠错!!In Reply To:麻烦牛人纠错!! Posted by:bjtu1 at 2009-02-17 12:06:38 > 用队列枚举,我的代码: > #include<stdio.h> > #include<memory.h> > #include<string.h> > #define MAX 1000000 > > > int visit[65537]; > > > typedef struct Node{ > int st; > int step; > int x,y; > int pre; > }Node; > > Node arr[MAX]; > int head,tail; > > int bfs(int temp){ > int stat; > int i,j,k; > head=0; > tail=1; > arr[tail].st=temp; > arr[tail].step=0; > arr[tail].x=arr[tail].y=0; > arr[tail].pre=-1; > visit[temp]=1; > while(head!=tail){ > head++; > for(i=0;i<4;i++) > for(j=0;j<4;j++){ > stat=arr[head].st; > for(k=0;k<4;k++) > stat^=1 << (15 - 4 * i - k); > for(k=0;k<4;k++) > if(k!=i){ > stat^=1 << (15 - 4 * k - j); > } > if(!visit[stat]){ > arr[++tail].st=stat; > arr[tail].step=arr[head].step+1; > arr[tail].x=i+1; > arr[tail].y=j+1; > arr[tail].pre=head; > visit[stat]=1; > } > if(stat==0) > return tail; > } > } > > return -1; > }; > > int main() > { > int temp; > int route[16]; > char str[4]; > int i,j; > int index; > int k; > //int flag=0; > > > > temp=0; > index=15; > for(i=1;i<=4;i++){ > scanf("%s",str); > for(j=0;j<4;j++){ > if(str[j]=='+') > temp+=(1<<(15-index)); > index--; > } > } > //flag=1; > memset(visit,0,sizeof(visit)); > k=bfs(temp); > i=0; > printf("%d\n",arr[k].step); > route[i]=k; > k=arr[k].pre; > while(k!=-1){ > route[++i]=k; > k=arr[k].pre; > } > for(j=i-1;j>=0;j--) > printf("%d %d\n",arr[route[j]].x,arr[route[j]].y); > > return 0; > } > 结果: > -+-- > ---- > ---- > -+-- > 6 > 1 1 > 1 2 > 1 4 > 4 1 > 4 2 > 4 4 > Press any key to continue > 这是为什么?? Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator