目录
题目
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=t1t2…tN of length NN consisting of
S
andR
. 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
andR
.
Input
Input is given from Standard Input in the following format:
N TOutput
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 SSRSSample Output 1 Copy
Copy
2 -1Takahashi is initially at (0, 0)(0,0) facing east. Then, he moves as follows.
- t_1 =t1=
S
, so he advances in the direction of east by distance 1, arriving at (1, 0)(1,0).- t_2 =t2=
S
, so he advances in the direction of east by distance 1, arriving at (2, 0)(2,0).- t_3 =t3=
R
, so he turns 9090 degrees clockwise, resulting in facing south.- 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 SRSRSSRSSSRSRRRRRSRRSample 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无关哈哈
结语
一起加油吧~~~