/* 1. foreach element in table which is not visited: a) check in row (right/left): - mark gem as visited - check gems before this one (till other gem) - check gems next one (till other gem) - sum how many gems (right + left) are same - if sum is greater or equal 3 we have match -> save to remove b) check in column similar as too row */ void myAlgorithm() { std::vector<int> currentlyHorizontalMarked; std::vector<int> currentlyVerticalMarked; std::vector< std::pair<int,int> > toRemove; //indexes, which should be removed for (int i = 0; i < BOARDSIZE; i++) for (int j = 0; j < BOARDSIZE; j++) { if (!BoardVisited[i][j]) { currentlyHorizontalMarked = checkHorizontal(i,j); if (currentlyHorizontalMarked.size() >= 3) {//we have horizontal match. We should check vertical for every horizontal matching gem and set to remove for (int index : currentlyHorizontalMarked) toRemove.push_back(std::make_pair(i, index)); } } //check columns if (!BoardVerticalVisited[i][j]) { currentlyVerticalMarked = checkVertical(i, j); if (currentlyVerticalMarked.size() >= 3) { for (int vInd : currentlyVerticalMarked) toRemove.push_back(std::make_pair(vInd, j)); } } } showRemoved(toRemove); }
int main (int argc, char *argv[]) { srand(time(NULL)); FILE *arch=fopen("Jugadas.txt", "w"); int i=0,columna,size,length=0,jugadas=1; //i se usa para recorrer el for; columna es la variable donde se almacena char *cubo; printf("Ingrese la cantidad de Columnas \n"); scanf("%d", &size); //size es la cantidad de pilas que se deberan usar db *array[size]; for (i = 0; i < size; i += 1) array[i]=NULL; while(length<10) { columna=rancolumn(size); //pila al azar para que caiga un cubo al azar cubo=rancube(); //color al azar array[columna-1]=append(&array[columna-1], cubo); //le agrega un elemento a la pila que haya sido seleccionada al azar fprintf(arch, "Jugada %d:Cubo %s - Pila %d \n",jugadas,cubo,columna); //se imprime cada una de las jugadas al archivo de salida checkVertical(array,columna, arch); //se verifica la regla 1 con la funcion checkVertical checkHorizontal(array, size, arch, columna); //se verifica la regla 2 con la funcion checkHorizontal if(len(array[columna-1])>length) length=len(array[columna-1]); //aumenta la variable de longitud de la pila en 1 para provocar la salida del while. jugadas++; } fprintf(arch, "--JUEGO TERMINADO-- \n"); //sale del while cuando una pila tiene 10 cubos for (i = 0; i < size; i += 1) //y se hara un registro del estado final de las pilas, usando la funcion len { fprintf(arch, "Pila %d [%d]: " ,i+1, len(array[i])); writeList(array[i], arch); } return 0; }
// check that following right pointers lead to siblings // direction = 0 for left, 1 for right // if strongcheck is true, do a full horizontal check void checkHorizontal(Transaction *tx, COid coid, int direction, bool strongcheck){ int res; Ptr<Valbuf> buf, buf2; COid coid2; SuperValue *sv, *sv2; int nextattr, prevattr; res = tx->vsuperget(coid, buf, 0, 0); assert(res==0); assert(buf->type!=0); sv = buf->u.raw; checkNodeMonot(sv); if (direction == 0){ // left nextattr = DTREENODE_ATTRIB_LEFTPTR; prevattr = DTREENODE_ATTRIB_RIGHTPTR; } else { // right nextattr = DTREENODE_ATTRIB_RIGHTPTR; prevattr = DTREENODE_ATTRIB_LEFTPTR; } // if right pointer is set if (sv->Attrs[nextattr]){ // read it coid2.cid = coid.cid; coid2.oid = sv->Attrs[nextattr]; res = tx->vsuperget(coid2, buf2, 0, 0); assert(res==0); assert(buf2->type!=0); sv2 = buf2->u.raw; checkNodeMonot(sv2); // check that it points back to us assert(sv2->Attrs[prevattr] == coid.oid); // check that level is the same assert(sv2->Attrs[DTREENODE_ATTRIB_HEIGHT] == sv->Attrs[DTREENODE_ATTRIB_HEIGHT]); // check that leaf status is the same assert((sv2->Attrs[DTREENODE_ATTRIB_FLAGS] & DTREENODE_FLAG_LEAF) == (sv->Attrs[DTREENODE_ATTRIB_FLAGS] & DTREENODE_FLAG_LEAF)); // check that int status is the same assert((sv2->Attrs[DTREENODE_ATTRIB_FLAGS] & DTREENODE_FLAG_INTKEY) == (sv->Attrs[DTREENODE_ATTRIB_FLAGS] & DTREENODE_FLAG_INTKEY)); // check that largest key in first node is < smallest key in second if (sv->Ncells && sv2->Ncells){ if (direction == 0){ assert(sv->Cells[0].nKey > sv2->Cells[sv2->Ncells-1].nKey); } else { assert(sv->Cells[sv->Ncells-1].nKey < sv2->Cells[0].nKey); } } // recursively call on right pointer if (strongcheck) checkHorizontal(tx, coid2, direction, strongcheck); } }
/* * Prueft ob Zahl schon vorhanden (ruft fuer jede Bedingung eine Unterfunktion auf * Rueckgabe: 0 fuer nicht gefunden * 1 fuer gefunden */ int check(int x, int y, int wert) { if(checkHorizontal(y, wert)) return 1; if(checkVertical(x, wert)) return 1; if(checkBox(x, y, wert)) return 1; return 0; }
int main(){ int puzzles; scanf("%d", &puzzles); int runs; for (runs = 0; runs < puzzles; ++runs) { int correctSolution = 1; // If this value is never changed, then the solution is correct. int field[SIZE][SIZE]; int i,j,k,l; for (i = 0; i < SIZE; ++i) { for (j = 0; j < SIZE; ++j) { scanf("%1d",&field[i][j]); // Reads in data } } i = 0; while(i <= 6 && correctSolution == 1){ j = i + 3; k = 0; while(k <= 6 && correctSolution == 1){ l = k + 3; correctSolution = checkBox(field,i,j,k,l); k += 3; } i += 3; } if(correctSolution == 1){ // If the solution is still correct to this point, all horizontal lines are checked. correctSolution = checkHorizontal(field); } if(correctSolution == 1){ // If the solution is still correct to this point, all vertical lines are checked. correctSolution = checkVertical(field); } if(correctSolution == 1){ printf("YES\n"); } else{ printf("NO\n"); } } return 0; }
/* * Begin game of tic-tac-toe */ void playGame(int **board, int board_size) { int winner = 0; int current_player = PLAYER1; position_t input; // if no winner, and board is not full yet, proceed playing while (winner == 0 && !isBoardFull(board, board_size)) { int move = 0; // scan the player input, if it is not valid, prompt player and scan again while (!move) { printBoard(board, board_size); input = takePlayerInput(board, current_player, board_size); // input is valid, place input on the board, and escape the scan loop if (checkPlayerInput(board, current_player, input, board_size) == 1) { move = 1; board[input.row][input.col] = current_player; } } // check if input is winning move, escape play loop if it is, or else switch player. if (checkHorizontal(board, current_player, input.row, board_size) == 1 || checkVertical(board, current_player, input.col, board_size) == 1 || checkDiagonal(board, current_player, input.row, input.col, board_size) == 1) { printf("~~~~Player %d wins!~~~~\n", current_player); winner = 1; } else { // switch player if (current_player == PLAYER1) current_player = PLAYER2; else current_player = PLAYER1; } } /* print the final status of the board */ printBoard(board, board_size); }
/* * Esse é o início do programa. */ int main(){ /* * A variável n é inicializada, ela armazenará o tamanho da matriz * e servirá como parâmetro para inicializá-la. */ int n; /* * Uma mensagem de boas-vindas é exibida ao usuário, a medida do * quadrado é pedida a ele e é armazenada na variável n. */ printf("Bem vindo ao programa do quadrado latino!\n"); printf("Digite a medida do quadrado: "); scanf("%i", &n); /* * A matriz é inicializada com n linhas e n colunas e, logo após, a função * popularMatriz é chamada e valores são atribuídos. */ int matriz[n][n]; popularMatriz(n, matriz); /* * A seguir, uma estrutura condicional usa as funções checkHorizontal * e checkVertical para checar se a matriz é ou não um quadrado latino. * As funções retornam valores booleanos de 1 (True) ou 0 (False), e a * matriz só é considerada um quadrado latino se ambas as condições forem * verdadeiras. * Se ambas forem verdadeiras, o programa diz que a matriz é um quadrado * latino, mas se ao menos uma das condições for falsa, o programa dirá * que a matriz não é um quadrado latino. */ if (checkHorizontal(n, matriz) && checkVertical(n, matriz)) printf("A matriz digitada eh um quadrado latino."); else printf("A matriz digitada nao eh um quadrado latino."); /* * Fim do programa */ return 0; }
/*checkWinner *Description: Works through each scanning function to find a winner */ int checkWinner(int grid[column_size][row_size]) { //prorotypes for checkWinner int checkHorizontal(int grid[column_size][row_size]), checkVertical(int grid[column_size][row_size]), forwardDiagonal(int grid[column_size][row_size]), backwardDiagonal(int grid[column_size][row_size]), checkDraw(int grid[column_size][row_size]); int winner; //intialised as default value. //works through each test until winner is not 0 //this saves doing the deeper tests if one of the higher tests is successful at finding a winner. winner = checkHorizontal(grid); if(winner == 0) winner = checkVertical(grid); if(winner == 0) winner = forwardDiagonal(grid); if(winner == 0) winner = backwardDiagonal(grid); if(winner == 0) winner = checkDraw(grid); //will return a 3 if a draw is found. return(winner); }
// if allkeys is set, stores all found keys there // if strongcheck is true, do full horizontal traversals for every node (slow) void checkCoid(COid startcoid, Set<I64> *allkeys, bool strongcheck){ Transaction *tx; tx = new Transaction(SC); LinkList<COidQueueElement> coidqueue; Set<COid> pastcoids; COidQueueElement *el, *elchild; COid coid; COid child; int res; Ptr<Valbuf> buf; i64 fencemin, fencemax; SuperValue *sv; el = new COidQueueElement(startcoid); el->fencemin = LLONG_MIN; el->fencemax = LLONG_MAX; coidqueue.pushTail(el); while (!coidqueue.empty()){ el = coidqueue.popHead(); coid = el->coid; fencemin = el->fencemin; fencemax = el->fencemax; delete el; if (pastcoids.belongs(coid)){ printf("COid %016llx:%016llx referenced more than once\n", (long long)coid.cid, (long long)coid.oid); assert(0); } else pastcoids.insert(coid); // read coid res = tx->vsuperget(coid, buf, 0, 0); assert(res==0); if (buf->type==0){ printf("COid %llx:%llx not a supervalue\n", (long long)coid.cid, (long long)coid.oid); assert(0); continue; } sv = buf->u.raw; checkNodeFence(coid, sv, fencemin, fencemax); checkNodeMonot(sv); checkHorizontal(tx, coid, 0, strongcheck); checkHorizontal(tx, coid, 1, strongcheck); if (sv->Attrs[DTREENODE_ATTRIB_FLAGS] & DTREENODE_FLAG_LEAF){ // this is a leaf node checkLeaf(coid, sv, allkeys); } else { // this is an inner node checkInner(coid, sv); } if (!(sv->Attrs[DTREENODE_ATTRIB_FLAGS] & DTREENODE_FLAG_LEAF)){ // add children to queue child.cid = coid.cid; for (int i=0; i < sv->Ncells; ++i){ child.oid = sv->Cells[i].value; elchild = new COidQueueElement(child); if (sv->CellType == 0){ // intkey if (i==0) elchild->fencemin = fencemin; else elchild->fencemin = sv->Cells[i-1].nKey; elchild->fencemax = sv->Cells[i].nKey; } else { // checking functionality is disabled for non-intkey trees, so just set fences to 0 elchild->fencemin = elchild->fencemax = 0; } coidqueue.pushTail(elchild); } // now add the last pointer child.oid = sv->Attrs[DTREENODE_ATTRIB_LASTPTR]; elchild = new COidQueueElement(child); if (sv->CellType == 0){ // intkey if (sv->Ncells >= 1) elchild->fencemin = sv->Cells[sv->Ncells-1].nKey; else elchild->fencemin = fencemin; elchild->fencemax = fencemax; } else { elchild->fencemin = elchild->fencemax = 0; } coidqueue.pushTail(elchild); } } delete tx; }