Exemplo n.º 1
0
void dominantVec(int width, int height, int blockSize ,IN int *input_x , int *input_y ,OUT int *output) {

    int numSubFrames = ((width + blockSize - width%blockSize)/blockSize)*
                       ((height + blockSize - height%blockSize)/blockSize);

    int startVal = 1 ;
    ResCon result = getCandidate (startVal ,input_x ,input_y , numSubFrames);
    int numbofLoops = 1 ;
    int count = result.count ;
    int index = result.index ;

    while (numbofLoops < numSubFrames && count < numSubFrames/3){
        startVal++;
        result = getCandidate(startVal ,input_x ,input_y , numSubFrames);
        numbofLoops++ ;
        if(count < result.count){
            index = result.index ;
            count = result.count ;
        }
    }
	memset(output, *(input_x+result.index) , 1);
	output ++;
	memset(output, *(input_y+result.index) , 1);
   }
Exemplo n.º 2
0
/*
 @brief alpah-beta搜索函数,寻找最适合的一个放子点
 @param board 棋局
 @param depth 当前深度
 @param alpha MAX结点的搜索下限
 @param beta MIN结点的搜索上限
 @param team 当前要下子的一方
 @return 此时的alpha值
 */
int AlphaBeta(int **oriBoard, int depth, int alpha, int beta, PieceTeam team)
{
    int **board = copy2DIntArray(oriBoard, kBoardRow, kBoardCol);
    
    if (depth == 0) {
        // 评估分综合双方棋局得到
        int s1 = Evaluate(board, team);
        int s2 = Evaluate(board, AnotherTeam(team));
        int score = s1-s2;
        return score;
    }
    
    SeqIntPt candi = getCandidate(board);
    
    // 先对所有可扩展结点进行估价,并排序
    SortType sortedCandi;
    for (SeqIntPt::iterator iter=candi.begin(); iter!=candi.end(); ++iter){
        int score = sortEvaluation(board, team, *iter);
        sortedCandi.insert(SortNode(score, iter->r, iter->c));
    }
    
    // 根据启发函数对可扩展结点排序后的顺序来扩展
    for(SortType::iterator it=sortedCandi.begin(); it!=sortedCandi.end(); ++it)
    {
        GBIntPoint pos = it->pos;
        if (board[pos.r][pos.c] != PieceTeamNone)
            continue;
        board[pos.r][pos.c] = team;
        
        // 第一层的剪枝优化:如果己方或对方在此处有必赢棋局,直接放在此处
        if (depth == InDepth) {
            int tscore = oneStepEvaluation(board, team, pos);
//            printf("candidate: %d, %d; score: %d\n",pos.r, pos.c, tscore);
            if (tscore>alpha){  //评分80及以上的局势都是必赢的局
//                for (int i=kBoardRow-1; i>=0; --i) {
//                    for (int j=0; j<kBoardCol; ++j) {
//                        printf("%d ", board[i][j]);
//                    }
//                    printf("\n");
//                }
//                printf("%d\n", tscore);
//                Evaluate(board, team);
                
                alpha = tscore;
                BestPiece.r = pos.r;
                BestPiece.c = pos.c;
            }
        }
        
        // 递归搜索
        int val = -AlphaBeta(board, depth - 1, -beta, -alpha, AnotherTeam(team));
        
        // 还原现场
        board[pos.r][pos.c] = PieceTeamNone;
        
        // alpha-beta 剪枝
        if (val >= beta) {
            delete2DIntArray(board);
            return beta;
        }
        if (val>alpha && board[pos.r][pos.c]==PieceTeamNone) {
            alpha = val;
            if (depth == InDepth){  // 第一层,更新最优位置
                BestPiece.r = pos.r;
                BestPiece.c = pos.c;
            }
        }
    }
    
    delete2DIntArray(board);
    return alpha;
}