User:Jeff/The Great Wall Game solution

using namespace std; // A big number used to represent infinity. // A pair of integers, representing a cartesian point. typedef pair p2i; // Function headers. int solve(int, vector &); int bestMatch(int, vector &, vector &); int bestMatch(vector &, int, vector &, vector &, int, int); int dist(p2i&, p2i&); int main { // For each board in the input... for (int i(1); ; ++i) { int n;    // Read in the number of stones cin >> n;    if (n == 0) return 0; // Create and populate the vector of stone positions vector stones(n); for (int j(0); j < n; ++j) cin >> stones[j].X >> stones[j].Y;    // Find the answer for this board configuration int ans(solve(n, stones)); if (i > 1) cout << endl; // Print the answer cout << "Board "<<i<<": "<<ans<<" moves required." << endl; } } // This function gives the best answer for a starting configuration int solve(int n, vector &stones) { // "ans" is the answer (shortest number of moves) which starts at infinity int ans(INF); // "wall" is the final positions of the stones. We try every possible final position (there are 2n+2 of them) vector wall(n); for (int i(1); i <= n; ++i) { for (int j(0); j < n; ++j) wall[j] = p2i(i, j+1); // "wall" is now a column of pegs at X=i; we update "ans" if it's better ans <?= bestMatch(n, stones, wall); for (int j(0); j < n; ++j) wall[j] = p2i(j+1, i); // "wall" is now a row of pegs at Y=i; we update "ans" if it's better ans <?= bestMatch(n, stones, wall); }  for (int i(0); i < n; ++i) wall[i] = p2i(i+1, i+1); // "wall" is now a diagonal from upper left to lower right; we update "ans" if it's better ans <?= bestMatch(n, stones, wall); for (int i(0); i < n; ++i) wall[i] = p2i(i+1, n-i); // "wall" is now a diagonal from upper right to lower left; we update "ans" if it's better ans <?= bestMatch(n, stones, wall); return ans; } // This function gives the best answer for a starting and ending configuration int bestMatch(int n, vector &stones, vector &wall) { // We memoize on the bitmask of stones which have been moved from their starting to their ending positions vector mem((1<<n), -1); return bestMatch(mem, n, stones, wall, 0, 0); } // Helper function using memoization int bestMatch(vector &mem, int n, vector &stones,    vector &wall, int x, int wallMask) { // If we've already computed an answer for this bitmask, then return it  if (mem[wallMask] != -1) return mem[wallMask]; // If we've placed all the tiles, there is no more work left to do, so return 0 if (x == n) return mem[wallMask] = 0; // The answer, initialized to infinity int ans(INF); for (int i(0); i < n; ++i) { // We try each possible ending position for stone x to move to (unless we've already moved a stone there) if (wallMask & (1<?(b.X-a.X)) + ((a.Y-b.Y)>?(b.Y-a.Y)); }
 * 1) include
 * 2) include
 * 1) define X first
 * 2) define Y second
 * 1) define INF (1<<30)