POJ2977 小学生的考试题,暴力得出O(1)的解法
#include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<cmath> #include<vector> using namespace std; int ans; void turn(int i,int j,int x,int y,int z,int x0,int y0,int L,int W,int H) { if(z==0)ans=min(ans,(x-x0)*(x-x0)+(y-y0)*(y-y0)); else { if(i>=0&&i<2) turn(i+1,j,H-z,y,x,x0+H,y0,H,W,L); if(j>=0&&j<2) turn(i,j+1,x,H-z,y,x0,y0+H,L,H,W); if(i<=0&&i>-2) turn(i-1,j,z,y,L-x,x0-L,y0,H,W,L); if(j<=0&&j>-2) turn(i,j-1,x,z,W-y,x0,y0-W,L,H,W); } } int rect_dist(int L,int W,int H,int x1,int y1,int z1,int x2,int y2,int z2) { if(z1!=0&&z1!=H) if(y1==0||y1==W) swap(y1,z1),swap(y2,z2),swap(W,H); else swap(x1,z1),swap(x2,z2),swap(L,H); if(z1==H) z1=0,z2=H-z2; ans=1<<30; turn(0,0,x2,y2,z2,x1,y1,L,W,H); return ans; } int main() {//freopen("t.txt","r",stdin); int L,W,H,x,y,z; while(scanf("%d%d%d%d%d%d",&L,&W,&H,&x,&y,&z)) { if(L==0&&W==0&&H==0&&x==0&&y==0&&z==0)return 0; printf("%d\n",rect_dist(L,W,H,0,0,0,x,y,z)); } return 0; }
时间: 2024-12-14 18:49:19