#include #include #include #include using namespace std; int distanceSquared (int x1, int y1, int x2, int y2) { return (x1-x2)*(x1-x2) + (y1-y2)*(y1-y2); } class Island { public: Island (int width, int height) : wide(width), high(height), zero(0) {taxes = new int[high*wide];} ~Island() {delete [] taxes;} int& operator() (int x, int y); int getWidth() {return wide;} int getHeight() {return high;} int taxCollection (int myX, int myY, int hisX, int hisY); private: int wide; int high; int* taxes; int zero; }; int& Island::operator() (int x, int y) { if (x >= 0 && x < wide && y >= 0 && y < high) return taxes[x + wide*y]; else return zero; } int Island::taxCollection (int myX, int myY, int hisX, int hisY) { int sum = 0; for (int x = myX-6; x <= myX+6; ++x) for (int y = myY-6; y <= myY+6; ++y) if (distanceSquared(x, y, myX, myY) <= 36) if (distanceSquared(x, y, myX, myY) < distanceSquared(x, y, hisX, hisY)) sum += operator()(x,y); return sum; } istream& operator>> (istream& in, Island& island) { for (int y = 0; y < island.getHeight(); ++y) for (int x = 0; x < island.getWidth(); ++x) { int d; in >> d; island(x,y) = d; } return in; } int selectHisPosition (Island& island, int myX, int myY, int& hisX, int& hisY) { int bestTaxAmount = -10000; for (int y = 0; y < island.getHeight(); ++y) for (int x = 0; x < island.getWidth(); ++x) { if (island(x,y) > 0.0 && distanceSquared(x, y, myX, myY) > 9) { int hisTaxes = island.taxCollection (x, y, myX, myY); int myTaxes = island.taxCollection (myX, myY, x, y); #ifndef ERROR1 int d = hisTaxes - myTaxes; #else int d = hisTaxes; #endif if (d > bestTaxAmount) { bestTaxAmount = d; hisX = x; hisY = y; } } } return bestTaxAmount; } int selectMyPosition (Island& island, int& myX, int& myY, int hisX, int& hisY) { int bestProfit = -10000; for (int y = 0; y < island.getHeight(); ++y) for (int x = 0; x < island.getWidth(); ++x) { if (island(x,y) > 0.0) { selectHisPosition (island, x, y, hisX, hisY); int hisTaxes = island.taxCollection (hisX, hisY, x, y); int myTaxes = island.taxCollection (x, y, hisX, hisY); #ifndef ERROR2 int d = myTaxes - hisTaxes; #else int d = myTaxes; #endif if (d > bestProfit) { bestProfit = d; myX = x; myY = y; } } } return bestProfit; } int main (int argc, char** argv) { if (argc != 2) { cerr << "Usage: contestSubmission < test.dat | " << argv[0] << " test.dat" << endl; return 1; } ifstream testIn (argv[1]); int w, h; testIn >> w >> h; Island island (w, h); testIn >> island; int myX, myY, hisX, hisY; selectMyPosition (island, myX, myY, hisX, hisY); selectHisPosition (island, myX, myY, hisX, hisY); /* cout << "My choice: " << myX << ' ' << myY << endl; cout << "His choice: " << hisX << ' ' << hisY << endl; cout << "My Taxes: " << island.taxCollection (myX, myY, hisX, hisY) << endl; cout << "His Taxes: " << island.taxCollection (hisX, hisY, myX, myY) << endl; */ int profit = island.taxCollection (myX, myY, hisX, hisY) - island.taxCollection (hisX, hisY, myX, myY); string constestantIn; getline(cin, constestantIn); while (!(!cin) && constestantIn == "") getline(cin, constestantIn); string expectedPhrase = "Place your castle at:"; cout << constestantIn << endl; if (constestantIn.find(expectedPhrase) == string::npos) { cout << " *** error in output phrasing " << endl; return 1; } constestantIn.erase (0, constestantIn.find(expectedPhrase) + expectedPhrase.size()); istrstream input (constestantIn.c_str()); int cx, cy; input >> cx >> cy; cout << cx << " " << cy << endl; if (island(cx,cy) == 0) { cout << "\n*** Illegal location selected." << endl; return 1; } selectHisPosition (island, cx, cy, hisX, hisY); int cprofit = island.taxCollection (cx, cy, hisX, hisY) - island.taxCollection (hisX, hisY, cx, cy); if (profit < cprofit) { cout << "*** Error in judging program!!!!!" << endl; } else if (profit > cprofit) { cout << "*** Error: nonoptimal location selected" << endl; } else cout << "OK" << endl; return 0; }