コード例 #1
0
Scores
LexicalReorderingTableTree::
GetScore(const Phrase& f, const Phrase& e, const Phrase& c)
{
  if((!m_FactorsF.empty() && 0 == f.GetSize())
      || (!m_FactorsE.empty() && 0 == e.GetSize())) {
    //NOTE: no check for c as c might be empty, e.g. start of sentence
    //not a proper key
    // phi: commented out, since e may be empty (drop-unknown)
    //std::cerr << "Not a proper key!\n";
    return Scores();
  }

  CacheType::iterator i;

  if(m_UseCache) {
    std::pair<CacheType::iterator, bool> r;
    r = m_Cache.insert(std::make_pair(MakeCacheKey(f,e),Candidates()));
    if(!r.second) return auxFindScoreForContext((r.first)->second, c);
    i = r.first;
  } else if((i = m_Cache.find(MakeCacheKey(f,e))) != m_Cache.end())
    // although we might not be caching now, cache might be none empty!
    return auxFindScoreForContext(i->second, c);

  // not in cache => go to file...
  Candidates cands;
  m_Table->GetCandidates(MakeTableKey(f,e), &cands);
  if(cands.empty()) return Scores();
  if(m_UseCache) i->second = cands;

  if(m_FactorsC.empty()) {
    UTIL_THROW_IF2(1 != cands.size(), "Error");
    return cands[0].GetScore(0);
  } else return auxFindScoreForContext(cands, c);
};
コード例 #2
0
bool SudokuSolver::FindMin(unsigned iMin, unsigned iMax, unsigned jMin, unsigned jMax, unsigned &outI, unsigned &outJ)
{
	bool found = false;
	unsigned count = 0;

	for (unsigned i = iMin; i < iMax; i++)
		for (unsigned j = jMin; j < jMax; j++)
			if (cells[i][j] == invalid &&	
				(!found || BitCount(Candidates(i, j)) < count)) {
				count = BitCount(Candidates(i, j));
				outI = i;
				outJ = j;
				found = true;
			}

	return found;
}
コード例 #3
0
bool SudokuSolver::Solve(unsigned iMin, unsigned iMax, 
		       unsigned jMin, unsigned jMax, bool destructive, bool checkBlocks)
{
	// Check that each block can be filled if we've been asked to
	if (checkBlocks) {
		for (unsigned i = 0; i < block_size; i++)
			for (unsigned j = 0; j < block_size; j++)
				if (!Solve( i*block_size, i*block_size + block_size,
					j*block_size, j*block_size + block_size, 
					false, false))
						return false;
	}

	// Guess a good cell to brute-force with
	unsigned i;
	unsigned j;

	if (!FindMin(iMin, iMax, jMin, jMax, i, j))
		// We must have finished
		return true;

	// Iterate through the possible values this cell could have
	unsigned num = 1;
	unsigned mask = BitFor(num);

	while(mask != mask_max) {
		if (Candidates(i, j) & mask) {

			// Try this number
			Set(i, j, num);

			bool solved = (Solve(iMin, iMax, jMin, jMax, 
				destructive, checkBlocks));

			// Reverse the changes if needed
			if (!solved || !destructive)
				Unset(i, j, num);

			if (solved)
				return true;

		}
			

		// Advance to the next number
		mask *= 2;
		num++;
	}

	// None of the possibilities for cell (i,j) work
	return false;
}
コード例 #4
0
bool SudokuSolver::LoadInput(std::vector<SudokuCell> v)
{
	for(int i = 0; i < (int)v.size(); ++i) {
		if(v[i].val != 0) {
			if((Candidates(v[i].row, v[i].col) & BitFor(v[i].val)) != 0) {
				Set(v[i].row, v[i].col, v[i].val);
			}
			else {
				Init();
				return false;
			}
		}
		else {
			std::pair<int, int> p;
			p.first = v[i].col;
			p.second = v[i].row;
			unknows.push_back(p);
		}
	}
	return true;
}