#include #include #include using namespace std; int w, h; string f[20]; struct Node { int x, y; }; vector nodes; int dm[10][10]; // distance matrix bool done[10]; int ans; bool create_distance_matrix() { int cf[21][21]; // copied field for (int i=0; i 0 && cf[y][x-1] == -1) { cf[y][x-1] = current + 1; } if (x < w-1 && cf[y][x+1] == -1) { cf[y][x+1] = current + 1; } if (y > 0 && cf[y-1][x] == -1) { cf[y-1][x] = current + 1; } if (y < h-1 && cf[y+1][x] == -1) { cf[y+1][x] = current + 1; } changed = true; } } } } for (int j=0; j= ans) { return; } if (n == nodes.size()) { if (sum < ans) { ans = sum; } return; } for (int i=0; i> w >> h, (w || h)) { for (int y=0; y> f[y]; } // initialization nodes.clear(); int start = -1; for (int y=0; y