bool dfs(int x, int y) { if (x >= 9) return true; char ch = bo[x][y]; int xx = x, yy = y; move(xx, yy); if (ch != '.') { bool ret = dfs(xx, yy); return ret; } int bid = calcBlockId(x, y); for (char i = '1'; i <= '9'; i ++) { if (row[x].find(i) != row[x].end() || col[y].find(i) != col[y].end() || block[bid].find(i) != block[bid].end()) { continue; } row[x].insert(i); col[y].insert(i); block[bid].insert(i); bool ret = dfs(xx, yy); if (ret) { bo[x][y] = i; return true; } row[x].erase(i); col[y].erase(i); block[bid].erase(i); } return false; }
bool cSectorMap::removeItem( cUObject* object ) { unsigned short x = object->pos().x; unsigned short y = object->pos().y; if ( x >= gridWidth_ * SECTOR_SIZE || y >= gridHeight_ * SECTOR_SIZE ) { error_ = "X or Y is out of the grid boundaries."; return false; } unsigned int block = calcBlockId( x, y ); // Seems like the item is already gone if ( !grid[block] ) return true; // Remove the Item from the array (most complicated part of the code) for ( unsigned int i = 0; i < grid[block]->count; ++i ) { if ( grid[block]->data[i] == object ) { // We found our object. Create a new array and copy the rest over cUObject** newData = ( cUObject** ) malloc( ( grid[block]->count - 1 ) * sizeof( cUObject* ) ); unsigned int offset = 0; unsigned int j; // MemCpy is nice 'n all but we're too stupid to use it // Copy the old elements over for ( j = 0; j < grid[block]->count; ++j ) { if ( j == i ) continue; newData[offset++] = grid[block]->data[j]; } /*memcpy( newData, grid[block]->data, sizeof( cUObject* ) * i ); memcpy( newData + ( i * sizeof( cUObject* ) ), grid[block]->data + sizeof( cUObject* ) * ( i + 1 ), QMAX( 0, sizeof( cUObject* ) * ( grid[block]->count - ( i + 1 ) ) ) );*/ free( grid[block]->data ); grid[block]->data = newData; grid[block]->count--; break; } } // Check if the block can be freed if ( !grid[block]->count ) { free( grid[block]->data ); delete grid[block]; grid[block] = 0; // This makes *sure* it gets resetted to 0 } return true; }
void solveSudoku(vector<vector<char> > &board) { if (board.size() != 9 || board[0].size() != 9) return; bo = board; for (int i = 0; i < 9; i ++) { for (int j = 0; j < 9; j ++) { char ch = bo[i][j]; if (ch == '.') continue; int bid = calcBlockId(i, j); row[i].insert(ch); col[j].insert(ch); block[bid].insert(ch); } } dfs(0, 0); board = bo; }
bool cSectorMap::addItem( cUObject* object ) { unsigned short x = object->pos().x; unsigned short y = object->pos().y; if ( x >= gridWidth_ * SECTOR_SIZE || y >= gridHeight_ * SECTOR_SIZE ) { error_ = "X or Y is out of the grid boundaries."; return false; } // Calculate the block id unsigned int block = calcBlockId( x, y ); if ( !grid[block] ) { grid[block] = new stSector; grid[block]->count = 1; grid[block]->data = ( cUObject * * ) malloc( sizeof( cUObject * ) ); grid[block]->data[0] = object; } else { // Check if the item is already inside of our array unsigned int i; for ( i = 0; i < grid[block]->count; ++i ) if ( grid[block]->data[i] == object ) return true; // Append our item to the array grid[block]->count++; grid[block]->data = ( cUObject * * ) realloc( grid[block]->data, grid[block]->count * sizeof( cUObject * ) ); grid[block]->data[grid[block]->count - 1] = object; } return true; }