There was a closed rule is that
The princess trapped in dreams, dreams of space is infinite, the princess is walk not to come out on their own,
System will randomly many actions, the player must choose the right orders on behalf of the princess, the princess under the direction of a players to choose, repeated several times after the can out of the woods,
Instruction has three characters S, R, L
S: before further
R: turn right
L: turn left
If the princess repeat the wrong instruction, she would have been in a circle, get out dreams,
The so-called "circle" means: no matter how many times the princess repeat instructions, she is always in a starting point as the center of the circle, circle radius is R, never go out of this circle, more can't walk out of the dream,
Igawa has been stuck in this for a long time, he is a pain can't save the princess as soon as possible from suffering, then seek help from clever you, let you to write code to determine which instructions are correct, which instructions are wrong,
Input:
The first line of an integer n
After a total of n lines, each behavior a command string
Input constraints:
N in the interval [1, 50]
Starting from the second row, each row command string length of 1-50, and contain only letter S, L, R
Output:
There is only one word,
Instruction of mistakes to print no
Instruction sequence correctly printed yes
Example 1:
Enter
1
SLSR
O
Yes
Explanation: princess assumes that the initial state to the north, the princess of the action sequence, in order to go forward, turn left, go forward, turn right, at this time the princess still to the north, but the position has been moved, as long as the time is long enough, the princess would have been in this direction, so the princess didn't come full circle, in the sense that she can walk out of the dream, the instructions are correct
Example 2:
Enter
2
SSSS
R
O
No
Explanation: the princess has been along a length of 4 steps of small circles in the square, instruction is wrong
CodePudding user response:
My initial idea is to simulate, according to his instruction, input to simulate the way, after the last judgment under all through, and the location of the initial point gapIf as initial point, it is no, otherwise it is yes
CodePudding user response:
Count all the instruction sequence, the number of the L in the must be equal to the number of R, otherwise, between L and R, S, will be around,Is this math problem? Programming is not the key,
CodePudding user response:
I think as long as the instruction to ensure that the two conditions can (in fact, the combined with the conditions of the 1 l and 2 l)One is that after the instruction execution direction constant offset (L and R)//light direction not the same, also can't stay in the original position, such as LLSSRRSS, direction invariable, it stays at the origin
Second, after the instruction execution position (i.e., don't stay put)//light position has not changed, but also direction invariable, the LZ SSSSR example is the question, whether turn L and R are 90 degrees turn, fourth will be 360 degrees turn back to the origin, if it is LL or turn RR180 degree, the second will return to the origin
So, in conclusion, I think as long as meet the conditions 1 and 2 at the same time, will not spare circle (, that is, no matter how to repeat, courage is )
Code sample
import java.util.Random;
import java.util.Scanner;
Public class Sample {
Public static void main (String [] args) {
Boolean test=true;//user manual input into a false
String [] CMD;
if (! Test) {
System. The out. Println (" input ");
CMD=getUserInput ();
Output System. Out. Printf (" % s \ n \ n ", checkCmd (CMD));
} else {//automatic test
for (int i=0; i<5; I++) {
CMD=getRandomCmd (I);
System. The out. Printf (" test % d \ n \ n \ n input % d ", I + 1, CMD. The length).
For (String s: CMD) {
System.out.println(s);
}
Output System. Out. Printf (" % s \ n \ n \ n ", checkCmd (CMD));
}
}
}
Public static String [] getUserInput () {
Scanner sc=new Scanner(System.in);
Int n=0;
While (true) {
Try {
//System. Out. Println (" please enter a number between 1 and 50: ");
N=Integer. The valueOf (sc) nextLine ());
If (n> 0 & amp; & N<51) break;
} the catch (Exception e) {
N=1;//the default
break;
}
//System. Out. Println (" input error, please heavy lose, ");
}
CMD=new String String [] [n].
for (int i=0; iWhile (true) {
Try {
//System. Out. Printf (" please enter the first line % d instructions (instructions must be in [the LRS] letter to form the total length of not more than 50) : ");
CMD=[I] sc. NextLine ();
If (CMD [I] matches (" [the LRS] 50 {1} ")) break;
} the catch (Exception e) {
CMD="S" [I];//the default
break;
}
//System. Out. Println (" input error, please heavy lose, ");
}
}
Return CMD.
}
Public static String checkCmd (String [] CMD) {
Int Angle=0 x0=0, y0=0 to x=x0, y=y0;//direction (perspective),
For (String s: CMD) {
For (char c: s.t oCharArray ()) {
If (c=='L') {
Angle -=90;//turn left as - 90
} else if (c=='R') {
Angle +=90;//right to + 90 degrees
} else if (c=='S') {//0 degrees north of the
Int m=Angle % 360;
If (m==0) y++;//north
Else if (m==- 90 | | m==270) x -;//went west
Else if (m==180 | | m==- 180) - y;//south
Else if (m==- 270 | | m==90) x++;//go east
}
}
}
If (Angle %==0 360 & amp; & (x!=x0 | | y!=y0)) {//if the same direction change
Return "yes";
} else {
Return "no";
}
}
}
CodePudding user response:
Forget to send the test data of the codeExplain the offset direction is constant or see the L and R, but turn to see the point of view of % 360 whether or not the same, because there is no R LLLLS offset is also a direction (turned 360 degrees), so it is also yes
The complete code
import Java. Util. Random;
import java.util.Scanner;
Public class Sample {
Public static void main (String [] args) {
Boolean test=true;//user manual input data into a false
String [] CMD;
if (! Test) {
System. The out. Println (" input ");
CMD=getUserInput ();
Output System. Out. Printf (" % s \ n \ n ", checkCmd (CMD));
} else {//automatic test
for (int i=0; i<5; I++) {
CMD=getRandomCmd (I);
System. The out. Printf (" test % d \ n \ n \ n input % d ", I + 1, CMD. The length).
For (String s: CMD) {
System.out.println(s);
}
Output System. Out. Printf (" % s \ n \ n \ n ", checkCmd (CMD));
}
}
}
Public static String [] getUserInput () {
Scanner sc=new Scanner(System.in);
Int n=0;
While (true) {
Try {
//System. Out. Println (" please enter a number between 1 and 50: ");
N=Integer. The valueOf (sc) nextLine ());
If (n> 0 & amp; & N<51) break;
} the catch (Exception e) {
N=1;//the default
break;
}
//System. Out. Println (" input error, please heavy lose, ");
}
CMD=new String String [] [n].
for (int i=0; iWhile (true) {
Try {
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull