示例#1
0
/* {{{ fillCellInSudoku() fills cell in sudoku->matrix[a].value[b][c],
 * updates status of empty cells and filled rows etc
 * parameters must be valid, otherwise behavior is undefined
 */
void fillCellInSudoku(Sudoku * sudoku, int m, int r, int c, RCBStack * rstack, int cellType, DSStack * dsstack){
  /* setting value here so it would not get set to forbidden and put to rstack */
  sudoku->matrix[m].value[r][c] = sudokuFilled;
  sudoku->matrix[m].rowEmptyCells[r] = 0;
  sudoku->matrix[m].columnEmptyCells[c] = 0;
  sudoku->matrix[m].boxEmptyCells[c / 3 + r - r % 3] = 0;
  sudoku->emptyCells--;
  sudoku->matrix[m].rowFilled[r] = true;
  sudoku->matrix[m].columnFilled[c] = true;
  sudoku->matrix[m].boxFilled[c / 3 + r - r % 3] = true;
  if(dsstack){
    if(dsstack->stackPointer == NULL)
      dsstack->stackPointer = &(dsstack->data[0]);
    else
      dsstack->stackPointer++;
    dsstack->stackPointer->filled.m = m;
    dsstack->stackPointer->filled.r = r;
    dsstack->stackPointer->filled.c = c;
    dsstack->stackPointer->forbiddenAmount = 0;
    dsstack->stackPointer->cellType = cellType;
  }
  for(int i = 0;i < 9;i++){
    setForbidden(sudoku, m, r, i, rstack, cellType, dsstack);
    setForbidden(sudoku, m, i, c, rstack, cellType, dsstack);
  }
  for(int i = c - c % 3; i < c - c % 3 + 3; i++)
    for(int j = r - r % 3; j < r - r % 3 + 3; j++)
      setForbidden(sudoku, m, j, i, rstack, cellType, dsstack);
 for(int i = 0; i < 9; i++)
    if(i != m)
      setForbidden(sudoku, i, r, c, rstack, cellType, dsstack);
} /* }}} */
QValidator::State stringListEntryValidator::validate(QString& s, int& pos) const
{
	Q_UNUSED(pos)
	stringListEntryWidget* parentPointer = qobject_cast<stringListEntryWidget*> (parent()) ;
	if(!parentPointer || !parentPointer->active()) return Acceptable ;

	QStringList forbiddenStrings ;
	forbiddenStrings << QString() ;
	foreach(stringListEntryWidget * widget, allWidgets)
	if(widget != parentPointer &&
		widget->active())
		forbiddenStrings << widget->content() ;
	setForbidden(forbiddenStrings);
	return stringListValidator::validate(s, pos) ;
}
ClusterEditingSolutionLight InducedCostHeuristicLight::solve() {
  // execute algorithm
  if (verbosity >= 1)
    std::cout<<"Running heuristic." << "."<<std::endl;
  if (verbosity >= 2)
    std::cout<<"Size of graph: " << graph.numNodes() << " nodes."<<std::endl;
  
  int totalEdges = edgeHeap.numUnprocessed();
  for (unsigned int i = 0; i < graph.numEdges() + 1; i++) {
    Edge eIcf = edgeHeap.getMaxIcfEdge();
    Edge eIcp = edgeHeap.getMaxIcpEdge();
    if (eIcf == LightCompleteGraph::InvalidEdge || eIcp == LightCompleteGraph::InvalidEdge) {
      break;
    }
    EdgeWeight mIcf = edgeHeap.getIcf(eIcf);
    EdgeWeight mIcp = edgeHeap.getIcp(eIcp);
    if (mIcf >= mIcp) {
      // set eIcf to permanent
      setPermanent(eIcf);
      edgeHeap.removeEdge(eIcf);

      // resolve implications
      std::vector<NodeId> uClique(graph.getCliqueOf(eIcf.u));
      std::vector<NodeId> vClique(graph.getCliqueOf(eIcf.v));
      for (NodeId x : uClique) {
	for (NodeId y : vClique) {
	  if (x == y)
	    continue;
	  Edge e = Edge(x,y);
	  setPermanent(e);
	  edgeHeap.removeEdge(e);
	}
      }
    } else {
      // set eIcp fo forbidden
      setForbidden(eIcp);
      edgeHeap.removeEdge(eIcp);
      
      // resolve implications
      std::vector<NodeId> uClique(graph.getCliqueOf(eIcp.u));
      std::vector<NodeId> vClique(graph.getCliqueOf(eIcp.v));
      for (NodeId x : uClique) {
	for (NodeId y : vClique) {
	  if (x == y)
	    continue;
	  Edge e = Edge(x,y);
	  setForbidden(e);
	  edgeHeap.removeEdge(e);
	}
      }
    }
    if (verbosity >= 1 && i % 100 == 0) {
      std::cout<<"Completed "<<((totalEdges - edgeHeap.numUnprocessed())*100 / totalEdges)<<"%\r"<<std::flush;
    }
  }
  
  // calculate clustering
  if (verbosity >= 1)
    std::cout<<"Constructing result."<<std::endl;
  std::vector<std::vector<NodeId>> clusters;
  std::vector<int> clusterOfNode(graph.numNodes(), -1);
  for (NodeId u = 0; u < graph.numNodes(); u++) {
    // add cluster if not explored yet
    if (verbosity >= 1)
      std::cout<<"Completed "<<(u*100/graph.numNodes())<<"%\r"<<std::flush;
    if (clusterOfNode[u] == -1) {
      int c = clusters.size();
      clusterOfNode[u] = c;
      clusters.push_back(std::vector<NodeId>(1, u));
      for (NodeId v = u+1; v < graph.numNodes(); v++) {
	if (graph.getWeight(Edge(u, v)) == LightCompleteGraph::Permanent) {
	  if (clusterOfNode[v] != -1) {
	    std::cerr << "Conflicting clustering assignment for node " << v << std::endl;
	  }
	  clusterOfNode[v] = c;
	  clusters[c].push_back(v);
	}
      }
    }
  }
  if (verbosity >= 2) {
    int sum = 0;
    for (std::vector<NodeId>& cluster : clusters) {
      sum += cluster.size();
    }
    std::cout<<"Found " << clusters.size() << " clusters with a total of "<< sum << " nodes." << std::endl;
  }
  return ClusterEditingSolutionLight(totalCost, clusters);
}