adv虫洞
3 5 0 0 9 9 0 3 6 9 6 0 0 1 2 1 1 3 6 2 2 3 0 9 8 4 6 4 8 8 3 2 3 4 9 20 1 2 7 10 3 6 6 10 18 3 3 0 0 5 6 3 4 1 2 1 7 8 4 5 3 2 0 5 4 2 #include<stdio.h> #include<stdlib.h> int d; int cdz[5][5]; int sx,sy; int ex,ey; int ip[5] = {0,1,2,3,4}; int op[5]; int min; int D = 0; void DFS(int step,int n,int x,int y); bool issafe(int step,int v); int main() { int n; int temp; freopen("a.txt","r",stdin); scanf("%d",&n); for(int i = 0;i < n;i++) { scanf("%d",&d); scanf("%d",&sx); scanf("%d",&sy); scanf("%d",&ex); scanf("%d",&ey); int dx = sx - ex > 0 ? sx - ex : ex - sx; int dy = sy - ey > 0 ? sy - ey : ey - sy; min = dx + dy; temp = min; for(int p = 0;p < d;p++) { for(int q = 0;q < 5;q++) { scanf("%d",&cdz[p][q]); } } for(int j = 1;j <= d;j++) { DFS(0,j,sx,sy); } printf("%d\n",min); min = temp; for(int f = 0;f < 5;f++) { op[f] = 0; for(int j = 0;j < 5;j++) { cdz[f][j] = 0; } } } return 0; } void DFS(int step,int n,int x,int y) { if(n == step) { int nx = ex - x > 0 ? ex - x : x - ex; int ny = ey - y > 0 ? ey - y : y - ey; if(D + nx + ny < min){ min = D + nx + ny; } /*for(int k = 0;k < n;k++) { printf("%d",op[k]); } printf(" %d",D + nx + ny); printf("\t");*/ return; } for(int i = 0;i < 5;i++) { if(issafe(step,i)) { op[step] = i; int dx = cdz[i][2] - x > 0 ? cdz[i][2] - x : x - cdz[i][2]; int dy = cdz[i][3] - y > 0 ? cdz[i][3] - y : y - cdz[i][3]; D += dx + dy + cdz[i][4]; DFS(step + 1,n,cdz[i][0],cdz[i][1]); D = D - dx - dy - cdz[i][4]; //D退 int gx = cdz[i][0] - x > 0 ? cdz[i][0] - x : x - cdz[i][0]; int gy = cdz[i][1] - y > 0 ? cdz[i][1] - y : y - cdz[i][1]; D += gx + gy + cdz[i][4]; DFS(step + 1,n,cdz[i][2],cdz[i][3]); D = D - gx - gy - cdz[i][4]; op[step] = 0; } } } bool issafe(int step,int v) { bool ret = true; for(int i = 0;i < step;i++) { if(v == op[i]) { ret = false; } } return ret; }