PKU 3251 Big Square

  • 主人公(?)の位置と正方形の一辺を表すベクトルで全探索です
  • N<=100なのでO(N^4)がギリギリ通る感じです
int main()
{
	static char farm[128][128];
	static char firstLine[128];
	gets(firstLine);
	const int N = atoi(firstLine);
	for (int x = 0; x < N; ++x) {
		gets(farm[x]);
	}

	int answer = 0;
	for (int bx = 0; bx < N; ++bx) {
		for (int by = 0; by < N; ++by) {
			if (farm[bx][by] == 'B') {
				continue;
			}

			for (int tx = 0; tx < N; ++tx) {
				for (int ty = 0; ty < N; ++ty) {
					if (bx == tx && by == ty || farm[tx][ty] != 'J') {
						continue;
					}

					const int dx = tx - bx;
					const int dy = ty - by;
					const int area = dx * dx + dy * dy;
					if (answer >= area) {
						continue;
					}

					const int dx2 = -dy;
					const int dy2 = dx;
					const int tx2 = bx + dx2;
					const int ty2 = by + dy2;
					if (tx2 < 0 || N <= tx2 || ty2 < 0 || N <= ty2 || farm[tx2][ty2] != 'J') {
						continue;
					}

					const int dx3 = dx + dx2;
					const int dy3 = dy + dy2;
					const int tx3 = bx + dx3;
					const int ty3 = by + dy3;
					if (tx3 < 0 || N <= tx3 || ty3 < 0 || N <= ty3 || farm[tx3][ty3] != 'J') {
						continue;
					}

					answer = area;
				}
			}
		}
	}

	printf("%d\n", answer);
}