node* binary_tree::BinayTree_LowerestCommonAncestor(int x,int y)
{
  node*tmp1=Binary_search(x);
  node*tmp2=Binary_search(y);
  if(tmp1==NULL || tmp2==NULL)
    return NULL;
  return LowerestCommonAncestor(root,tmp1,tmp2);
}
Exemplo n.º 2
0
/**
 * @brief
 *
 * @param source
 * @param low_index
 * @param high_index
 * @param target
 * @param option
 *
 * @return
 */
int Binary_search(int *source, int low_index, int high_index, int target,
                  int option) {
  int mid_index;
  mid_index = (low_index + high_index) / 2;

  if (low_index > high_index) return -1;

  if (source[mid_index] == target)
    return mid_index;
  else if (source[mid_index] < target) {
    if (option == ASCENDING_ORDER)
      return Binary_search(source, mid_index + 1, high_index, target, option);
    else
      return Binary_search(source, low_index, mid_index - 1, target, option);
  } else {
    if (option == ASCENDING_ORDER)
      return Binary_search(source, low_index, mid_index - 1, target, option);
    else
      return Binary_search(source, mid_index + 1, high_index, target, option);
  }
}
Exemplo n.º 3
0
MoveAndScoreIndex Book::GetProbe(const Position& position, const std::string& fName, const bool pickBest) {
	BookEntry entry;
	u16 best = 0;
	u32 sum = 0;
	Move move = g_MOVE_NONE;//該当なしのときに使う値☆
	const Key key = this->GetBookKey(position);
	const ScoreIndex min_book_score = static_cast<ScoreIndex>(static_cast<int>(position.GetRucksack()->m_engineOptions["Min_Book_Score"]));
	ScoreIndex score = ScoreZero;

	if (this->m_fileName_ != fName && !this->OpenBook(fName.c_str())) {
		// 定跡ファイルが開けなかった場合☆
		return MoveAndScoreIndex(g_MOVE_NONE, ScoreNone);
	}

	Binary_search(key);

	// 現在の局面における定跡手の数だけループする。
	while (read(reinterpret_cast<char*>(&entry), sizeof(entry)), entry.m_key == key && good()) {
		best = std::max(best, entry.m_count);
		sum += entry.m_count;

		// 指された確率に従って手が選択される。
		// count が大きい順に並んでいる必要はない。
		if (min_book_score <= entry.m_score
			&& ((m_random_.GetRandom() % sum < entry.m_count)
				|| (pickBest && entry.m_count == best)))
		{
			const Move tmp = Move(entry.m_fromToPro);
			const Square to = tmp.To();
			if (tmp.IsDrop()) {
				const PieceType ptDropped = tmp.GetPieceTypeDropped();
				move = ConvMove::Convert30_MakeDropMove_da(ConvMove::FROM_PIECETYPE_DA10(ptDropped), to);
			}
			else {
				// 定跡の手(持ち駒以外)を、ムーブの書式に変換している??(^q^)?
				const Square from = tmp.From();
				const Move fromMove = ConvMove::FROM_PIECETYPE_ONBOARD10(ConvPiece::TO_PIECE_TYPE10(position.GetPiece(from)));
				if (tmp.IsPromotion()) {
					move = UtilMovePos::BUILD_CARD_CAPTURE_PROMOTE(position,fromMove, from, to);
				}
				else {
					move = UtilMovePos::BUILD_CARD_CAPTURE(position, fromMove, from, to);
				}
			}
			score = entry.m_score;
		}
	}

	return MoveAndScoreIndex(move, score);
}
Exemplo n.º 4
0
/**
 * @brief
 *
 * @param source
 * @param size
 * @param target
 *
 * @return
 */
int search(int *source, int size, int target) {
  return Binary_search(source, 0, size - 1, target, ASCENDING_ORDER);
  /* return Random_search(source,0,size-1,target);  */
  /* return Deterministic_search(source,0,size-1,target); */
  /* return Scramble_search(source,0,size-1,target); */
}