1 条题解

  • 0
    @ 2023-10-8 20:24:05

    #include #include using namespace std; struct zb{ int x,y; }c[26]; int a[5][5]; int b[5][5]; int w[5][5][5][5]; bool used[5][5]; int sum=0; int minn=0x7fffffff; void search(int pos,int v)


    { if(pos>sum) { minn=min(minn,v); return ; } for(int i=1;i<=4;i++) for(int j=1;j<=4;j++) if(w[c[pos].x][c[pos].y][i][j]!=0&&!used[i][j]) { used[i][j]=true; search(pos+1,v+w[c[pos].x][c[pos].y][i][j]); used[i][j]=false; } } int main() { char ee; for(int i=1;i<=4;i++) for(int j=1;j<=4;j++) {cin>>ee; a[i][j]=ee-'0'; } for(int i=1;i<=4;i++) for(int j=1;j<=4;j++) { cin>>ee; b[i][j]=ee-'0'; if(a[i][j]==b[i][j]) a[i][j]=b[i][j]=0; } for(int i=1;i<=4;i++) for(int j=1;j<=4;j++) if(a[i][j]==1) { c[++sum].x=i; c[sum].y=j; for(int k=1;k<=4;k++) for(int kk=1;kk<=4;kk++) if(b[k][kk]==1) { w[i][j][k][kk]=abs(i-k)+abs(j-kk); } } search(1,0); printf("%d",minn); return 0; }

    • 1

    信息

    ID
    1207
    时间
    1000ms
    内存
    512MiB
    难度
    10
    标签
    递交数
    7
    已通过
    4
    上传者