Professor Layton and the geek
Feb. 22nd, 2008 10:49 pm![[personal profile]](https://www.dreamwidth.org/img/silk/identity/user.png)
![[livejournal.com profile]](https://www.dreamwidth.org/img/external/lj-userinfo.gif)
Well, there was another puzzle late in the game neither one of us had much luck with, so I did the same thing with that puzzle as well.
#include <algorithm> #include <stdio.h> #include <vector> #include <string> class TooManyQueens5 /* aka Puzzle 130 */ { std::vector<int> m_curArray; unsigned m_numAnswers; bool checkAnswer() const; void printAnswer() const; public: TooManyQueens5(); void solve(); }; class Puzzle99 /* aka 33333 */ { std::vector<int> m_curArray; int m_top; int m_bottom; bool checkAnswer(); void printAnswer() const; public: Puzzle99(); void solve(); }; int main (int argc, char * const argv[]) { printf("Professor Layton solver by sqlrob\n"); printf("Solving Puzzle 99 - 33333\n"); Puzzle99 diff; diff.solve(); printf("\n\n"); printf("Solving Puzzle 130 - Too Many Queens 5\n"); TooManyQueens5 queens; queens.solve(); printf("\n\n"); return 0; } class Incrementer { int m_val; public: Incrementer(int start = 0) : m_val(start) { } int operator()() { return m_val++; } }; TooManyQueens5::TooManyQueens5() : m_numAnswers(0) { m_curArray.resize(8); Incrementer inc(0); std::generate(m_curArray.begin(),m_curArray.end(),inc); } void TooManyQueens5::solve() { do { if (checkAnswer()) { ++m_numAnswers; printAnswer(); } } while(std::next_permutation(m_curArray.begin(),m_curArray.end())); } bool TooManyQueens5::checkAnswer() const { for (unsigned i = 0; i < 7; ++i) { for (unsigned j = i+1; j < 8; ++j) { if ((m_curArray[i] + (j-i) == m_curArray[j]) || (m_curArray[i] - (j-i) == m_curArray[j])) { return false; } } } return true; } void TooManyQueens5::printAnswer() const { printf("Found answer #%d:\n",m_numAnswers); for (unsigned i=0;i<8;++i) { std::string row(8,'-'); row[m_curArray[i]] = 'X'; printf("%s\n",row.c_str()); } printf("\n\n"); } Puzzle99::Puzzle99() : m_top(0),m_bottom(0) { m_curArray.resize(9); Incrementer inc(1); std::generate(m_curArray.begin(),m_curArray.end(),inc); } bool Puzzle99::checkAnswer() { m_top = m_curArray[0] + 10*m_curArray[1] + 100 * m_curArray[2] + 1000 * m_curArray[3] + 10000 * m_curArray[4]; m_bottom = m_curArray[5] + 10 * m_curArray[6] + 100 * m_curArray[7] + 1000 * m_curArray[8]; return (m_top - m_bottom) == 33333; } void Puzzle99::solve() { do { if (checkAnswer()) { printAnswer(); } } while(std::next_permutation(m_curArray.begin(),m_curArray.end())); } void Puzzle99::printAnswer() const { printf("%d-%d=33333\n",m_top,m_bottom); }
Code made pretty by C++2HTML