前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【Gym - 100947G】Square Spiral Search

【Gym - 100947G】Square Spiral Search

作者头像
饶文津
发布2020-06-02 15:31:02
2720
发布2020-06-02 15:31:02
举报
文章被收录于专栏:饶文津的专栏饶文津的专栏

BUPT 2017 summer training (for 16) #1C

题意

A new computer scientist is trying to develop a new memory management system called "spiral memory management".

The basic idea of this system is that it represents the memory as a 2D grid with a predefined origin point, and the address of each memory location is represented as its (x,y) coordinates, and it tries to store frequently used data as close to the origin point as possible.

This system needs a search algorithm. Our computer scientist is currently developing an algorithm called "square spiral search".

The algorithm searches the memory locations in the following order (also shown in the figure):

(0,0), (1,0), (1,1), (0,1), (-1,1), (-1,0), (-1,-1), (0,-1), (1,-1), (2,-1), (2,0), (2,1), (2,2), (1,2), (0,2), (-1,2), (-2,2), (-2,1), (-2,0), (-2,-1), (-2,-2,) ,(-1,-2) ... and so on. Now he is wondering: how many steps would it take to reach a memory location with the address (x, y) using the square spiral search. Can you help him find the answer?

题解

代码

代码语言:javascript
复制
#include <cstdio>
#include <cmath>
#include <algorithm>
#define ll long long
using namespace std;
int main(){
	int t;
	scanf("%d",&t);
	while(t--){
		int x,y;
		scanf("%d%d",&x,&y);
		if(!x&&!y){
			puts("0");continue;		
		}
		ll s=max(abs(x),abs(y)),ans=(s*2+1)*(s*2+1)-1;
		if(y==-s)
			ans-=(s-x);
		else if(x==-s)
			ans-=s*2+(s+y);
		else if(y==s)
			ans-=s*4+(s+x);
		else
			ans-=s*6+(s-y);
		printf("%lld\n",ans);
	}
	return 0;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2017-07-18 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 题意
  • 题解
  • 代码
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档