题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=12821
枚举可能的正方形边长值。
代码如下:
#include <algorithm> #include <iostream> #include <sstream> #include <string> #include <vector> #include <stack> #include <deque> #include <queue> #include <set> #include <map> #include <cstdio> #include <cstdlib> #include <cctype> #include <cmath> #include <cstring> using namespace std; /*************** Program Begin **********************/ class AlienAndGame { public: int getNumber(vector <string> board) { int rows = board.size(); int cols = board[0].size(); int n = min(board.size(), board[0].size()); for (; n >= 1; n--) { // 从大到小枚举各个可能的 正方形边长 for (int j = 0; j < rows - n + 1; j++) { for (int k = 0; k < cols - n + 1; k++) { // 判断以board[j][k]为正方形左上顶点时是否有边长为n的正方形 bool flag_row; for (int s = 0; s < n; s++) { flag_row = true; for (int t = 0; t < n; t++) { if ( board[j+s][k] != board[j+s][k+t] ) { flag_row = false; break; } } if (!flag_row) { break; } } if (flag_row) { return n * n; } } } } return 1; } }; /************** Program End ************************/