Exemplo n.º 1
0
std::vector<int> MazeSolver::SolveMaze(std::vector<std::vector<int> > walls) {
	/*
		A brute force solver. Tries every path until it finds one that works.
		It is biased to attempt paths that are in the direction of the end cell (opposite corner to the start).
		About 130% faster than the example.

		1. Begin with the final cell and an empty maze point stack
		2. Set the position and walls of the current maze cell in a new maze point
		3. Add the last cell (top of path stack) as an explored path, if any
		3. Pick one free path from the current cell
			a. If no free path is available, pop the stack elements until one has a free path
			b. Pop the free path cell and set the current cell as that one
			c. Get the next free path from the current cell
		4. Set the free path as explored in the current cell
		5. Push the current cell to the stack
		6. Set the current cell to the next one, taken from the free path
		7. Repeat until the current cell is the start
		8. Push the last position to the stack to complete the path
		9. Dump path into final path collection, from stack top to bottom
	*/

	int mazeSize = std::sqrt(walls.size());
	point pos(mazeSize - 1, mazeSize - 1);
	std::stack<maze_point> path;

	while (pos.x != 0 || pos.y != 0) {
		maze_point currentCell(pos);
		currentCell.setWalls(walls[currentCell.position.getIndex(mazeSize)]);
		if (!path.empty()) {
			currentCell.setExplored(currentCell.position.getDirection(path.top().position));
		}
		int direction = currentCell.getFirstAvailablePath();
		if (direction == -1) {
			while (!path.top().hasOneFreePath()) {
				path.pop();
			}
			currentCell = path.top();
			path.pop();
			direction = currentCell.getFirstAvailablePath();
		}
		currentCell.setExplored(direction);
		path.push(currentCell);
		pos = currentCell.position.getRelative(direction);
	}
	path.push(maze_point(pos));

	return toIndexPath(path, mazeSize);
}
Exemplo n.º 2
0
  /*! \brief Replaces current cell with a new cell.
  *
  * \todo create a cellcopy operation.
  * \todo test!
  *
  */
  void CellCursor::replaceCurrentWith(Cell *newCell)
  {
    //       newCell->setParent(currentCell()->parentCell());
    //       newCell->setChild(currentCell()->child());
    //       newCell->setLast(currentCell()->last());
    //       newCell->setPrevious(currentCell()->previous());
    //       newCell->setNext(currentCell()->next());
    qDebug("replaceWithCurrent");

    newCell->setText(currentCell()->text());

    //Replace cell.
    deleteCurrentCell();
    addBefore(newCell);
    qDebug("End replaceWithCurrent");
  }
Exemplo n.º 3
0
std::vector<int> MazeSolver::SolveMaze3(std::vector<std::vector<int> > walls) {
	/*
		Hybrid of the first and second solver, only one level of dead ends are filled
	*/

	int mazeSize = std::sqrt(walls.size());
	int mazeStart = 0;
	int mazeEnd = mazeSize * mazeSize - 1;
	std::vector<int> deadEnds;

	for (int i = mazeStart + 1; i < mazeEnd - 1; i++) {
		fillDeadEnd(&walls, mazeSize, mazeStart, mazeEnd, i, 0, 1);
	}

	point pos(mazeSize - 1, mazeSize - 1);
	std::stack<maze_point> path;

	while (pos.x != 0 || pos.y != 0) {
		maze_point currentCell(pos);
		currentCell.setWalls(walls[currentCell.position.getIndex(mazeSize)]);
		if (!path.empty()) {
			currentCell.setExplored(currentCell.position.getDirection(path.top().position));
		}
		int direction = currentCell.getFirstAvailablePath();
		if (direction == -1) {
			while (!path.top().hasOneFreePath()) {
				path.pop();
			}
			currentCell = path.top();
			path.pop();
			direction = currentCell.getFirstAvailablePath();
		}
		currentCell.setExplored(direction);
		path.push(currentCell);
		pos = currentCell.position.getRelative(direction);
	}
	path.push(maze_point(pos));

	return toIndexPath(path, mazeSize);
}
Exemplo n.º 4
0
int QtTableSelectionManager::qt_metacall(QMetaObject::Call _c, int _id, void **_a)
{
    _id = QObject::qt_metacall(_c, _id, _a);
    if (_id < 0)
        return _id;
    if (_c == QMetaObject::InvokeMetaMethod) {
        switch (_id) {
        case 0:
            currentChanged((*reinterpret_cast< int(*)>(_a[1])),(*reinterpret_cast< int(*)>(_a[2])),(*reinterpret_cast< int(*)>(_a[3])),(*reinterpret_cast< int(*)>(_a[4])));
            break;
        case 1:
            anchorChanged((*reinterpret_cast< int(*)>(_a[1])),(*reinterpret_cast< int(*)>(_a[2])),(*reinterpret_cast< int(*)>(_a[3])),(*reinterpret_cast< int(*)>(_a[4])));
            break;
        case 2:
            selectionsChanged((*reinterpret_cast< const QList<QtTableSelectionRange>(*)>(_a[1])));
            break;
        case 3:
            setCurrentCell((*reinterpret_cast< int(*)>(_a[1])),(*reinterpret_cast< int(*)>(_a[2])));
            break;
        case 4:
            setAnchorCell((*reinterpret_cast< int(*)>(_a[1])),(*reinterpret_cast< int(*)>(_a[2])));
            break;
        case 5:
            setSelected((*reinterpret_cast< const QtTableSelectionRange(*)>(_a[1])),(*reinterpret_cast< SelectionMode(*)>(_a[2])));
            break;
        case 6:
            setSelected((*reinterpret_cast< const QtTableSelectionRange(*)>(_a[1])));
            break;
        case 7:
            clearSelections();
            break;
        case 8:
            d_func()->_q_modelDestroyed();
            break;
        case 9:
            d_func()->_q_rowsInserted((*reinterpret_cast< int(*)>(_a[1])),(*reinterpret_cast< int(*)>(_a[2])));
            break;
        case 10:
            d_func()->_q_rowsRemoved((*reinterpret_cast< int(*)>(_a[1])),(*reinterpret_cast< int(*)>(_a[2])));
            break;
        case 11:
            d_func()->_q_columnsInserted((*reinterpret_cast< int(*)>(_a[1])),(*reinterpret_cast< int(*)>(_a[2])));
            break;
        case 12:
            d_func()->_q_columnsRemoved((*reinterpret_cast< int(*)>(_a[1])),(*reinterpret_cast< int(*)>(_a[2])));
            break;
        default:
            ;
        }
        _id -= 13;
    }
#ifndef QT_NO_PROPERTIES
    else if (_c == QMetaObject::ReadProperty) {
        void *_v = _a[0];
        switch (_id) {
        case 0:
            *reinterpret_cast< QtCell*>(_v) = currentCell();
            break;
        case 1:
            *reinterpret_cast< QtCell*>(_v) = anchorCell();
            break;
        case 2:
            *reinterpret_cast< bool*>(_v) = isAnchorSelectionActive();
            break;
        case 3:
            *reinterpret_cast< SelectionMode*>(_v) = selectionMode();
            break;
        }
        _id -= 4;
    } else if (_c == QMetaObject::WriteProperty) {
        void *_v = _a[0];
        switch (_id) {
        case 0:
            setCurrentCell(*reinterpret_cast< QtCell*>(_v));
            break;
        case 1:
            setAnchorCell(*reinterpret_cast< QtCell*>(_v));
            break;
        }
        _id -= 4;
    } else if (_c == QMetaObject::ResetProperty) {
        _id -= 4;
    } else if (_c == QMetaObject::QueryPropertyDesignable) {
        _id -= 4;
    } else if (_c == QMetaObject::QueryPropertyScriptable) {
        _id -= 4;
    } else if (_c == QMetaObject::QueryPropertyStored) {
        _id -= 4;
    } else if (_c == QMetaObject::QueryPropertyEditable) {
        _id -= 4;
    } else if (_c == QMetaObject::QueryPropertyUser) {
        _id -= 4;
    }
#endif // QT_NO_PROPERTIES
    return _id;
}
Exemplo n.º 5
0
  void
  GCells::addCells(double maxdiam)
  {
    list.clear();
    partCellData.clear();
    NCells = 1;

    for (size_t iDim = 0; iDim < NDIM; iDim++)
      {
	cellCount[iDim] = int(Sim->primaryCellSize[iDim] 
			      / (maxdiam * (1.0 + 10 * std::numeric_limits<double>::epsilon())));
      
	if (cellCount[iDim] < 2 * overlink + 1)
	  cellCount[iDim] = 2 * overlink + 1;
	
	NCells *= cellCount[iDim];
      
	dilatedCellMax[iDim] = cellCount[iDim] - 1;
	cellLatticeWidth[iDim] = Sim->primaryCellSize[iDim] / cellCount[iDim];
	cellDimension[iDim] = cellLatticeWidth[iDim] + (cellLatticeWidth[iDim] - maxdiam) 
	  * lambda;
	cellOffset[iDim] = -(cellLatticeWidth[iDim] - maxdiam) * lambda * 0.5;
      }

    if (getMaxSupportedInteractionLength() < maxdiam)
      M_throw() << "The system size is too small to support the range of interactions specified (i.e. the system is smaller than the interaction diameter of one particle).";

    //Find the required size of the morton array
    magnet::math::MortonNumber<3> coords(cellCount[0], cellCount[1], cellCount[2]);
    size_t sizeReq = coords.getMortonNum();

    list.resize(sizeReq); //Empty Cells created!

    dout << "Cells <x,y,z> " << cellCount[0] << ","
	 << cellCount[1] << "," << cellCount[2]
	 << "\nCell Offset "
	 << cellOffset[0] / Sim->units.unitLength() << ","
	 << cellOffset[1] / Sim->units.unitLength() << ","
	 << cellOffset[2] / Sim->units.unitLength()
	 << "\nCells Dimension " 
	 << cellDimension[0] / Sim->units.unitLength()
	 << ","
	 << cellDimension[1] / Sim->units.unitLength()
	 << "," 
	 << cellDimension[2] / Sim->units.unitLength()
	 << "\nLattice spacing " 
	 << cellLatticeWidth[0] / Sim->units.unitLength()
	 << ","
	 << cellLatticeWidth[1] / Sim->units.unitLength()
	 << "," 
	 << cellLatticeWidth[2] / Sim->units.unitLength()
	 << "\nRequested supported length " << maxdiam / Sim->units.unitLength()
	 << "\nSupported length           " << getMaxSupportedInteractionLength() / Sim->units.unitLength()
	 << "\nVector Size <N>  " << sizeReq << std::endl;
  
    //Add the particles section
    //Required so particles find the right owning cell
    Sim->dynamics->updateAllParticles();
  
    ////Add all the particles 
    BOOST_FOREACH(const size_t& id, *range)
      {
	Particle& p = Sim->particles[id];
	Sim->dynamics->updateParticle(p); 
	addToCell(id);
	if (verbose)
	  {
	    boost::unordered_map<size_t, size_t>::iterator it = partCellData.find(id);
	    magnet::math::MortonNumber<3> currentCell(it->second);
	    
	    magnet::math::MortonNumber<3> estCell(getCellID(Sim->particles[ID].getPosition()));
	  
	    Vector wrapped_pos = p.getPosition();
	    for (size_t n = 0; n < NDIM; ++n)
	      {
		wrapped_pos[n] -= Sim->primaryCellSize[n] *
		  lrint(wrapped_pos[n] / Sim->primaryCellSize[n]);
	      }
	    Vector origin_pos = wrapped_pos + 0.5 * Sim->primaryCellSize - cellOffset;

	    derr << "Added particle ID=" << p.getID() << " to cell <"
		 << currentCell[0].getRealValue() 
		 << "," << currentCell[1].getRealValue()
		 << "," << currentCell[2].getRealValue()
		 << ">"
		 << "\nParticle is at this distance " << Vector(p.getPosition() - calcPosition(it->second, p)).toString() << " from the cell origin"
		 << "\nParticle position  " << p.getPosition().toString()	
		 << "\nParticle wrapped distance  " << wrapped_pos.toString()	
		 << "\nParticle relative position  " << origin_pos.toString()
		 << "\nParticle cell number  " << Vector(origin_pos[0] / cellLatticeWidth[0],
							 origin_pos[1] / cellLatticeWidth[1],
							 origin_pos[2] / cellLatticeWidth[2]
							 ).toString()
		 << std::endl;
	  }
      }

    dout << "Cell loading " << float(partCellData.size()) / NCells 
	 << std::endl;
  }