没事写着玩玩,通过debian上的黑白棋测试,搜了10层,打hard应该问题不大

#include <cstdio>
#include <cstring> using namespace std; template <typename T> T min(const T &x, const T &y) { return x < y ? x : y; }
template <typename T> T max(const T &x, const T &y) { return x > y ? x : y; } const int weight[10][10] =
{
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 65, -3, 6, 4, 4, 6, -3, 65, 0,
0, -3, -29, 3, 1, 1, 3, -29, -3, 0,
0, 6, 3, 5, 3, 3, 5, 3, 6, 0,
0, 4, 1, 3, 1, 1, 3, 1, 4, 0,
0, 4, 1, 3, 1, 1, 3, 1, 4, 0,
0, 6, 3, 5, 3, 3, 5, 3, 6, 0,
0, -3, -29, 3, 1, 1, 3, -29, -3, 0,
0, 65, -3, 6, 4, 4, 6, -3, 65, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
};//苟的debian黑白棋AI的权值表
const int d[8][2] =
{
-1, 0, 1, 0, 0, -1, 0, 1,
-1, -1, -1, 1, 1, -1, 1, 1
};
#define re register
int state[10][10], ansx, ansy;
//black:1 white:-1
int cur[10][10];
inline int alpha_beta(int dep, int player, int alpha, int beta, int cnt) //0 for me 1 for it
{
if(dep > 9 || cnt > 64) //dep是搜索的层数
{
int ret = 0;
for(re int i = 1; i <= 8; ++i)
for(re int j = 1; j <= 8; ++j)
ret += weight[i][j]*state[i][j];
return ret;
}
int temp[10][10], tot = 0;
memcpy(temp, state, sizeof state);
if(!player)
for(re int id = 1, x, y, i, j; id <= 64; ++id)
{
i = (id-1>>3)+1, j = (id-1)%8+1;
if(state[i][j] == 0)
{
int flag = 0, t;
for(re int k = 0; k < 8; ++k)
{
t = 0, x = i+d[k][0], y = j+d[k][1];
while(state[x][y] == -1) t++, x += d[k][0], y += d[k][1];
if(t && state[x][y] == 1)
{
do
{
x -= d[k][0], y -= d[k][1];
state[x][y] = 1;
} while(x != i || y != j);
flag = 1;
}
}
if(flag)
{
tot++;
int v = alpha_beta(dep+1, player^1, alpha, beta, cnt+1);
if(v > alpha)
{
if(!dep) ansx = i, ansy = j, memcpy(cur, state, sizeof state);
alpha = v;
}
memcpy(state, temp, sizeof temp);
if(beta <= alpha) break;
}
}
}
if(!tot) player = 1;
if(player)
{
for(re int id = 1, x, y, tx, ty, i, j; id <= 64; ++id)
{
i = (id-1>>3)+1, j = (id-1)%8+1;
if(state[i][j] == 0)
{
int flag = 0, t;
for(re int k = 0; k < 8; ++k)
{
t = 0, x = i+d[k][0], y = j+d[k][1];
while(state[x][y] == 1) t++, x += d[k][0], y += d[k][1];
if(t && state[x][y] == -1)
{
do
{
x -= d[k][0], y -= d[k][1];
state[x][y] = -1;
} while(x != i || y != j);
flag = 1;
}
}
if(flag)
{
int v = alpha_beta(dep+1, player^1, alpha, beta, cnt+1);
if(beta > v) beta = v;
memcpy(state, temp, sizeof temp);
if(beta <= alpha) break;
}
}
}
}
return !player ? alpha : beta;
} int main()
{
memset(cur, 0, sizeof cur);
cur[4][4] = cur[5][5] = -1, cur[4][5] = cur[5][4] = 1;
for(int i = 0; i < 10; ++i) cur[0][i] = cur[9][i] = -2;
for(int i = 1; i < 9; ++i) cur[i][0] = cur[i][9] = -2;
for(int i = 0, tx, ty; i < 32; ++i)
{
memcpy(state, cur, sizeof cur);
ansx = -1, ansy = -1;
alpha_beta(0, 0, -1000000000, 1000000000, i*2);
if(ansx != -1)
{
printf("----------\n");
printf("you should put here:%d %d\n", ansx, ansy);
for(int p = 1; p <= 8; ++p)
{
for(int q = 1; q <= 8; ++q) printf("%d", cur[p][q] < 0 ? 2 : cur[p][q]);
printf("\n");
}
printf("----------\n");
}
printf("Enter your competitor's move(If he can't even move,just type -1 -1):"); scanf("%d%d", &tx, &ty);
if(tx == -1) continue;
for(int k = 0, x, y, t; k < 8; ++k)
{
t = 0, x = tx+d[k][0], y = ty+d[k][1];
while(cur[x][y] == 1) t++, x += d[k][0], y += d[k][1];
if(t && cur[x][y] == -1)
{
do
{
x -= d[k][0], y -= d[k][1];
cur[x][y] = -1;
} while(x != tx || y != ty);
}
}
}
return 0;
}

最新文章

  1. equals和==的区别 你真的掌握了吗?
  2. 《DSP using MATLAB》 示例Example4.1
  3. JavaScript学习笔记及知识点整理_1
  4. iOS - (简单平移动画/弹出View的使用)
  5. Android开发中出现cannot be resolved to a variable错误,也就是R文件不能生成。
  6. 基于visual Studio2013解决面试题之0710求方优化
  7. Spring mvc配置Json返回
  8. UNIX网络编程——UDP 中的外出接口的确定
  9. svn中出现各种感叹号说明
  10. vuecli3 项目添加配置文件以及使用@映射、代理
  11. js通过查看屏幕大小,更改其他css属性
  12. TortoiseSVN常用配置
  13. freeSSHd (Auth fail)错误!以及Xmanager的(ssh服务器拒绝了密码,请再试一次)错误!
  14. 软件测试_Loadrunner_APP测试_性能测试_脚本优化_脚本回放
  15. [转]TortoiseSVN客户端重新设置用户名和密码
  16. android onActivityResult的执行
  17. Python 爬虫笔记(二)
  18. yii\bootstrap
  19. Visual Studio警告IDE0006的解决办法
  20. CodeForces - 13D :Triangles(向量法:问多少个蓝点三角形内部无红点)

热门文章

  1. Mybatis入门之动态sql
  2. PHP进程信号处理
  3. React create-react-app Build fails after eject: Cannot find module &#39;@babel/plugin-transform-react-jsx&#39;
  4. WEB前端需要了解的XML相关基础知识
  5. Android 报错:error: too many padding sections on bottom border
  6. vue的表单编辑删除,保存取消功能
  7. 记一次zabbix排错(数据库安装在其它服务器上)
  8. idea右键无法生成javaclass
  9. Azure存储账户的日志分析方法
  10. 使用栈实现队列(2)(Java)