/* Subject: Contest submission for problem #1, file 1.cc */ /* rob@imail.EECS.Berkeley.EDU */ /* Wed Sep 10 23:11:50 PDT 2003 */ /*___CONTEST_SUBMISSION___ rob 1 */ //Assignment 2, Problem 1: Triangles //Robert Chu #include int min(int a, int b) { if(a < b) return a; else return b; } struct charCount { char letter; int count; charCount* next; }; charCount* head = NULL; void addToList(char a, int c) { if(head == NULL) { head = new charCount; head->letter = a; head->count = c; head->next = NULL; return; } else { charCount* cursor = head; charCount* prev = head; while(cursor != NULL) { if(a == cursor->letter) { cursor->count = c; return; } else if(a < cursor->letter) { charCount* temp = new charCount; temp->letter = a; temp->count = c; temp->next = cursor; prev->next = temp; return; } else if(cursor->next == NULL) { charCount* temp2 = new charCount; temp2->letter = a; temp2->count = c; temp2->next = NULL; cursor->next = temp2; return; } prev = cursor; cursor = cursor->next; } } } int main() { bool l, t, r, b; bool flag; int direction[4]; int count = 0; int level; while(true) { int dim; cin >> dim; if(dim == 0) { break; } char matrix[dim][dim]; int i; int j; int x = 0; int y = 0; charCount* cursor = head; for(i = 0; i < dim; i++) { for(j = 0; j < dim; j++) { cin >> matrix[i][j]; addToList(matrix[i][j], 0); } } for(i = 0; i < dim; i++) { for(j = 0; j < dim; j++) { l = t = r = b = false; cursor = head; while(cursor != NULL) { if(cursor->letter == matrix[i][j]) { count = cursor->count; break; } cursor = cursor->next; } for(x = 0; x < 4; x++) { direction[x] = 0; } if(((i-1) >= 0) && matrix[i-1][j] == matrix[i][j]) { //check top t = true; } if(((i+1) < dim) && matrix[i+1][j] == matrix[i][j]) { //check bottom b = true; } if(((j-1) >= 0) && matrix[i][j-1] == matrix[i][j]) { //check left l = true; } if(((j+1) < dim) && matrix[i][j+1] == matrix[i][j]) { //check right r = true; } if(l && t) { count++; direction[0] = 1; level = 0; flag = true; while(true) { if((i-2-level) >= 0) { y = j; for(x = i-2-level; x <= i; x++) { if((matrix[x][y] != matrix[i][j]) || ((y-1)<0 && x!=i)) { flag = false; break; } y--; } if(flag) { direction[0]++; level++; count++; } else { break; } } else { break; } } } if(t && r) { count++; direction[1] = 1; level = 0; flag = true; while(true) { if((i-2-level) >= 0) { y = j; for(x = i-2-level; x <= i; x++) { if((matrix[x][y] != matrix[i][j]) || ((y+1)>=dim && x!=i)) { flag = false; break; } y++; } if(flag) { direction[1]++; level++; count++; } else { break; } } else { break; } } } if(r && b) { count++; direction[2] = 1; level = 0; flag = true; while(true) { if((i+2+level) < dim) { y = j; for(x = i+2+level; x >= i; x--) { if((matrix[x][y] != matrix[i][j]) || ((y+1)>=dim && x!=i)) { flag = false; break; } y++; } if(flag) { direction[2]++; level++; count++; } else { break; } } else { break; } } } if(b && l) { count++; direction[3] = 1; level = 0; flag = true; while(true) { if((i+2+level) < dim) { y = j; for(x = i+2+level; x >= i; x--) { if((matrix[x][y] != matrix[i][j]) || ((y-1)<0 && x!=i)) { flag = false; break; } y--; } if(flag) { direction[3]++; level++; count++; } else { break; } } else { break; } } } count += min(direction[0], direction[1]); count += min(direction[1], direction[2]); count += min(direction[2], direction[3]); count += min(direction[3], direction[0]); cursor->count = count; } } cursor = head; count = 0; while(cursor != NULL) { count += cursor->count; cursor = cursor->next; } cout << "(" << count << ")"; while(head != NULL) { cout << " " << head->count << " " << head->letter; head = head->next; } cout << endl; } exit(0); }