【Codeforces 1459 B】Move and Turn,奇偶性分类,公式推导

举报
小哈里 发表于 2022/05/10 22:21:59 2022/05/10
【摘要】 problem B. Move and Turn time limit per test2 seconds memory limit per test512 megabytes inputstandar...

problem

B. Move and Turn
time limit per test2 seconds
memory limit per test512 megabytes
inputstandard input
outputstandard output
A robot is standing at the origin of the infinite two-dimensional plane. Each second the robot moves exactly 1 meter in one of the four cardinal directions: north, south, west, and east. For the first step the robot can choose any of the four directions, but then at the end of every second it has to turn 90 degrees left or right with respect to the direction it just moved in. For example, if the robot has just moved north or south, the next step it takes has to be either west or east, and vice versa.

The robot makes exactly n steps from its starting position according to the rules above. How many different points can the robot arrive to at the end? The final orientation of the robot can be ignored.

Input
The only line contains a single integer n (1≤n≤1000) — the number of steps the robot makes.

Output
Print a single integer — the number of different possible locations after exactly n steps.

Examples
inputCopy
1
outputCopy
4
inputCopy
2
outputCopy
4
inputCopy
3
outputCopy
12
Note
In the first sample case, the robot will end up 1 meter north, south, west, or east depending on its initial direction.

In the second sample case, the robot will always end up 2–√ meters north-west, north-east, south-west, or south-east.

solution

/*
题意:
+ 从原点出发走n步,相邻两步方向必须为{左右}和{上下}二选一
+ 求能到达多少个不同的点
思路:
+ n为偶数时,水平和垂直各走n/2步。考虑往左有0,1,2,,,n/2种,所以最后是(n/2+1)*(n/2+1)种。
+ n为奇数时,分先走垂直或水平两种情况,答案都是(n/2+1)(n/2+2)。因为两者不重合,所以直接*2.
*/
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn = 110;
int main(){
	ios::sync_with_stdio(false);
	int n;  cin>>n;
	int k = n/2;
	if(n%2==0){
		cout<<(k+1)*(k+1)<<"\n";
	}else{
		cout<<(k+1)*(k+2)*2<<"\n";
	}
	return 0;
}



  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

文章来源: gwj1314.blog.csdn.net,作者:小哈里,版权归原作者所有,如需转载,请联系作者。

原文链接:gwj1314.blog.csdn.net/article/details/113471742

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。