程序代码:
#include <stdio.h>
#define MaxN 10
#define Grass 0
#define A 1
#define B 2
#define Jia 3
#define Jian 4
#define Water 5
#define PriceJia 50
#define PriceJian 100
#define Win 1
#define Lost 2
#define CannotMove 3
#define None 4
#define Front 1
#define Back 2
#define Left 3
#define Right 4
#define BuyJia 5
#define BuyJian 6
int depth;
int bestX, bestY, bestFs;
int ys;
int UseJia, UseJian;
int Xua [MaxN + 2][MaxN + 2] = {0},
Hap [MaxN + 2][MaxN + 2] = {0},
Alw [MaxN + 2][MaxN + 2] = {0},
What[MaxN + 2][MaxN + 2] = {0};
int N;
int Who;
int MoneyA, MoneyB;
int MoveX[100], MoveY[100], MoveFs[100], MoveLen = 0;
int min(int a, int b)
{
return a < b ? a : b;
}
int GetValue()
{
int value = 0, meMaxHappy = 0, meMaxMoved = 0,
otherMaxHappy = 0, otherMaxMoved = 0,
meMoney = 0, otherMoney = 0;
int i, j;
for (i = 1; i <= N; i++)
{
for (j = 1; j <= N; j++)
{
if (What[i][j] == A || What[i][j] == B)
{
int temp = min(i, j);
temp = min(temp, N - i + 1);
temp = min(temp, N - j + 1);
if (What[i][j] == ys)
{
value += temp + 10;
if (Xua[i][j] == 0)
{
if (Hap[i][j] > meMaxHappy)
meMaxHappy = Hap[i][j];
if (Alw[i][j] > meMaxMoved)
meMaxMoved = Alw[i][j];
}
}
else
{
value -= temp + 10;
if (Xua[i][j] == 0)
{
if (Hap[i][j] > otherMaxHappy)
otherMaxHappy = Hap[i][j];
if (Alw[i][j] > otherMaxMoved)
otherMaxMoved = Alw[i][j];
}
}
}
}
}
switch (ys)
{
case A:
meMoney = MoneyA;
otherMoney = MoneyB;
break;
case B:
meMoney = MoneyB;
otherMoney = MoneyA;
break;
}
if (meMaxHappy < otherMaxMoved)
value -= 100;
if (otherMaxHappy < meMaxMoved)
value += 100;
if (meMaxHappy + meMoney / PriceJia < otherMaxMoved + otherMoney / PriceJian)
value -= 200;
if (otherMaxHappy + otherMoney / PriceJia < meMaxMoved + meMoney / PriceJian)
value += 200;
return value;
}
int TimePass(int y)
{
int itFind = 0, otherFind = 0, itCanMove = 0;
int i, j;
for (i = 1; i <= N; i++)
{
for (j = 1; j <= N; j++)
if ((What[i][j] == A || What[i][j] == B) && Xua[i][j] != 0)
Xua[i][j]--;
}
for (i = 1; i <= N; i++)
{
for (j = 1; j <= N; j++)
{
if (What[i][j] == A || What[i][j] == B)
{
if (What[i][j] == y)
{
itFind = 1;
if (Xua[i][j] == 0)
itCanMove = 1;
}
if (What[i][j] == A + B - y)
otherFind = 1;
}
}
}
if (!itFind)
return Lost;
if (!otherFind)
return Win;
if (!itCanMove)
return CannotMove;
return None;
}
int Not(int mfs)
{
switch (mfs)
{
case Front:
return Back;
case Back:
return Front;
case Left:
return Right;
case Right:
return Left;
}
}
int GetX(int x, int mfs)
{
switch (mfs)
{
case Front:
case Back:
return x;
case Left:
return x - 1;
case Right:
return x + 1;
}
}
int GetY(int y, int mfs)
{
switch (mfs)
{
case Front:
return y - 1;
case Back:
return y + 1;
case Left:
case Right:
return y;
}
}
void Move(int x, int y, int mfs, int mmove)
{
MoveLen = 0;
if (!(What[x][y] == A || What[x][y] == B))
return;
int fbX = x, fbY = y;
if (Front <= mfs && mfs <= Right)
{
int notMfs = Not(mfs);
int can = 0;
int sumHap = 0;
while (sumHap <= Hap[fbX][fbY])
{
x = GetX(x, mfs);
y = GetY(y, mfs);
if (What[x][y] == A || What[x][y] == B)
{
sumHap += Alw[x][y];
if (sumHap > Hap[fbX][fbY])
return;
}
else
{
can = 1;
break;
}
}
if (!can)
{
x = GetX(x, mfs);
y = GetY(y, mfs);
if (What[x][y] == A || What[x][y] == B)
return;
}
int tempX = x, tempY = y;
x = GetX(x, notMfs);
y = GetY(y, notMfs);
if (What[tempX][tempY] != Water && mmove)
{
Xua [tempX][tempY] = Xua [x][y];
Hap [tempX][tempY] = Hap [x][y];
Alw [tempX][tempY] = Alw [x][y];
What[tempX][tempY] = What[x][y];
if (What[tempX][tempY] == Jia)
Hap[tempX][tempY]++;
else if (What[tempX][tempY] == Jian)
Xua[tempX][tempY]++;
}
MoveX [MoveLen] = x;
MoveY [MoveLen] = y;
MoveFs[MoveLen] = mfs;
MoveLen++;
if (x != fbX || y != fbY)
{
for (x = GetX(x, notMfs), y = GetY(y, notMfs);
GetX(x, mfs) != fbX || GetY(y, mfs) != fbY;
x = GetX(x, notMfs), y = GetY(y, notMfs))
{
tempX = GetX(x, mfs);
tempY = GetY(y, mfs);
if (mmove)
{
Xua [tempX][tempY] = Xua [x][y];
Hap [tempX][tempY] = Hap [x][y];
Alw [tempX][tempY] = Alw [x][y];
What[tempX][tempY] = What[x][y];
}
MoveX [MoveLen] = x;
MoveY [MoveLen] = y;
MoveFs[MoveLen] = mfs;
MoveLen++;
}
}
if (mmove)
What[fbX][fbY] = Grass;
}
else if (mfs == BuyJia)
{
switch (Who)
{
case A:
if (MoneyA >= PriceJia && UseJia)
{
MoveX [MoveLen] = x;
MoveY [MoveLen] = y;
MoveFs[MoveLen] = mfs;
MoveLen++;
if (mmove)
{
Hap[x][y]++;
MoneyA -= PriceJia;
}
}
else
return;
break;
case B:
if (MoneyB >= PriceJia && UseJia)
{
MoveX [MoveLen] = x;
MoveY [MoveLen] = y;
MoveFs[MoveLen] = mfs;
MoveLen++;
if (mmove)
{
Hap[x][y]++;
MoneyB -= PriceJia;
}
}
else
return;
break;
}
}
else if (mfs == BuyJian)
{
switch (Who)
{
case A:
if (MoneyA >= PriceJian && UseJian)
{
MoveX [MoveLen] = x;
MoveY [MoveLen] = y;
MoveFs[MoveLen] = mfs;
MoveLen++;
if (mmove)
{
Hap[x][y]++;
MoneyA -= PriceJian;
}
}
else
return;
break;
case B:
if (MoneyB >= PriceJian && UseJian)
{
MoveX [MoveLen] = x;
MoveY [MoveLen] = y;
MoveFs[MoveLen] = mfs;
MoveLen++;
if (mmove)
{
Hap[x][y]++;
MoneyB -= PriceJian;
}
}
else
return;
break;
}
}
}
int CanKillOurs(int x, int y, int mfs)
{
if (Front <= mfs && mfs <= Right)
{
if (What[x][y] != A && What[x][y] != B)
return 0;
while (What[x][y] == A || What[x][y] == B)
{
x = GetX(x, mfs);
y = GetY(y, mfs);
}
if (What[x][y] == Water && What[GetX(x, Not(mfs))][GetY(y, Not(mfs))] == Who)
return 1;
else
return 0;
}
else
return 0;
}
int CleverThink(int depthNow, int alpha, int beta)
{
if (depthNow == 0)
return GetValue();
if (depthNow != depth * 2)
{
Who = A + B - Who;
switch (TimePass(Who))
{
case Win:
if (ys == Who)
return -1000 * depthNow;
else
return 1000 * depthNow;
case Lost:
if (ys == Who)
return 1000 * depthNow;
else
return -1000 * depthNow;
case CannotMove:
return -CleverThink(depthNow - 1, -beta, -alpha);
}
}
int fbXua [MaxN + 2][MaxN + 2] = {0},
fbHap [MaxN + 2][MaxN + 2] = {0},
fbAlw [MaxN + 2][MaxN + 2] = {0},
fbWhat[MaxN + 2][MaxN + 2] = {0},
fbWho, fbMoneyA, fbMoneyB;
int i, j, k, l, fs;
for (i = 0; i <= N + 1; i++)
{
for (j = 0; j <= N + 1; j++)
{
fbXua [i][j] = Xua [i][j];
fbHap [i][j] = Hap [i][j];
fbAlw [i][j] = Alw [i][j];
fbWhat[i][j] = What[i][j];
}
}
fbWho = Who;
fbMoneyA = MoneyA;
fbMoneyB = MoneyB;
for (i = 1; i <= N; i++)
{
for (j = 1; j <= N; j++)
{
if (What[i][j] == Who && Xua[i][j] == 0)
{
for (fs = Front; fs <= BuyJian; fs++)
{
Move(i, j, fs, 0);
if (MoveLen > 0 && !CanKillOurs(i, j, fs))
{
if (Front <= fs && fs <= Right)
{
for (k = 0; k < MoveLen - 1; k++)
Xua[MoveX[k]][MoveY[k]] = 2;
}
Move(i, j, fs, 1);
int value = -CleverThink(depthNow - 1, -beta, -alpha);
for (k = 0; k <= N + 1; k++)
{
for (l = 0; l <= N + 1; l++)
{
Xua [k][l] = fbXua [k][l];
Hap [k][l] = fbHap [k][l];
Alw [k][l] = fbAlw [k][l];
What[k][l] = fbWhat[k][l];
}
}
Who = fbWho;
MoneyA = fbMoneyA;
MoneyB = fbMoneyB;
if (value >= beta && beta != -2147483647)
return value;
if (value > alpha)
{
alpha = value;
if (depth * 2 == depthNow)
{
bestX = i;
bestY = j;
bestFs = fs;
}
}
}
}
}
}
}
return alpha;
}
int main()
{
freopen("ai.in", "r", stdin);
freopen("ai.out", "w", stdout);
int i;
scanf("%d %d", &UseJia, &UseJian);
scanf("%d %d %d %d", &N, &Who, &MoneyA, &MoneyB);
int x, y, xuanyun, happiness, alwaysMoved;
do
{
scanf("%d %d %d %d %d", &x, &y, &xuanyun, &happiness, &alwaysMoved);
Xua[x][y] = xuanyun;
Hap[x][y] = happiness;
Alw[x][y] = alwaysMoved;
What[x][y] = A;
}
while (x != 0);
do
{
scanf("%d %d %d %d %d", &x, &y, &xuanyun, &happiness, &alwaysMoved);
Xua[x][y] = xuanyun;
Hap[x][y] = happiness;
Alw[x][y] = alwaysMoved;
What[x][y] = B;
}
while (x != 0);
do
{
scanf("%d %d", &x, &y);
What[x][y] = Jia;
}
while (x != 0);
do
{
scanf("%d %d", &x, &y);
What[x][y] = Jian;
}
while (x != 0);
for (i = 0; i <= N + 1; i++)
{
What[0 ][i ] = Water;
What[i ][0 ] = Water;
What[N + 1][i ] = Water;
What[i ][N + 1] = Water;
}
ys = Who;
depth = 3;
CleverThink(depth * 2, -2147483647, 2147483647);
printf("%d\n", bestX);
printf("%d\n", bestY);
switch (bestFs)
{
case Front:
printf("F");
break;
case Back:
printf("B");
break;
case Left:
printf("L");
break;
case Right:
printf("R");
break;
case BuyJia:
printf("Jia");
break;
case BuyJian:
printf("Jian");
}
printf("\n");
return 0;
}
我写的AI,没加注释