根据自己的理解写的,不知道有没有错误,欢迎指教
#include <iostream>
using namespace std;
int map[5][5];
int mp[5][5];
int dir[12][4] = {
1,1,2,2,0,1,0,2,0,-1,0,-2,-1,-1,-2,-2,
-1,1,-2,2,1,0,2,0,-1,0,-2,0,1,-1,2,-2,
0,1,0,-1,1,0,-1,0,-1,1,1,-1,-1,-1,1,1
};
void init()
{
for (int i = 0; i < 5; i++)
for (int j = 0; j < 5; j++)
map[i][j] = 0;
}
int CalcV(int x, int y)
{
int count = 0;
for (int i = 0; i < 10; i++)
{
int x1 = x + dir[i][0]; int y1 = y + dir[i][1];
int x2 = x + dir[i][2]; int y2 = y + dir[i][3];
if (x1 < 1 || x1>3 || x2 < 1 || x2>3 || y1 < 1 || y1>3 || y2 < 1 || y2>3 || (map[x1][y1] != 0) || (map[x2][y2] != 0)) continue;
count += 1;
}
return count;
}
int CalV(int x, int y)
{
int count = 0;
for (int i = 0; i < 12; i++)
{
int x1 = x + dir[i][0]; int y1 = y + dir[i][1];
int x2 = x + dir[i][2]; int y2 = y + dir[i][3];
if (x1 < 1 || x1>3 || x2 < 1 || x2>3 || y1 < 1 || y1>3 || y2 < 1 || y2>3 || (mp[x1][y1] != 0) || (mp[x2][y2] != 0)) continue;
//cout << x1 <<y1<<' '<<x2 << y2 << endl;
count += 1;
}
return count;
}
int findway2(int x,int y,int max)
{
int min = 1000;
mp[x][y] = -1;
for (int i = 1; i < 4; i++)
{
for (int j = 1; j < 4; j++)
{
if (mp[i][j] == 0)
{
mp[i][j] = 1;
int q = CalV(x,y);
//cout << q << endl;
//cout << i << ' ' << j << endl;
//cout << q << endl;
if (min > q) min = q;
mp[i][j] = 0;
}
if (min < max) return -1;//cut
}
}
return min;
}
void findway1(int* x, int *y)
{
int max = 0; int posx = -1, posy = -1;
int gmax = 0;
for (int i = 1; i < 4; i++)
for (int j = 1; j < 4; j++)
mp[i][j] = map[i][j];
for (int i = 1; i < 4; i++)
{
for (int j = 1; j < 4; j++)
{
int min;
if (map[i][j] == 0)
{
//int p=CalcV(i, j);
min = findway2(i,j,max);
cout << min << endl;
mp[i][j] = 0;
if (min < 0) continue;
//cout << min << endl;
//cout << p << endl;
cout << i << ',' << j << endl;
//cout << min << endl;
if (min > max)
{
max = min;
posx = i; posy = j;
}
//if (max < gmax) break;
//gmax = max;
}
}
}
*x = posx, *y = posy;
// cout << posx <<' '<< posy << endl;
}
void print()
{
cout << "*******************" << endl;
for (int i = 1; i < 4; i++)
{
for (int j = 1; j < 4; j++)
cout << map[i][j] << ' ';
cout << endl;
}
}
void randxy(int *x, int *y)
{
}
int main()
{
int flag = 0;
// computer -1,and person 1
int x, y;
int xx, yy;
//map[2][2] = -1;
//cout << findway2(2, 2, 0) << endl;
init();
while (flag<9)
{
print();
findway1(&x, &y);
cout << x << y << endl;
if (x == -1 && y == -1) {
cout << "you win" << endl;
break;
}
map[x][y] = -1;
print();
cin >> xx >> yy;
while (xx < 1 || xx>3 || yy < 1 || yy>3)
{
cout << " input error ,please input again" << endl;
cin >> xx >> yy;
}
map[xx][yy] = 1;
}
print();
int bcnode;
cin >> bcnode;
return 0;
}