4 10 4 5 6 2 10 2 10 4 5 6 2 10 2 60 1 20 29 20 50 20 60 1 20 20 20 60 20 #include<stdio.h> #define SIZE 100 int data[SIZE]; int Gate[3][2]; int D[3]; int num; int bushu,minbushu; int pos[SIZE]; int leftorder(int p){ int ps=Gate[p][0]; int people=Gate[p][1]; int left=0; int right=num-1; int orig=0; int count=0; for(int n=people;n>0;){ if(ps-orig-1>=left&&pos[ps-1-orig]==0){ pos[ps-orig-1]=-(p+1); count=count+orig+1; n--; } if(n==0) break; if(ps-1+orig<=right&&pos[ps-1+orig]==0){ pos[ps+orig-1]=-(p+1); n--; count=count+orig+1; } orig++; } return count; } int rightorder(int p){ int ps=Gate[p][0]; int people=Gate[p][1]; int left=0; int right=num-1; int orig=0; int count=0; for(int n=people;n>0;){ if(ps-1+orig<=right&&pos[ps-1+orig]==0){ pos[ps+orig-1]=-(p+1); n--; count=count+orig+1; } if(n==0) break; if(ps-orig-1>=left&&pos[ps-1-orig]==0){ pos[ps-orig-1]=-(p+1); n--; count=count+orig+1; } orig++; } return count; } void init(int p){ for(int i=0;i<num;i++){ if(pos[i]==-p-1){ pos[i]=0; } } } void DFS(int step,int bushu){ if(step==3){ if(bushu<minbushu){ minbushu=bushu; } return; } if(bushu>minbushu) return; for(int i=0;i<3;i++){ if(!D[i]){ D[i]=1; if(i==0){ DFS(step+1,bushu+leftorder(i)); init(i); } if(i==2){ DFS(step+1,bushu+rightorder(i)); init(i); } if(i==1){ DFS(step+1,bushu+leftorder(i)); init(i); DFS(step+1,bushu+rightorder(i)); init(i); } D[i]=0; } } } int main(){ //将txt内数据读入 freopen("a.txt","r",stdin); int nCase; scanf("%d",&nCase); for(int tc=0;tc<nCase;tc++) { scanf("%d",&num); for(int i=0;i<3;i++) { scanf("%d%d",&Gate[i][0],&Gate[i][1]); } for(int i=0;i<3;i++){ D[i]=0; } for(int i=0;i<SIZE;i++){ pos[i]=0; } minbushu=0x0FFFFFFF; bushu=0; DFS(0,0); printf("%d ",minbushu); //打印读入数据 /*cout<<num<<endl; for(int i=0;i<3;i++){ cout<<Gate[i][0]<<" "<<Gate[i][1]<<endl; }*/ } return 0; }