HzuWHF


私信TA

用户名:I7I08I9047

访问量:76041

签 名:

我RUN了

等  级
排  名 18
经  验 20388
参赛次数 13
文章发表 127
年  龄 3
在职情况 学生
学  校 贺州学院
专  业

  自我简介:

解题思路:

       遍历每个点,依次对当前点往右下角拓展一个单位,也就是把边长增加一个单位,issame 函数用来判断当前颜色与拓展边的颜色是否一致,Offset为偏移总量,往上遍历就可以获取拓展边的颜色,遍历就能得出最大同色正方形的 Side length。


参考代码:

#include<bits/stdc++.h>
using namespace std;

int Map[201][201];

bool issame(int posx, int posy, int Offset) {
	/*        保存原颜色       */
	int color = Map[posx][posy];
	/*    往右下偏移一个单位   */
	posx++; posy++;

	/*    往上往左遍历拓展边   */
	if (Map[posx][posy] != color)
		return false;
	for (int i = 1; i <= Offset; i++) {
		if (Map[posx - i][posy] != color) return false;
		if (Map[posx][posy - i] != color) return false;
	}
	return true;
}

int main() {
	/*   side是所求最大的边长  */
	int high, width, side = 1;
	cin >> high >> width;

	/*         Map存储         */
	for (int i = 0; i < high; i++)
		for (int d = 0; d < width; d++)
			cin >> Map[i][d];

	bool end = false;
	/*   两层for遍历所有的点  */
	for (int posx = 0; posx < high; posx++) {
		for (int posy = 0; posy < width; posy++) {

			/*  如果找到最大色块的边长已经比
			    该点余下的高度大就不用在找了  */
			if (high - posx - 1 < side) {
				end = true; break;
			}

			/*  如果找到最大色块的边长已经比
			    该点余下的宽度大就跳到下一行  */
			if (width - posy - 1 < side)
				continue;
			/*      Offset为在当前点往右下的偏移量      */
			int Offset = 1, tempx = posx, tempy = posy;
			while (tempx + 1 < high && tempy + 1 < width) {   /*    边界条件    */

				if (issame(tempx, tempy, Offset)) {           /*  函数功能见上  */
					tempx++; tempy++; Offset++;               /*    更新状态    */
					if (Offset > side) side = Offset;         /*  更新最大边长  */
				}
				else
					break;
			}
		}
		if (end == true)
			break;
	}
	cout << side*side << endl;
	return 0;
}


 

0.0分

15 人评分

  评论区