#include<cstdio> char a[5][5],b[5][5]; struct E { int x,y; }s[10],t[10]; int l1,l2,tot,ans=-1,f[10][10]; bool flag[10]; int ABS(int x) {if (x<0) return -x; return x;} void dfs(int x,int y) { bool fl=0; tot+=f[x][y]; flag[y]=1; for (int i=1;i<=l2;i++) if (!flag[i]) { fl=1; dfs(x+1,i); } if (!fl&&(ans==-1||ans>tot)) ans=tot; tot-=f[x][y]; flag[y]=0; } int main() { for (int i=0;i<4;i++) scanf("%s",a[i]); for (int i=0;i<4;i++) scanf("%s",b[i]); for (int i=0;i<4;i++) for (int j=0;j<4;j++) if (a[i][j]!=b[i][j]) { if (a[i][j]=='1') s[++l1].x=i,s[l1].y=j; else t[++l2].x=i,t[l2].y=j; } for (int i=1;i<=l1;i++) for (int j=1;j<=l2;j++) f[i][j]=ABS(s[i].x-t[j].x)+ABS(s[i].y-t[j].y); for (int i=1;i<=l2;i++) { tot=0; dfs(1,i); } if (ans==-1) puts("0"); else printf("%d",ans); return 0; }
|