The algorithm process is as follows: Enter the start child of the calculation of Qi, check the blank surrounding the chess piece, and check whether the blank has been computed. If not, if it has been calculated, it will be skipped, and then recursively call to calculate the air of the left and right sub-pieces of our chess piece. Finally, the algorithm will return the sub-numbers and gas numbers of the chess string. Algorithm annotations are very detailed.
For more information about the algorithm used to calculate the chess component, see the algorithm. This blog post introduces more about go algorithms. If you are interested, you can read them further.
In the original article, the suanqi function does not calculate the efficiency of the computed chess string repeatedly. We should know the length of the chess string if we do not calculate the function that has been computed.
#include <iostream>#include <utility>#define EDGE 19int go[EDGE][EDGE];//棋盘数据,0为黑棋,1为白棋,2为空白int gokong[EDGE][EDGE]; //0=该空点未曾计算过气,1=已计算,避免重复计算公气int gozi[EDGE][EDGE]; //0=该子未计算串气,1=已计算,避免重复计算同一个子的气int goqi; //此棋串气数int goziLength;//此棋串子数int g_gozi[EDGE][EDGE]; void str_qi(int x,int y,int hb) { //本函数计算 x,y 处的hb颜色棋子的气 gozi[x][y]=1; //标记本子已经计算过气 goziLength++; /////////////////////////////////////////////////////右临子 if (x+1<=19)//如果没有超出棋盘边线 { if ((go[x+1][y]==2)&&(gokong[x+1][y]==0)) //如果右临点为空并且该点未曾计算过气则 { goqi++; //气数加一 gokong[x+1][y]=1; //标记本空点已经计算过气 } else if ((go[x+1][y]==hb)&&(gozi[x+1][y]==0)) //否则如果右临点为和本子同色子并且该子未曾计算过气则 str_qi(x+1,y,hb); //递归调用到右临子 } /////////////////////////////////////////////////////左临子 if (x-1>=1) //果没有超出棋盘边线 { if ((go[x-1][y]==2)&&(gokong[x-1][y]==0)) //如果左临点为空并且该点未曾计算过气则 { goqi++; //气数加一 gokong[x-1][y]=1; //标记本空点已经计算过气 } else if ((go[x-1][y]==hb)&&(gozi[x-1][y]==0)) //否则如果左临点为和本子同色子并且该子未曾计算过气则 str_qi(x-1,y,hb); //递归调用到左临子 } ////////////////////////////////////////////////////下临子 if (y-1>=1)//如果没有超出棋盘边线 { if ((go[x][y-1]==2)&&(gokong[x][y-1]==0)) //如果下临点为空并且该点未曾计算过气则 { goqi++; //气数加一 gokong[x][y-1]=1; //标记本空点已经计算过气 } else if ((go[x][y-1]==hb)&&(gozi[x][y-1]==0)) //否则如果下临子点为和本子同色子并且该子未曾计算过气则 str_qi(x,y-1,hb); //递归调用到下临子 } ////////////////////////////////////////////////////上临点 if (y+1<=19)//如果没有超出棋盘边线 { if ((go[x][y+1]==2)&&(gokong[x][y+1]==0)) //如果上临点为空并且该点未曾计算过气则 { goqi++; //气数加一 gokong[x][y+1]=1; //标记本空点已经计算过气 } else if ((go[x][y+1]==hb)&&(gozi[x][y+1]==0)) //否则如果上临点为和本子同色子并且该子未曾计算过气则 str_qi(x,y+1,hb); //递归调用到上临子 } } std::pair<int,int> str_lib(int x,int y, int hb) { for (int i = 1; i <= 19; i++) for (int j = 1; j <= 19; j++) { gozi[i][j] = 0; //初始化变量,表示该子未计算串气 gokong[i][j] = 0; //初始化变量,表示该空点未计算串气 } goqi=0; //棋串气初值 goziLength=0;//棋串初始子数 str_qi(x,y,hb); //调用串气子程序 for(int i=0;i<EDGE;i++) for(int j=0;j<EDGE;j++){ if(gozi[i][j]==1) g_gozi[i][j]=1; } return std::make_pair<int,int>(goqi,goziLength); } void suanqi() { int qq; for (int i = 0; i <EDGE; i++) for (int j = 0; j <EDGE; j++) { go[i][j]=qipan[i][j].color;//初始化go数组 } for (int i = 0; i <EDGE; i++) for (int j = 0; j <EDGE; j++) { if (go[i][j]!=2&&g_gozi[i][j]==0)//仅处理未计算过的棋串 { qq=str_lib(i,j,go[i][j]).first; qipan[i][j].qs=qq; } } }
Indicate the source for reprinting. Thank you.
Man-Machine game-play chess game (2) computing