P2239 [NOIP2014 普及组] 螺旋矩阵

P2239 [NOIP2014 普及组] 螺旋矩阵 50分

//O(n^2)复杂度,能算n<=10000的 
#include <bits/stdc++.h>
using namespace std;
//row当前行, column当前列, left:左边界,righ:右边界,top:上边界,bottom:下边界 
int n, x, y, ans, row=1, column=0, lef, righ, top, bottom;
int main()
{
	scanf("%d %d %d", &n, &x, &y);
	lef=1;			//左边界 
	righ=n;			//右边界 
	top=2;			//上边界 
	bottom=n; 		//下边界 
	while(1){
		//从左向右走 
		while(column<righ){
			column++;
			ans++;
			if(row==x && column==y){
				printf("%d", ans);
				return 0;
			}
		}
		righ--;
		//从上向下走 
		while(row<bottom){
			row++;
			ans++;
			if(row==x && column==y){
				printf("%d", ans);
				return 0;
			}
		}
		bottom--;
		//从右向左走 
		while(column>lef){
			column--;
			ans++;
			if(row==x && column==y){
				printf("%d", ans);
				return 0;
			}
		}
		lef++;
		//从下向上走 
		while(row>top){
			row--;
			ans++;
			if(row==x && column==y){
				printf("%d", ans);
				return 0;
			}
		}
		top++;
	}
	return 0;
}

P2239 [NOIP2014 普及组] 螺旋矩阵 递归满分

#include <bits/stdc++.h>
using namespace std;
int n, x, y, ans;
int asd(int n, int row, int column)
{
	if(row==1){		//第1行 
		return column;
	}
	else if(column==n){	//第n列 
		return n+row-1;
	}
	else if(row==n){	//第n行 
		return 3*n-column-1;	//n + n-1 + n-1 - column + 1;
	} 
	else if(column==1){	//第1列 
		return 4*n-row-2;		//n + n-1 + n-1 + n-1 - row +1;
	}
	else{ 
		return asd(n-2, row-1, column-1)+4*n-4;
	}
}
int main()
{
	scanf("%d %d %d", &n, &x, &y);
	printf("%d", asd(n, x, y));
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ypeijasd

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值