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 |
TLE,求求大家了,我真的真的不知道怎么办了,看看吧,跪谢,谁能帮我看看呀,不知道怎么优化一下,非常谢谢 #include<iostream> #include<stdio.h> using namespace std; #define N 70000 bool flag[70000]; struct State{ int st; int step; }; int b[16]={1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768}; void move(int r,State &t){ int i=r/4,j=r%4; t.st^=b[15-r]; int n=r-4; int k=i-1; while(k>=0){ t.st^=b[15-n]; k--; n-=4; } k=i+1; n=r+4; while(k<4){ t.st^=b[15-n]; k++; n=n+4; } k=j-1; n=r-1; while(k>=0){ t.st^=b[15-n]; k--,n--; } k=j+1;n=r+1; while(k<4){ t.st^=b[15-n]; k++,n++; } } void BFS(State s,string ste){ State q[N]; string q1[N]; int front=0,rear=0; q[rear]=s; q1[rear++]=ste; while(front!=rear){ State temp=q[front]; string temp1=q1[front++]; for(int i=0;i<16;i++){ State t=temp; string s1=temp1; int k; k=i/4; s1.append(1,k); k=i%4; s1.append(1,k); move(i,t); if(t.st==0){ printf("%d\n",t.step+1); int len=s1.length(); for(int i=0;i<len;i=i+2) printf("%d %d\n",s1[i]+1,s1[i+1]+1); return; } if(flag[t.st]==0){ flag[t.st]=true; t.step++; q[rear]=t; q1[rear++]=s1; } } } } int main(){ char map[17]; string ste; int i,j; State s; s.st=0;s.step=0; scanf("%s%s%s%s",map,map+4,map+8,map+12); for(i=0;i<16;i++) { if(map[i]=='+') s.st++; s.st<<=1; } s.st>>=1; flag[s.st]=true; if(s.st==0) printf("0\n"); else BFS(s,ste); return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator