CF 1453 C. Triangles(枚举)

Gildong has a square board consisting of n rows and n columns of square cells, each consisting of a single digit (from 0 to 9). The cell at the j-th column of the i-th row can be represented as (i,j), and the length of the side of each cell is 1. Gildong likes big things, so for each digit d, he wants to find a triangle such that:

Each vertex of the triangle is in the center of a cell.
The digit of every vertex of the triangle is d.
At least one side of the triangle is parallel to one of the sides of the board. You may assume that a side of length 0 is parallel to both sides of the board.
The area of the triangle is maximized.
Of course, he can’t just be happy with finding these triangles as is. Therefore, for each digit d, he’s going to change the digit of exactly one cell of the board to d, then find such a triangle. He changes it back to its original digit after he is done with each digit. Find the maximum area of the triangle he can make for each digit.

Note that he can put multiple vertices of the triangle on the same cell, and the triangle can be a degenerate triangle; i.e. the area of the triangle can be 0. Also, note that he is allowed to change the digit of a cell from d to d.

Input
Each test contains one or more test cases. The first line contains the number of test cases t (1≤t≤1000).

The first line of each test case contains one integer n (1≤n≤2000) — the number of rows and columns of the board.

The next n lines of each test case each contain a string of n digits without spaces. The j-th digit of the i-th line is the digit of the cell at (i,j). Each digit is one of the characters from 0 to 9.

It is guaranteed that the sum of n2 in all test cases doesn’t exceed 4⋅106.

Output
For each test case, print one line with 10 integers. The i-th integer is the maximum area of triangle Gildong can make when d=i−1, multiplied by 2.

Example
input
5
3
000
122
001
2
57
75
4
0123
4012
3401
2340
1
9
8
42987101
98289412
38949562
87599023
92834718
83917348
19823743
38947912
output
4 4 1 0 0 0 0 0 0 0
0 0 0 0 0 1 0 1 0 0
9 6 9 9 6 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
18 49 49 49 49 15 0 30 42 42
Note
In the first case, for d=0, no matter which cell he chooses to use, the triangle with vertices at (1,1), (1,3), and (3,1) is the biggest triangle with area of 2⋅22=2. Since we should print it multiplied by 2, the answer for d=0 is 4.

For d=1, Gildong can change the digit of the cell at (1,3) into 1, making a triangle with vertices on all three 1’s that has an area of 2.

For d=2, Gildong can change the digit of one of the following six cells into 2 to make a triangle with an area of 12: (1,1), (1,2), (1,3), (3,1), (3,2), and (3,3).

For the remaining digits (from 3 to 9), the cell Gildong chooses to change will be the only cell that contains that digit. Therefore the triangle will always be a degenerate triangle with an area of 0.

In the third case, for d=4, note that the triangle will be bigger than the answer if Gildong changes the digit of the cell at (1,4) and use it along with the cells at (2,1) and (4,3), but this is invalid because it violates the condition that at least one side of the triangle must be parallel to one of the sides of the board.

题意
给你一个正方形,里面有数字0到9,问每种数字能构成三角形面积最大是多少?

思路
三点能构成三角形,现在可以改变一个点的数值,那么只要两个点就行,给了3s,我们先将最大最小x,y记录,然后枚举就行了。

代码

#include <bits/stdc++.h>
typedef long long ll;
const ll mod = 1e9 +7;
using namespace std;
const int N = 2e3 + 5;
ll Case,n;
ll a[N][N];
ll maxx[15],maxy[15],minx[15],miny[15],res[15];
void init(){
	for(int i=0;i<10;i++){
		maxx[i]=maxy[i]=0;
		minx[i]=miny[i]=N;
		res[i]=0;
	}
}
int main(){
	init();
	Case=1;
	scanf("%lld",&Case);
	while(Case--){
		init();
		scanf("%lld",&n);
		for(ll i=1;i<=n;i++){
			for(ll j=1;j<=n;j++){
				scanf("%1lld",&a[i][j]);
				ll t = a[i][j];
				maxx[t]=max(i,maxx[t]);
                minx[t]=min(i,minx[t]);
                maxy[t]=max(j,maxy[t]);
                miny[t]=min(j,miny[t]);
			}
		}
		for(ll i=1;i<=n;i++){
			for(ll j=1;j<=n;j++){
				ll t = a[i][j];
				res[t]=max(res[t],abs(maxx[t]-i)*max(j-1,n-j));
                res[t]=max(res[t],abs(minx[t]-i)*max(j-1,n-j));
                res[t]=max(res[t],abs(maxy[t]-j)*max(i-1,n-i));
            	res[t]=max(res[t],abs(miny[t]-j)*max(i-1,n-i));
			}
		}
		for(int i=0;i<10;i++) printf("%lld ",res[i]);
		puts("");
 	}
	return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值