AtCoder Beginner Contest 244 B题(超详讲解)

目录

 题目

 状态

 代码及讲解

结语 


 题目

Time Limit: 2 sec / Memory Limit: 1024 MB

Score : 200 points

Problem Statement

Consider an xy-plane. The positive direction of the x-axis is in the direction of east, and the positive direction of the y-axis is in the direction of north.
Takahashi is initially at point (x, y) = (0, 0) and facing east (in the positive direction of the x-axis).

You are given a string T = t_1t_2\ldots t_NT=t1​t2​…tN​ of length NN consisting of S and R. Takahashi will do the following move for each i = 1, 2, \ldots, Ni=1,2,…,N in this order.

  • If t_i =ti​= S, Takahashi advances in the current direction by distance 1.
  • If t_i =ti​= R, Takahashi turns 90 degrees clockwise without changing his position. As a result, Takahashi's direction changes as follows.
    • If he is facing east (in the positive direction of the xx-axis) before he turns, he will face south (in the negative direction of the y-axis) after he turns.
    • If he is facing south (in the negative direction of the yy-axis) before he turns, he will face west (in the negative direction of the x-axis) after he turns.
    • If he is facing west (in the negative direction of the xx-axis) before he turns, he will face north (in the positive direction of the y-axis) after he turns.
    • If he is facing north (in the positive direction of the yy-axis) before he turns, he will face east (in the positive direction of the x-axis) after he turns.

Print the coordinates Takahashi is at after all the steps above have been done.

Constraints

  • 1 \leq N \leq 10^51≤N≤105
  • N is an integer.
  • T is a string of length N consisting of S and R.

Input

Input is given from Standard Input in the following format:

N
T

Output

Print the coordinates (x, y) Takahashi is at after all the steps described in the Problem Statement have been completed, in the following format, with a space in between:

x y

Sample Input 1 Copy

Copy

4
SSRS

Sample Output 1 Copy

Copy

2 -1

Takahashi is initially at (0, 0)(0,0) facing east. Then, he moves as follows.

  1. t_1 =t1​= S, so he advances in the direction of east by distance 1, arriving at (1, 0)(1,0).
  2. t_2 =t2​= S, so he advances in the direction of east by distance 1, arriving at (2, 0)(2,0).
  3. t_3 =t3​= R, so he turns 9090 degrees clockwise, resulting in facing south.
  4. t_4 =t4​= S, so he advances in the direction of south by distance 11, arriving at (2, -1).

Thus, Takahashi's final position, (x, y) = (2, -1), should be printed.


Sample Input 2 Copy

Copy

20
SRSRSSRSSSRSRRRRRSRR

Sample Output 2 Copy

Copy

0 1

 中文翻译

 

 状态

 已ac

 

 代码及讲解

#include<iostream>
using namespace std;
int main(){
	int n;
	cin>>n;
	char str[n];
	for(int i = 0;i<n;i++){
		cin>>str[i];
	}
	int x = 0,y = 0;
	int dx[] = {1,0,-1,0},dy[] = {0,-1,0,1};
	int j = 0;
	for(int i = 0;i<n;i++){
		if(str[i] == 'S'){
			
			x+=(dx[j]*1);
			y+=(dy[j]*1);
			
		}else{
			
			j = (j+1)%4;
		}
	}
	cout<<x<<" "<<y<<endl;
	return 0;
}

本道题是以东,北为正方向

从起点出发,起初朝向东这个方向, 如果输入S则向该方向移动一个单位,如果输入R则顺时针移动90度(只是朝向变化了,并没有移动),再输入S则再朝该方向移动一个单位,如果输入R则继续转动90度。。。求最后的坐标

 首先是以朝向东,以这一点开始枚举四个方向

dx[] = {1,0,-1,0},dy[] = {0,-1,0,1}

本题关键是枚举四个方向

即代表向东移动一位 x+=dx[0],y+=dx[0]

代表向南移动一位 x+=dx[1],y+=dx[1]

代表向西移动一位 x+=dx[2],y+=dx[2]

代表向北移动一位 x+=dx[3],y+=dx[3]

每转动90度(输入‘R’)换一个   j++   再求取移动距离

注意要j = (j+1)%4

int j = 0;
	for(int i = 0;i<n;i++){
		if(str[i] == 'S'){
			
			x+=(dx[j]*1);//x方向移动的距离
			y+=(dy[j]*1);//y方向移动的距离
			
		}else{
			
			j = (j+1)%4;//一个东西南北四个方向嘛
		}
	}

dx[] = {1,0,-1,0},dy[] = {0,-1,0,1}

这段代码是在比赛的时候想到的,思路产生是想到了前几天看到了一个 视频,那是一个讲解简单dp的,虽然此题与dp无关哈哈

结语 

一起加油吧~~~

  • 37
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 42
    评论
评论 42
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

超级小何

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

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

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

打赏作者

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

抵扣说明:

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

余额充值