const unsigned short Board::getNeighbors(unsigned short row, unsigned short column) const { unsigned short count = 0; for(short rowPos = validRow(row-1); rowPos <= validRow(row+1); rowPos++){ for(short columnPos = validColumn(column-1); columnPos <= validColumn(column+1); columnPos++){ count += isAlive(rowPos, columnPos) && !(rowPos == row && columnPos == column)? 1 : 0; } } return count; };
bool PoitemTableModel::insertRowIntoTable(const QSqlRecord& record) { if (record.isEmpty()) return true; bool isNull = true; for (int i = 0; i < record.count(); i++) { if (i == record.indexOf("poitem_pohead_id") || (record.value(i).toString().isEmpty() && (i == record.indexOf("poitem_status") || i == record.indexOf("poitem_vend_item_descrip") || i == record.indexOf("poitem_vend_uom") || i == record.indexOf("poitem_vend_item_number") || i == record.indexOf("poitem_comments") ))) continue; isNull &= record.isNull(i); } if (isNull) return true; QSqlRecord newRecord(record); if (! validRow(newRecord)) return false; return QSqlRelationalTableModel::insertRowIntoTable(newRecord); }
double PageItem_Table::rowPosition(int row) const { if (!validRow(row)) return 0.0; return m_rowPositions.at(row); }
double PageItem_Table::rowHeight(int row) const { if (!validRow(row)) return 0.0; return m_rowHeights.at(row); }
static u64 slowFlipsDir(int r, int c, int dr, int dc, u64 mover, u64 enemy) { u64 flips = 0; r+=dr; c+=dc; while (validRow(r) && validRow(c)) { const int sq = square(r, c); if (bitSet(sq, enemy)) { flips |= 1ULL<<sq; } else { return (bitSet(sq, mover)) ? flips : 0; } r+=dr; c+=dc; } return 0; }
void DenseMatrix<T>::rowInterchange(size_t row1, size_t row2) { if(!validRow(row1)) { throw typename Matrix<T>::RowRangeError(row1); } if(!validRow(row2)) { throw typename Matrix<T>::RowRangeError(row2); } for(size_t j = 0; j < numCols; ++j) { std::swap(a(row1, j), a(row2, j)); } }
void DenseMatrix<T>::rowReplace(size_t rowToReplace, size_t otherRow, const T& multiple) { if(!validRow(rowToReplace)) { throw typename Matrix<T>::RowRangeError(rowToReplace); } if(!validRow(otherRow)) { throw typename Matrix<T>::RowRangeError(otherRow); } for(size_t j = 0; j < numCols; ++j) { a(rowToReplace, j) += a(otherRow, j) * multiple; } }
void PageItem_Table::removeRows(int index, int numRows) { ASSERT_VALID(); if (!validRow(index) || numRows < 1 || numRows >= rows() || index + numRows > rows()) return; // Remove row heights, row positions and rows of cells. double removedHeight = 0.0; for (int i = 0; i < numRows; ++i) { // Remove row height and position. removedHeight += m_rowHeights.takeAt(index); m_rowPositions.removeAt(index); // Invalidate removed cells. foreach (TableCell removedCell, m_cellRows[index]) removedCell.setValid(false); // Remove row of cells. m_cellRows.removeAt(index); } // Adjust following rows. for (int nextRow = index; nextRow < rows() - numRows; ++nextRow) { // Adjust position of following row. m_rowPositions[nextRow] -= removedHeight; // "Move" cells in following row up. foreach (TableCell cell, m_cellRows[nextRow]) cell.moveUp(numRows); } // Update row spans. updateSpans(index, numRows, RowsRemoved); // Decrease number of rows. m_rows -= numRows; // Update cells. TODO: Not for entire table. updateCells(); // Remove any invalid cells from selection. QMutableSetIterator<TableCell> cellIt(m_selection); while (cellIt.hasNext()) if (!cellIt.next().isValid()) cellIt.remove(); // Move to cell below. moveTo(cellAt(qMin(index + 1, rows() - 1), m_activeColumn)); emit changed(); ASSERT_VALID(); }
bool PoitemTableModel::updateRowInTable(int row, const QSqlRecord& record) { // touch everything so we can distinguish unchanged fields from NULL/0 as new val for (int i = 0; i < columnCount(); i++) setData(index(row, i), data(index(row, i))); QSqlRecord newRecord(record); if (! validRow(newRecord)) return false; return QSqlRelationalTableModel::updateRowInTable(row, newRecord); }
void DenseMatrix<T>::rowScale(size_t row, const T& constant) { if(!validRow(row)) { throw typename Matrix<T>::RowRangeError(row); } for(size_t j = 0; j < numCols; ++j) { a(row, j) *= constant; } }
const T& DenseMatrix<T>::getElement(size_t row, size_t col) const { if(!validRow(row)) { throw typename Matrix<T>::RowRangeError(row); } if(!validColumn(col)) { throw typename Matrix<T>::ColumnRangeError(col); } return a(row, col); }
MyVector<T> DenseMatrix<T>::getRowAsVector(size_t row) const { if(!validRow(row)) { throw typename Matrix<T>::RowRangeError(row); } MyVector<T> vec(numCols); for(size_t j = 0; j < numCols; ++j) { vec[j] = a(row, j); } return vec; }
void PageItem_Table::resizeRow(int row, double height, ResizeStrategy strategy) { ASSERT_VALID(); if (!validRow(row)) return; if (strategy == MoveFollowing) resizeRowMoveFollowing(row, height); else if (strategy == ResizeFollowing) resizeRowResizeFollowing(row, height); else qWarning("Unknown resize strategy!"); // Update cells. TODO: Not for entire table. updateCells(); emit changed(); ASSERT_VALID(); }
void solveSudoku(vector<vector<char>>& board) { vector<pair<int, int>> v; int n = 9; for(int i = 0; i < n; ++i) { for(int j = 0; j < n; ++j) { if(board[i][j] == '.') v.emplace_back(make_pair(i, j)); } } function<bool(int)> validSquare = [&](int i) { vector<bool> visited(n + 1, false); int x = (i / 3) * 3; int y = (i % 3) * 3; for(int i = x; i < x + 3; ++i) { for(int j = y; j < y + 3; ++j) { if(board[i][j] != '.') { if(visited[board[i][j] - '0']) return false; visited[board[i][j] - '0'] = true; } } } return true; }; function<bool(int)> validCol = [&](int i) { vector<bool> visited(n + 1, false); for(int j = 0; j < n; ++j) { if(board[j][i] != '.') { if(visited[board[j][i] - '0']) return false; visited[board[j][i] - '0'] = true; } } return true; }; function<bool(int)> validRow = [&](int i) { vector<bool> visited(n + 1, false); for(int j = 0; j < n; ++j) { if(board[i][j] != '.') { if(visited[board[i][j] - '0']) return false; visited[board[i][j] - '0'] = true; } } return true; }; function<bool()> validBoard = [&]() { for(int i = 0; i < n; ++i) if(!validRow(i)) return false; for(int i = 0; i < n; ++i) if(!validCol(i)) return false; for(int i = 0; i < n; ++i) if(!validSquare(i)) return false; return true; }; function<bool(int)> dfs = [&](int i) { for(const auto& a : board) { for(const auto& b : a) { cout<<b<<" "; } cout<<endl; } cout<<"---------"<<endl; if(!validBoard()) return false; if(i == v.size()) return true; for(int j = 1; j < n + 1; ++j) { board[v[i].first][v[i].second] = '0' + j; if(dfs(i + 1)) return true; board[v[i].first][v[i].second] = '.'; } return false; }; dfs(0); }