题目描述
acquez and Alia’s parents take them on many road trips. To keep themselves occupied, they play a triangular peg game, meant for one player. In the original game, one player has an equilateral triangle containing 15 holes (five holes per side of the triangle). Initially one hole is empty and the remaining 14 are filled with pegs. The player moves by picking up a peg to “jump” it over an adjacent peg, landing on an empty hole adjacent to the jumped peg. Jumps must be in straight lines (horizontally or diagonally). The peg that is jumped over is removed. The goal of this game is to leave just one peg on the board. Figure 1 shows the result of a jump in this game.
Figure 1: One possible move in the original game. On the left is an initial board, where X represents a peg and O represents an empty hole. The jump moves the right peg in the second (from top) row diagonally down and to the left, over the middle peg of the third row, which is removed.
Ultimately, Jacquez and Alia have gotten bored of playing this game individually. They decide to invent a version that they could play together. In the new version, each peg has a positive point value, and they alternate turns. During a player’s turn, they must make a jump if one is available. The score for a jump is the product of the two pegs involved in the jump.
The total score of a player is the sum of the scores of their jumps. The game ends when a player has no possible jumps to make. Each player’s goal is to maximize their own total score minus their opponent’s total score (at the end of the game). For example, Jacquez would prefer to win with a score of 100 to Alia’s score of 60 (with a differential of 40), than to win with a score of 1000 to Alia’s score of 998 (with a smaller differential of only 2). Similarly, Alia wants to win by as much as possible over Jacquez.
For this version of the game, we can display the game state by replacing each X shown above with a corresponding value for that peg, and using a value of 0 (zero) for each of the open holes. Figure 2 shows an example move.
Figure 2: One possible move in the new game, jumping the peg with value 6 over the peg with value 7. This move is worth 6⋅7=42 for Jacquez (since he moves first). The values are from the first sample input.
Jacquez has had some trouble winning. Write a program to calculate the best he can do, assuming that he starts and both players play optimally.
输入
The input consists of five lines, describing the initial state of the board. The ith (1≤i≤5) line contains i space separated integers, representing the pegs and holes on the ith row. Each of these integers is between 0 and 100, inclusive. A value of 0 represents a hole while the rest of the values represent pegs. It is possible for two different pegs to have the same value. Of the 15 input values it is guaranteed that exactly one is 0, thus all input boards start with exactly one hole.
输出
Output the value of Jacquez’s score minus Alia’s score at the end of the game, assuming Jacquez starts and both players play optimally.
样例输入
3
1 6
1 7 8
5 0 3 4
9 3 2 1 9
样例输出
21
思路
由于只有15个点,因此只需要将所有的点的移动方向枚举出,模拟点的移动获得最大值即可
代码实现
#pragma GCC optimize(3,"Ofast","inline")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
const int N=2e5+10;
const int INF=0x3f3f3f3f;
vector<P>mov[20];
int a[20];
P dfs()
{
int maxd=-INF;
int ft=0,se=0;
for(int i=0;i<15;i++)
{
if(a[i]==0)
{
for(int j=0;j<mov[i].size();j++)
{
int to=mov[i][j].first;
int upto=mov[i][j].second;
if(!a[to] || !a[upto]) continue;
int bfx=a[to],bfu=a[upto];
a[to]=a[upto]=0;
a[i]=bfu;
P t=dfs();
if(t.second+bfx*bfu-t.first>maxd)
{
maxd=t.second+bfx*bfu-t.first;
ft=t.second+bfx*bfu;
se=t.first;
}
a[to]=bfx;
a[upto]=bfu;
a[i]=0;
}
}
}
return P(ft,se);
}
void init()
{
mov[0].push_back(P(1,3));
mov[0].push_back(P(2,5));
mov[1].push_back(P(3,6));
mov[1].push_back(P(4,8));
mov[2].push_back(P(4,7));
mov[2].push_back(P(5,9));
mov[3].push_back(P(1,0));
mov[3].push_back(P(6,10));
mov[3].push_back(P(7,12));
mov[3].push_back(P(4,5));
mov[4].push_back(P(7,11));
mov[4].push_back(P(8,13));
mov[5].push_back(P(8,12));
mov[5].push_back(P(9,14));
mov[5].push_back(P(2,0));
mov[5].push_back(P(4,3));
mov[6].push_back(P(3,1));
mov[6].push_back(P(7,8));
mov[7].push_back(P(4,2));
mov[7].push_back(P(8,9));
mov[8].push_back(P(4,1));
mov[8].push_back(P(7,6));
mov[9].push_back(P(5,2));
mov[9].push_back(P(8,7));
mov[10].push_back(P(6,3));
mov[10].push_back(P(11,12));
mov[11].push_back(P(7,4));
mov[11].push_back(P(12,13));
mov[12].push_back(P(11,10));
mov[12].push_back(P(7,3));
mov[12].push_back(P(8,5));
mov[12].push_back(P(13,14));
mov[13].push_back(P(8,4));
mov[13].push_back(P(12,11));
mov[14].push_back(P(9,5));
mov[14].push_back(P(13,12));
}
int main()
{
init();
for(int i=0;i<15;i++) scanf("%d",&a[i]);
P ans=dfs();
printf("%d\n",ans.first-ans.second);
return 0;
}