记录编号 140747 评测结果 AAAAAAAAAA
题目名称 [NOIP 2014PJ]螺旋矩阵 最终得分 100
用户昵称 GravatarSatoshi 是否通过 通过
代码语言 C++ 运行时间 0.006 s
提交时间 2014-11-24 20:39:52 内存使用 0.42 MiB
显示代码纯文本
#include <fstream>
using namespace std;
int small(int x,int y)
{
	if(x>y)return y;
	else return x;
}
int main()
{
	int x[30001]={0},y[30001]={0},n,a,b,aa,bb,i,up=0,down=0,ceng=0,xi=0,yi=0,ans=0;
	ifstream in("matrixc.in");
	ofstream out("matrixc.out");
	in>>n>>a>>b;
	x[1]=1;
	if(n%2==1)
	{
		up=n;
		down=2;
	    for(i=2;i<=n/2+1;i++)
	    {
		    x[i]=x[i-1]+(up-1)*4;
			up-=2;
	    }
		for(i=n/2+2;i<=n;i++)
		{
			x[i]=x[i-1]-(down-1)*4;
			down+=2;
		}
		for(i=1;i<=n/2+1;i++)
		{
			y[i]=x[n-i+1]+(n/2+1-i)*2;
		}
		for(i=n/2+2;i<=n;i++)
		{
			y[i]=x[n-i+1]+(i-(n/2+1))*2;
		}
		aa=a;
		bb=b;
		if(a>n/2+1)aa=n-a+1;
		if(b>n/2+1)bb=n-b+1;
		ceng=small(aa,bb);
		//out<<ceng<<endl;
		xi=ceng;
		yi=n-ceng+1;
		if(a==xi)ans=x[xi]+b-xi;
		if(a==yi)ans=x[yi]+yi-b;
		if(b==xi)ans=y[xi]+yi-a;
		if(b==yi)ans=y[yi]+a-xi;
		if(a==xi&&b==xi)ans=x[xi];
	}
	else
	{
		up=n;
		down=2;
	    for(i=2;i<=n/2;i++)
	    {
		    x[i]=x[i-1]+(up-1)*4;
			up-=2;
	    }
		x[n/2+1]=x[n/2]+2;
		for(i=n/2+2;i<=n;i++)
		{
			x[i]=x[i-1]-down*4;
			down+=2;
		}
		for(i=1;i<=n/2;i++)
		{
			y[i]=x[n-i+1]+(n/2-i)*2+1;
		}
		for(i=n/2+1;i<=n;i++)
		{
			y[i]=x[n-i+1]+(i-(n/2))*2-1;
		}
		aa=a;
		bb=b;
		if(a>n/2)aa=n-a+1;
		if(b>n/2)bb=n-b+1;
		ceng=small(aa,bb);
		//out<<ceng<<endl;
		xi=ceng;
		yi=n-ceng+1;
		if(a==xi)ans=x[xi]+b-xi;
		if(a==yi)ans=x[yi]+yi-b;
		if(b==xi)ans=y[xi]+yi-a;
		if(b==yi)ans=y[yi]+a-xi;
		if(a==xi&&b==xi)ans=x[xi];
	}
	//for(i=1;i<=n;i++)out<<x[i]<<' '<<endl;
	//out<<endl;
	//for(i=1;i<=n;i++)out<<y[i]<<' '<<endl;
	out<<ans<<endl;
	in.close();
	out.close();
	return 0;
}