void Solver::checkCell(Cell& cell) { const Trie* words = m_words; int length = cell.text.length(); for (int i = 0; i < length; ++i) { words = words->child(cell.text.at(i)); if (words == 0) { return; } } cell.checked = true; m_word += cell.text; m_positions.append(cell.position); qSwap(m_words, words); if (m_words->isWord() && (m_word.length() >= m_minimum)) { m_solutions[m_word].append(m_positions); } if (!m_words->isEmpty()) { int count = cell.neighbors.count(); for (int i = 0; i < count; ++i) { Cell& next = *cell.neighbors.at(i); if (!next.checked) { checkCell(next); } } } cell.checked = false; m_word.chop(length); m_positions.removeLast(); m_words = words; }
int checkCase(int n, char** tik) { int empty = checkEmpty(tik); int i; int ok; fprintf(output,"Case #%d: ", n+1); for (i=0; i<4; i++) { ok = checkCell(tik,i,0); if (ok == 0) { if (tik[0][0] != 'T') { fprintf(output,"%c won\n", tik[0][0]); return 0; } else { fprintf(output,"%c won\n", tik[0][1]); return 0; } } ok = checkCell(tik,0,i); if (ok == 0) { if (tik[0][0] != 'T') { fprintf(output,"%c won\n", tik[0][0]); return 0; } else { fprintf(output,"%c won\n", tik[0][1]); return 0; } } } if (empty == 0) { fprintf(output,"Game has not completed\n"); return 0; } else { fprintf(output,"Draw \n"); return 0; } }
void Solver::solve(const QStringList& letters) { m_solutions.clear(); m_positions.clear(); m_word.clear(); // Set cell contents for (int r = 0; r < m_size; ++r) { for (int c = 0; c < m_size; ++c) { m_cells[c][r].text = letters.at((r * m_size) + c).toUpper(); } } // Solve board for (int r = 0; r < m_size; ++r) { for (int c = 0; c < m_size; ++c) { checkCell(m_cells[c][r]); } } }
void Solver::checkCell(Cell& cell) { const Trie::Node* node = m_node; int length = cell.text.length(); for (int i = 0; i < length; ++i) { node = m_words->child(cell.text.at(i), node); if (node == 0) { return; } } cell.checked = true; m_word += cell.text; if (m_track_positions) { m_positions.append(cell.position); } qSwap(m_node, node); if (m_node->isWord() && (m_word.length() >= m_minimum)) { m_count++; if (m_track_positions) { m_solutions[m_word].append(m_positions); } } if (!m_node->isEmpty()) { int count = cell.neighbors.count(); for (int i = 0; i < count; ++i) { Cell& next = *cell.neighbors.at(i); if (!next.checked) { checkCell(next); } } } cell.checked = false; m_word.chop(length); if (m_track_positions) { m_positions.removeLast(); } m_node = node; }
bool GPSGridClient::GetNearestEdge( Result* result, const UnsignedCoordinate& coordinate, double radius, double headingPenalty, double heading ) { const GPSCoordinate gps = coordinate.ToProjectedCoordinate().ToGPSCoordinate(); const GPSCoordinate gpsMoved( gps.latitude, gps.longitude + 1 ); const double unsigned_per_meter = (( double ) UnsignedCoordinate( ProjectedCoordinate( gpsMoved ) ).x - coordinate.x ) / gps.ApproximateDistance( gpsMoved ); // Convert radius and headingPenalty from meters to unsigned^2. double gridRadius = unsigned_per_meter * radius; double gridRadius2 = gridRadius * gridRadius; double gridHeadingPenalty = unsigned_per_meter * headingPenalty; double gridHeadingPenalty2 = gridHeadingPenalty * gridHeadingPenalty; // Convert heading from 'degrees from North' to 'radians from x-axis' // (clockwise, [0, 0] is topleft corner, [1, 1] is bottomright corner). heading = fmod( ( heading + 270 ) * 2.0 * M_PI / 360.0, 2 * M_PI ); static const int width = 32 * 32 * 32; ProjectedCoordinate position = coordinate.ToProjectedCoordinate(); NodeID yGrid = floor( position.y * width ); NodeID xGrid = floor( position.x * width ); // Set the distance to the nearest edge initially to infinity. result->gridDistance2 = 1e20; QVector< UnsignedCoordinate > path; checkCell( result, &path, xGrid - 1, yGrid - 1, coordinate, gridRadius2, gridHeadingPenalty2, heading ); checkCell( result, &path, xGrid - 1, yGrid, coordinate, gridRadius2, gridHeadingPenalty2, heading ); checkCell( result, &path, xGrid - 1, yGrid + 1, coordinate, gridRadius2, gridHeadingPenalty2, heading ); checkCell( result, &path, xGrid, yGrid - 1, coordinate, gridRadius2, gridHeadingPenalty2, heading ); checkCell( result, &path, xGrid, yGrid, coordinate, gridRadius2, gridHeadingPenalty2, heading ); checkCell( result, &path, xGrid, yGrid + 1, coordinate, gridRadius2, gridHeadingPenalty2, heading ); checkCell( result, &path, xGrid + 1, yGrid - 1, coordinate, gridRadius2, gridHeadingPenalty2, heading ); checkCell( result, &path, xGrid + 1, yGrid, coordinate, gridRadius2, gridHeadingPenalty2, heading ); checkCell( result, &path, xGrid + 1, yGrid + 1, coordinate, gridRadius2, gridHeadingPenalty2, heading ); if ( path.empty() ) return false; double length = 0; double lengthToNearest = 0; for ( int pathID = 1; pathID < path.size(); pathID++ ) { UnsignedCoordinate sourceCoord = path[pathID - 1]; UnsignedCoordinate targetCoord = path[pathID]; double xDiff = ( double ) sourceCoord.x - targetCoord.x; double yDiff = ( double ) sourceCoord.y - targetCoord.y; double distance = sqrt( xDiff * xDiff + yDiff * yDiff ); length += distance; if ( pathID < ( int ) result->previousWayCoordinates ) lengthToNearest += distance; else if ( pathID == ( int ) result->previousWayCoordinates ) lengthToNearest += result->percentage * distance; } if ( length == 0 ) result->percentage = 0; else result->percentage = lengthToNearest / length; return true; }
bool Mesh::hit(const Ray& ray, double& tmin, ShadeRecord& sr) const { // Check the mesh. if(textureCoords.size() && textureCoords.size() != points.size()) { printf("%lu %lu\n", textureCoords.size(), points.size()); exit(1); } // the following code includes modifications from Shirley and Morley (2003) double tx_min = (bbox.x0 - ray.origin.x) / ray.direction.x; double tx_max = (bbox.x1 - ray.origin.x) / ray.direction.x; if(ray.direction.x < 0) swap(tx_min, tx_max); double ty_min = (bbox.y0 - ray.origin.y) / ray.direction.y; double ty_max = (bbox.y1 - ray.origin.y) / ray.direction.y; if(ray.direction.y < 0) swap(ty_min, ty_max); double tz_min = (bbox.z0 - ray.origin.z) / ray.direction.z; double tz_max = (bbox.z1 - ray.origin.z) / ray.direction.z; if(ray.direction.z < 0) swap(tz_min, tz_max); double t0 = max(max(tx_min, ty_min), tz_min); double t1 = min(min(tx_max, ty_max), tz_max); if (t0 > t1) return(false); const Point3D& p = bbox.contains(ray.origin) ? ray.origin : ray(t0); int ix = (int) clamp((p.x - bbox.x0) * nx / (bbox.wx), 0, nx - 1); int iy = (int) clamp((p.y - bbox.y0) * ny / (bbox.wy), 0, ny - 1); int iz = (int) clamp((p.z - bbox.z0) * nz / (bbox.wz), 0, nz - 1); // ray parameter increments per cell in the x, y, and z directions double dtx = (tx_max - tx_min) / nx; double dty = (ty_max - ty_min) / ny; double dtz = (tz_max - tz_min) / nz; int ix_step, iy_step, iz_step; int ix_stop, iy_stop, iz_stop; double tx_next = calculateNext(ray.direction.x, tx_min, ix, dtx, nx, ix_step, ix_stop); double ty_next = calculateNext(ray.direction.y, ty_min, iy, dty, ny, iy_step, iy_stop); double tz_next = calculateNext(ray.direction.z, tz_min, iz, dtz, nz, iz_step, iz_stop); double t = 0; // Traverse the grid while(true) { int idx = ix + nx * iy + nx * ny * iz; // assert(idx < numCells); Voxel* cell = voxels[idx]; if (tx_next < ty_next && tx_next < tz_next) { t = tx_next; if(checkCell(ray, cell, t, sr)) { tmin = t; return true; } tx_next += dtx; ix += ix_step; if (ix == ix_stop) return false; } else if (ty_next < tz_next) { t = ty_next; if(checkCell(ray, cell, t, sr)) { tmin = t; return true; } ty_next += dty; iy += iy_step; if (iy == iy_stop) return false; } else { t = tz_next; if(checkCell(ray, cell, t, sr)) { tmin = t; return true; } tz_next += dtz; iz += iz_step; if (iz == iz_stop) return false; } } }
bool Grid::hit(const Ray& ray, double& tmin, ShadeRecord& sr) const { // the following code includes modifications from Shirley and Morley (2003) double tx_min = (bbox.x0 - ray.origin.x) / ray.direction.x; double tx_max = (bbox.x1 - ray.origin.x) / ray.direction.x; if(ray.direction.x < 0) swap(tx_min, tx_max); double ty_min = (bbox.y0 - ray.origin.y) / ray.direction.y; double ty_max = (bbox.y1 - ray.origin.y) / ray.direction.y; if(ray.direction.y < 0) swap(ty_min, ty_max); double tz_min = (bbox.z0 - ray.origin.z) / ray.direction.z; double tz_max = (bbox.z1 - ray.origin.z) / ray.direction.z; if(ray.direction.z < 0) swap(tz_min, tz_max); double t0 = max(max(tx_min, ty_min), tz_min); double t1 = min(min(tx_max, ty_max), tz_max); if (t0 > t1) return(false); Point3D p = ray.origin; if(!bbox.contains(ray.origin)) { p = ray(t0); } int ix = (int) clamp((p.x - bbox.x0) * nx / (bbox.wx), 0, nx - 1); int iy = (int) clamp((p.y - bbox.y0) * ny / (bbox.wy), 0, ny - 1); int iz = (int) clamp((p.z - bbox.z0) * nz / (bbox.wz), 0, nz - 1); // ray parameter increments per cell in the x, y, and z directions double dtx = (tx_max - tx_min) / nx; double dty = (ty_max - ty_min) / ny; double dtz = (tz_max - tz_min) / nz; int ix_step, iy_step, iz_step; int ix_stop, iy_stop, iz_stop; double tx_next = calculateNext(ray.direction.x, tx_min, ix, dtx, nx, ix_step, ix_stop); double ty_next = calculateNext(ray.direction.y, ty_min, iy, dty, ny, iy_step, iy_stop); double tz_next = calculateNext(ray.direction.z, tz_min, iz, dtz, nz, iz_step, iz_stop); // Traverse the grid while(true) { GridVoxel* cell = voxels[ix + nx * iy + nx * ny * iz]; if (tx_next < ty_next && tx_next < tz_next) { // tmin = tx_next; if(checkCell(ray, cell, tmin, sr)) return true; tx_next += dtx; ix += ix_step; if (ix == ix_stop) return false; } else if (ty_next < tz_next) { // tmin = ty_next; if(checkCell(ray, cell, tmin, sr)) return true; ty_next += dty; iy += iy_step; if (iy == iy_stop) return false; } else { // tmin = tz_next; if(checkCell(ray, cell, tmin, sr)) return true; tz_next += dtz; iz += iz_step; if (iz == iz_stop) return false; } } }