#include #include #include #define MAX_H 100 #define MAX_W 100 int numKind[6]; char field[MAX_H][MAX_W]; char names[6] = {'B', 'C', 'E', 'F', 'M', 'O'}; char fullNames[6][9] = {"Birch", "Chestnut", "Elm", "Fir", "Maple", "Oak"}; struct point{ int _i, _j; }; int name2num(char c) { switch(c) { case 'B': case 'b': return 0; case 'C': case 'c': return 1; case 'E': case 'e': return 2; case 'F': case 'f': return 3; case 'M': case 'm': return 4; case 'O': case 'o': return 5; } return -1; } char num2name(int n) { return(names[n]); } void readInput(int h, int w) { int i, j; char dummy[256]; int quals[6] = {-5, 6, 3, -2, 1, 2}; for (i = 0; i < 6; i++) numKind[i] = 0; for (i = 0; i < h; i++) { for (j = 0; j < w; j++) field[i][j] = getchar(); gets(dummy); } } double abs(double x) { if (x >= 0) return x; else return -x; } double computeAngle(double y1, double x1, double y2, double x2) { double ang; if (x1 == x2) if (y2 > y1) return 90.0; else return 270.0; else if (y1 == y2) if (x2 > x1) return 0.0; else return 180.0; else { ang = atan(abs((y2 - y1)/(x2 - x1))) * 180.0 / 3.141592653; if (x1 > x2) if (y1 > y2) return 180.0 + ang; else return 180.0 - ang; else if (y1 > y2) return 360.0 - ang; else return ang; } } double distance(int y1, int x1, int y2, int x2) { return sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1)); } void main(void) { int h, w, numInputs, l, i, j, i1, i2, j1, j2, k; char dummy[256]; int max_j; int numPoints; point p[MAX_H*MAX_W]; int sp[MAX_H*MAX_W]; double totLength, angle, currAngle, minAngle; int startP, currP, minAngP; scanf("%d", &numInputs); for (l = 1; l <= numInputs; l++) { scanf("%d", &h); scanf("%d", &w); gets(dummy); readInput(h, w); numPoints = 0; max_j = -1; for (i = 0; i < h; i++) for (j = 0; j < w; j++) if (field[i][j] != '-') { p[numPoints]._i = i; p[numPoints]._j = j; if (j > max_j) {max_j = j; startP = numPoints;} numPoints++; } if (numPoints <= 1) {totLength = 0.0; goto finish;} currP = startP; currAngle = 90.0; totLength = 0.0; do { minAngle = 1000.0; for (i = 0; i < numPoints; i++) if (i != currP) { angle = computeAngle(p[i]._i, p[currP]._j, p[currP]._i, p[i]._j); angle = angle - currAngle; if (angle < 0) angle += 360.0; if (angle < minAngle) { minAngle = angle; minAngP = i; } } currAngle = computeAngle(p[minAngP]._i, p[currP]._j, p[currP]._i, p[minAngP]._j); totLength += distance(p[currP]._i, p[currP]._j, p[minAngP]._i, p[minAngP]._j); currP = minAngP; } while (currP != startP); finish: totLength *= 10000.0; totLength = (int)((totLength + 5) / 10.0); totLength /= 100.0; printf("Data set %d: He will need %.2f yards of fence.\n", l, totLength); } }