int strategy( const int hd[], const int fd[], int cg, int tk, const int ud[], int us) { int myhd[HNUM]; int select = -1; int hdcopy[CNUM]; int udcopy[us]; int currentpoint = 0; int deck[CNUM] = { 0 }; int decknum = CNUM; int k; int lastchangehd = 0; // 最初から高い点なら手札を変えずに終了 arr_copy( hdcopy, hd, HNUM); currentpoint = poker_point(hdcopy); if ( currentpoint >= P6 ) { return -1; } arr_copy( udcopy, ud, us ); decknum = make_deck( hdcopy, udcopy, us, deck ); if ( tk == 4 && decknum <= 7 - cg ) { //LAST_RECURSION_LIMIT = decknum; select = select_card_last( hdcopy, cg, tk, udcopy, us, deck, decknum ); } else { select = select_card( hdcopy, cg, tk, udcopy, us, deck, decknum ); } return select; }
void warshall(const int rel[][NUM], int arr0[][NUM], int n) { int arr1[NUM][NUM]; arr_copy(rel, arr0, n); arr_copy(rel, arr1, n); while ( 1 ) { arr_copy(arr0, arr1, n); arr_logical_multi(arr0, arr1, rel, n); if ( arr_match(arr0, arr1, n) == true ) { break; } } }
int poker_take(const int stock[], int tk, int used[], int *us) { //---- 局所変数 int field[FNUM]; // 場札 int hand[HNUM]; // 手札 int state[CHNG+1][HNUM]; // 局面(手札の列) int ope[CHNG]; // 打手札 int the; // 打手 int cg; // チェンジ回数 int take_p; // テイク得点 int k; // 反復変数 //---- テイクの準備 if ( *us +5 < CNUM ) { arr_copy(hand, &stock[*us], HNUM); // 手札の配布(5枚) arr_copy(state[0], hand, HNUM); // 局面の記録 } else { return 0; } //---- テイクの最中(チェンジの繰返し) for ( cg = 0; cg < CHNG; cg++ ) { if ( *us < CNUM-5 ) { the = strategy(hand, field, cg, tk, used, *us); // 戦略により捨札を決定 } else { the = -1; break; } ope[cg] = hand[the]; // 打手札の記録 if ( the < 0 ) { break; } // 手札を交換せずにテイク終了 field[cg] = ope[cg]; // 捨札を場札に追加 used[(*us)++] = ope[cg]; // 捨札を捨札配列に追加 hand[the] = stock[HNUM-1+(*us)]; // 手札を交換 arr_copy(state[cg+1], hand, HNUM); // 局面の記録 } //---- テイクの後処理 take_p = poker_point(hand); // テイク得点 for ( k = 0; k < HNUM; k++ ) { used[*us] = hand[k]; // 手札を捨札配列に追加 (*us)++; } //---- テイクの経過表示 if ( Disp_Mode ) { take_show(state, ope, field, cg, take_p); } return take_p; }
void calcexp( int hd[], int cg, int tk, int pointhd[], int pointnum, int deck[], int decknum, int recursioncount, int starthd ) { int nexthd[HNUM]; int nextdeck[CNUM]; int point = 0; int i, j; for ( i = starthd; i < HNUM; i++) { if ( pointnum != -1 ) { //printf("%d, %d, %d\n", pointnum, i, decknum); } for ( j = 0; j < CNUM; j++) { if ( deck[j] == 0 ) { arr_copy( nexthd, hd, HNUM ); nexthd[i] = j; arr_copy( nextdeck, deck, CNUM ); nextdeck[j] = -1; if ( pointnum == -1 ) { // 一手目の期待値の加算 //pointhd[i] += poker_point(nexthd)*(decknum*(LIMIT+1-recursioncount)); pointhd[i] += poker_point(nexthd)*decknum; //pointhd[i] += pointconvert(poker_point(nexthd))*decknum; } else { // 二手目以降の期待値の加算 point = poker_point(nexthd); //point = pointconvert(poker_point(nexthd)); pointhd[pointnum] += point; if ( pointnum < i ) { pointhd[i] += point; } } //if ( tk < 1 || tk > 3 ) { continue; } if ( cg > 7 - LIMIT ) { continue; } if ( recursioncount >= LIMIT ) { continue; } if ( pointnum == -1 ) { calcexp( nexthd, cg+1, tk, pointhd, i, nextdeck, decknum-1, recursioncount+1, i ); } else { calcexp( nexthd, cg+1, tk, pointhd, pointnum, nextdeck, decknum-1, recursioncount+1, i ); } } } } }
void search(int ni, int n, int* save, int si, int sn, pal l) { int k = 0; if (si == sn) { al_add_last(l, arr_copy(save, sn)); return; } for (k = ni; k <= n; k ++) { save[si] = k; search(k+1, n, save, si+1, sn, l); } }
void Insertion(int* org, int n) { int* arr = arr_copy(org, n); for (size_t i = 1; i < n; i++) { for (size_t j = 0; j < i; j++) { if (arr[i] < arr[j]) { swap(&arr[i], &arr[j]); } } } display(org, arr, n, "Insertion"); }
/* 稳定排序: 冒泡 Bubble 插入 Insertion 桶 Bucket 计数 Counting 基数 Radix ? 合并 Merge 二叉(排序)树 ? */ void Bubble(int* org, int n) { int *arr = arr_copy(org, n); for (size_t i = 0; i < n-1; i++) { for (size_t j = 0; j < n-i-1; j++) { if (arr[j] > arr[j+1]) { swap(&arr[j], &arr[j+1]); } } } display(org, arr, n, "Bubble"); }
int strategy( const int hd[], const int fd[], int cg, int tk, const int ud[], int us) { int myhd[HNUM]; int select = -1; int hdcopy[CNUM]; int udcopy[us]; int currentpoint = 0; int deck[CNUM] = { 0 }; int decknum = CNUM; int k; // 最初から高い点なら手札を変えずに終了 arr_copy( hdcopy, hd, HNUM); currentpoint = poker_point(hdcopy); if ( currentpoint >= P6 ) { return -1; } arr_copy( udcopy, ud, us ); decknum = makedeck( hdcopy, udcopy, us, deck ); select = selectcard(hdcopy, cg, tk, udcopy, us, deck, decknum ); return select; }
void search(ptn n, int c, int s, int* sign, int* r, int ri, pal l, pal ln) { if (*sign) return; if (n == NULL) return; r[ri] = n->val; search(n->left, c+n->val, s, sign, r, ri+1, l, ln); search(n->right, c+n->val, s, sign, r, ri+1, l, ln); if (n->left == NULL && n->right == NULL && c+n->val == s) { al_add_last(l, arr_copy(r, ri+1)); al_add_last(ln, arr_con(ri+1)); } }
void calc_exp( int hd[], int cg, int tk, int pointhd[], int pointnum, int deck[], int decknum, int recursioncount, int starthd ) { int nexthd[HNUM]; int nextdeck[CNUM]; int point = 0; int i, j; for ( i = starthd; i < HNUM; i++) { for ( j = 0; j < CNUM; j++) { // 山札にカードがあれば if ( deck[j] == 0 ) { // 手札とデッキの複製 arr_copy( nexthd, hd, HNUM ); nexthd[i] = j; arr_copy( nextdeck, deck, CNUM ); nextdeck[j] = -1; if ( recursioncount == 1 ) { // 一手目の期待値の加算 pointhd[i] += point_convert(poker_point(nexthd))*decknum; } else { // 二手目の期待値の加算 point = point_convert(poker_point(nexthd)); pointhd[pointnum] += point; if ( pointnum < i ) { pointhd[i] += point; } } if ( cg > 7 - RECURSION_LIMIT ) { continue; } if ( recursioncount >= RECURSION_LIMIT ) { continue; } // 再帰呼び出し if ( pointnum == -1 ) { calc_exp( nexthd, cg+1, tk, pointhd, i, nextdeck, decknum-1, recursioncount+1, i ); } else { calc_exp( nexthd, cg+1, tk, pointhd, pointnum, nextdeck, decknum-1, recursioncount+1, i ); } } } } }
void Counting(int* org, int n) { int* arr = arr_copy(org, n); int counts[NUM_MAX] = {0}; for (size_t i = 0; i < n; i++) { counts[org[i]]++; } for (size_t i = 1; i < NUM_MAX; i++) { counts[i] += counts[i-1]; } for (int i = n-1; i >= 0; i--) { arr[counts[org[i]]-1] = org[i]; } display(org, arr, n, "Counting"); }
void seek_hightest( int hd[], int cg, int tk, int changecard, int deck[], int recursioncount ) { int nexthd[HNUM]; int nextdeck[CNUM]; int currentpoint; int i, k; for ( i = 0; i < CNUM; i++) { if ( deck[i] == 0 ) { arr_copy( nexthd, hd, HNUM ); nexthd[changecard] = i; arr_copy( nextdeck, deck, CNUM ); nextdeck[i] = -1; currentpoint = poker_point(nexthd); if ( currentpoint > MAX_EXP ) { MAX_EXP = currentpoint; } if ( cg > 7 - LAST_RECURSION_LIMIT ) { continue; } if ( recursioncount >= LAST_RECURSION_LIMIT ) { continue; } for ( k = 0; k < HNUM; k++ ) { seek_hightest( nexthd, cg+1, tk, k, nextdeck, recursioncount+1 ); } } } }
int strategy(const int hd[], const int fd[], int cg, int tk, const int ud[], int us) { int myhd[HNUM]; int hdnum[13] = {0}; // 数位 int hdsuite[4] = {0}; // マーク arr_copy(myhd, hd, HNUM); check_myhd(myhd, hdnum, hdsuite); if ( check_pair(myhd, hdnum) ) { return -1; } else { return 1; } //if ( tk < 2 ) { return -1; } //if ( poker_point(myhd) > P2 ) { return -1; } //return 0; }
// https://www.byvoid.com/blog/sort-radix/ void Bucket(int* org, int n) { int* arr = arr_copy(org, n); int counts[NUM_MAX] = {0}; for (size_t i = 0; i < n; i++) { counts[org[i]]++; } int j = 0; int k = j; for (size_t i = 0; i < NUM_MAX; i++) { while (k < j + counts[i]) { arr[k] = i; k++; } j = k; } display(org, arr, n, "Bucket"); }
vector<int> RandomNum::pickRandomly(vector<int> &arr, const int m) { if (arr.empty() || m==0) return vector<int>(); srand(time(NULL)); vector<int> arr_copy(arr); for (int i=0; i< m; i++) { int randNum = rand() % (arr.size()-i) + i; int temp = arr_copy[randNum]; arr_copy[randNum]=arr_copy[i]; arr_copy[i]=temp; } return vector<int>(arr_copy.begin(), arr_copy.begin()+m); }
int main() { Array2d<int> arr; Array2d<int> arr_param(10, 10); for( int y = 0; y < 10; ++y ) { for( int x = 0; x < 10; ++x ) { try { arr.Set( x, y, x + y ); arr_param.Set( x, y, x + y ); } catch( std::out_of_range except ) { std::cout << except.what(); } } } Array2d<int> arr_copy( arr ); std::cout << "New 2-d array:" << std::endl; PrintArray( arr, arr.Width(), arr.Height() ); std::cout << std::endl; std::cout << "Width: " << arr.Width() << std::endl; std::cout << "Height: " << arr.Height() << std::endl; std::cout << "Size: " << arr.Size() << std::endl; std::cout << std::endl; try { arr.Shrink( 1, 0, 10, 1 ); } catch( std::out_of_range except ) { std::cout << except.what(); } std::cout << "Shrunk to its first row from the second column to the last column:" << std::endl; PrintArray( arr, arr.Width(), arr.Height() ); std::cout << std::endl; arr.Expand(2, 3, 0); std::cout << "Expanded 2 columns and 3 rows, with the new spaces filled with 0:" << std::endl; PrintArray<int>( arr, arr.Width(), arr.Height() ); std::cout << std::endl; unsigned int count_1 = arr.Count( 1 ); std::cout << "The value 1 appears in the array " << count_1 << " time"; if( count_1 != 1 ) { std::cout << "s"; } std::cout << "." << std::endl; std::cout << std::endl; std::cout << "Original array created with the parametrized constructor:" << std::endl; PrintArray<int>( arr_param, arr_param.Width(), arr_param.Height() ); std::cout << std::endl; std::cout << "Original array made with the copy constructor:" << std::endl; PrintArray<int>( arr_copy, arr_copy.Width(), arr_param.Height() ); std::cout << std::endl; std::cout << "All tests finished." << std::endl; std::cout << "Press any key to finish: "; getchar(); std::cout << std::endl; return 0; }