/* Subject: Contest submission for problem #1, file 1.cc */ /* cs188-ah@imail.EECS.Berkeley.EDU */ /* Wed Sep 10 13:49:48 PDT 2003 */ /*___CONTEST_SUBMISSION___ cs188-ah 1 */ //Estimated time: 2hr30minute Real time: 4 hours //Took so long to get triangle detection subroutines //to work perfectly! Also was tricky coming up with //surrounding matrix trick //SID 14394533 #include #include #include #include #include using namespace std; typedef set::iterator setiter; typedef vector::iterator viter; int tri1(const vector& nn, size_t n, char t, size_t x, size_t y); int tri2(vector nn, size_t n, char t, size_t x, size_t y); int main(int argc, char* argv[]) { vector nn(60); set sc; map c; size_t n, total; while (cin >> n) { if (n == 0) break; c.clear(); sc.clear(); total = 0; for(viter vi = nn.begin(); vi != nn.end(); vi++) *vi = string("000000000100000000010000000001000000000100000000010000000001"); // surround the matrix with non letter entries, necessary to find all of tri2 for(int ii = 20; ii < 20 + n; ii++) { string row; cin >> row; nn[ii].replace(20, n, row); // cout << nn[ii] << endl; for(int jj =0; jj < row.length(); jj++) sc.insert(char(row[jj])); } // for each matrix, find number of triangle for each kind of letter for(setiter si = sc.begin(); si != sc.end(); si++) { // look for matrices 2x2 up to to size nxn for(int ii = 2; ii <= n ; ii++) { // start at edge of our matrix left to right for(int jj = 0; jj <= 60 - ii; jj++) // go work our way downwards for(int kk = 0; kk <= 60 - ii; kk++) { c[*si] += tri1(nn, ii, *si, jj, kk); c[*si] += tri2(nn, ii, *si, jj, kk); } } total += c[*si]; } cout << "(" << total << ")"; for(setiter si = sc.begin(); si != sc.end(); si++) cout << " " << c[*si] << " " << *si; cout << endl; } } // find triangle of matrix n size // only looks for triangles with legs // alined along 2 axis int tri1(const vector& nn, size_t n, char t, size_t x, size_t y) { int t1 = 1, t2 = 1, t3 = 1, t4 = 1; // for 2x2 just do brute force if (n == 2) { return (((nn[0 + x][0 + y] == t) && (nn[0 + x][1 + y] == t) && (nn[1 + x][0 + y] == t)) + ((nn[0 + x][0 + y] == t) && (nn[0 + x][1 + y] == t) && (nn[1 + x][1 + y] == t)) + ((nn[0 + x][0 + y] == t) && (nn[1 + x][0 + y] == t) && (nn[1 + x][1 + y] == t)) + ((nn[1 + x][0 + y] == t) && (nn[1 + x][1 + y] == t) && (nn[0 + x][1 + y] == t))); } // looks for // A // A A // A A A for(int ii = 0; ii < n; ii++) for(int jj = 0 ; jj < ii + 1; jj++) if (nn[ii + x][jj + y] != t) t1 = 0; // looks for // A A A // A A // A for(int ii = 0; ii < n; ii++) for(int jj = 0; jj < n - ii; jj++) if (nn[ii + x][jj + y] != t) t2 = 0; // looks for // A A A // A A // A for(int ii = 0; ii < n; ii++) for(int jj = ii; jj < n; jj++) if (nn[ii + x][jj + y] != t) t3 = 0; // looks for // A // A A //A A A for(int ii = 0; ii < n; ii++) for(int jj = n - ii - 1; jj < n; jj++) if (nn[ii + x][jj + y] != t) t4 = 0; return t1 + t2 + t3 + t4; } int tri2(vector nn, size_t n, char t, size_t x, size_t y) { int t1 = 1, t2 = 1, t3 = 1, t4 = 1; int np = n + 1; // only possible in odd size matrix if ((n % 2) == 0) return 0; // look for // A A A A A // A A A // A for(int ii = 0, kk = n; ii < (np/2); ii++, kk--) for(int jj = ii; jj < kk; jj++) if (nn[ii + x][jj + y] != t) t1 = 0; // look for // A // A A A // A A A A A for(int ii = (np/2) - 1, kk = 0; ii < n; ii++, kk++) for(int jj = (np/2) - kk -1; jj < (np/2) + kk ; jj++) if (nn[ii + x][jj + y] != t) t2 = 0; // look for // A // A A // A A A // A A // A for(int ii = 0, kk = n; ii < n; ii++,kk--) { int zz = (ii > kk) ? kk : ii + 1; for(int jj = 0; jj < zz; jj++) { if (nn[ii + x][jj + y] != t) t3 = 0; } } // look for // A // A A // A A A // A A // A for(int ii = 0, kk = n - 1; ii < n; ii++, kk--) { int zz = (ii > kk) ? ii : kk; for(int jj = zz; jj < n ; jj++) if (nn[ii + x][jj + y] != t) t4 = 0; } return t1 + t2 + t3 + t4; }