洛谷P2239 螺旋矩阵

传送门

分析:将整个矩阵看成 “回” 形状的分层结构,然后进行去层处理,使得要求得 ((i,j)) 处于最外层,然后再分情况讨论。最外面的一层共有数: $ 4 * n - 4 $ . 第二层共有数: 4n-4 -8
假设 $ (i,j) $ 外共有 $ x $ 层,则外层所有的数为: $ ans=4
nx-4x*x $ 。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <cmath>
using namespace std ;

inline int read () {
	int f = 1 , x = 0 ;
	char ch = getchar () ;
	while(ch > '9' || ch < '0')  {if(ch == '-')  f = -1 ; ch = getchar () ;} 
	while(ch >= '0' && ch <= '9') {x = (x << 1) + (x << 3) + ch - '0' ; ch = getchar () ;}
	return x * f ;
}

int n , x , y ;
int t , ans ;

int main () {
	n = read () ;
	x = read () ; y = read () ;
	t = min(x - 1 , n - x) ;
	t = min(t , min(y - 1 , n - y) ) ;
	ans = 4 * n * t - 4 * t * t ;
	
	if(x - t == 1)
		ans += y - t ;
	else if(x + t == n)
		ans += 3 * n - 5 * t - 1 - y ;
	else if(y - t == 1) 
		ans += 4 * n - 7 * t - 2 - x ;
	else ans += n - 3 * t + x - 1 ;
	printf("%d
" , ans) ;
	return 0 ;
}
原文地址:https://www.cnblogs.com/Stephen-F/p/10588108.html