L - Donald is a postman

唐老鸭作为邮递员需将儿童来信放入特定信箱中,每个信箱位于不同的位置。此问题探讨如何计算唐老鸭按信件顺序放置所有信件所需的最小步数。

Donald Duck works as a postman for the Walt Disney Studios. He delivers children’s letters from all over the world to his friends, which are cartoon characters. The Studios has three cases for the letters, with nine sections in each case. Every section has the name of the receiver on it. All cases stand in a row as it is shown at the picture below.
Donald Duck have brought n letters today. Initially, he stands near the leftmost case. He has to make one step to go to the neighboring case or to the previous one. How many steps will he make until he puts all the letters into the respective sections, if he does this in the order they are in his bag?
Problem illustration
Input
The first line contains an integer n that is the amount of letters in Donald’s bag (1 ≤ n ≤ 1 000). The following n lines contain receivers of the letters in the order they are in the bag.
Output
Output the number of steps Donald should make to put all the letters into the cases.
Example
input output
4
Aurora
Tiana
Ariel
Mulan
5
Hint


题意:

三个盒子上面每个有9个不同的信箱,相邻的盒子之间的距离为1,要将n封信放入不同信箱,问总共需要走几步》

思路:

预处理一下各个名字在哪个case里面,然后在按照输入顺序移动即可!

代码:

#include<bits./stdc++.h>
using namespace std;
int main()
{
	//freopen("aaa.txt","r",stdin);
	int n,i,x,y,sum=0;
	string s;
	char c;
	map<char,int>a;
	a['A']=0;a['P']=0; a['O']=0; a['R']=0;
	a['B']=1; a['M']=1; a['S']=1;
	a['D']=2; a['G']=2; a['J']=2; a['K']=2; a['T']=2; a['W']=2; 
	cin>>n;
	cin>>s; c=s[0];
	x=a[c]; sum=x;
	for(i=1;i<n;i++)
	{
		cin>>s;
		c=s[0];
		y=a[c];
		sum+=fabs(y-x);
		x=y;
	}
	cout<<sum<<endl;
	return 0;
} 

心得:
今天5小时 和队友就AC了两个水题,好气好气好气好气 !!!!!!!!!!!!!!!!!!感觉自己弱爆了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值