1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
| #include<iostream> #include<fstream> using namespace std; struct zuobao //创建结构体用来储存横边和竖边的坐标 { int row; int column; }; bool ifexist(int i, int j, int size, const zuobao* rc) { for (int m = 0; m < size; ++m) { if (rc[m].row == i && rc[m].column == j) return true; } return false; } void panduan(int i, int j, int square_size, int lenth, int *number, const zuobao *r, const zuobao*c) {
for (int m = 1; m < square_size; ++m) { int key = 1; for (int n = 0; n < m; ++n) { if (!ifexist(i + n, j, lenth, c)) { key = 0; break; } } if (key == 0) continue; for (int n = 0; n < m; ++n) { if (!ifexist(i + m, j + n, lenth, r)) { key = 0; break; } } if (key == 0) continue; for (int n = 0; n < m; ++n) { if (!ifexist(i + n, j + m, lenth, c)) { key = 0; break; } } if (key == 0) continue; for (int n = 0; n < m; ++n) { if (!ifexist(i, j + n, lenth, r)) { key = 0; break; } } if (key == 0) continue; ++number[m]; } } int main() { int n = 0, m = 0, times = 1; int t = 0; while (cin >> n >> m) { if (0 != t) cout <<endl<< "**********************************" << endl << endl; t = 1; char kind; zuobao * r = new zuobao[m + 2]; zuobao * c = new zuobao[m + 2]; int number[12] = { 0 }; for (int i = 0; i < m; ++i) { cin >> kind; if(kind=='H') cin >> r[i].row >> r[i].column; else cin >> c[i].column >> c[i].row; } cout << "Problem #" << times << endl << endl; ++times; for (int i = 1; i < n; ++i) for (int j = 1; j < n; ++j) { panduan(i, j, n, m, number, r, c); } int temp = 0; for (int i = 1; i < n; ++i) { if (number[i] != 0) { cout << number[i] << " square (s) of size " << i << endl; temp = 1; } } if (0 == temp) cout << "No completed squares can be found." << endl;
} }
|