1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
| #include<bits/stdc++.h> using namespace std; const int INF=0x3f3f3f3f; int a[5][5]; int point[2]; int d1[6][2]={-2,0,2,0,0,-2,0,2,2,2,-2,-2}; int d2[6][2]={-1,0,1,0,0,-1,0,1,1,1,-1,-1}; bool f(int x, int y) { return x>=0 && x<5 && y>=0 && y<5 && x>=y; }
int dfs(int flag) { int endd=1; int maxx; if(flag==0) maxx=-INF; else maxx=INF; for(int x=0; x<5; ++x) { for(int y=0; y<=x; ++y) { if(a[x][y]) continue; for(int k=0; k<6; ++k) { int x1=x+d1[k][0]; int y1=y+d1[k][1]; int x2=x+d2[k][0]; int y2=y+d2[k][1]; if(f(x1,y1)&&f(x2,y2)&&a[x1][y1]&&a[x2][y2]) { endd=0; int p1=a[x1][y1]; int p2=a[x2][y2]; a[x][y]=p1; a[x1][y1]=0; a[x2][y2]=0; point[flag]+=p1*p2; int t=dfs(1-flag); if(flag==0) maxx=max(maxx,t); else maxx=min(maxx,t); a[x][y]=0; a[x1][y1]=p1; a[x2][y2]=p2; point[flag]-=p1*p2; } } } } if (endd) return point[0]-point[1]; else return maxx; }
int main() { for (int i = 0; i < 5; ++i) for (int j = 0; j <= i; ++j) scanf("%d", &a[i][j]); printf("%d\n",dfs(0)); return 0; }
|