fork(6) download
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. struct point{
  6. int x, y;
  7. point(){
  8. x = 0;
  9. y = 0;
  10. }
  11. point(int x1,int y1){
  12. x=x1;
  13. y=y1;
  14. }
  15. bool isequal(point&a, point&b);
  16. }my_point;
  17.  
  18. bool point::isequal(point&a, point&b){
  19. return a.x == b.x and a.y == b.y;
  20. }
  21.  
  22. int main() {
  23. int counter = 0, x = 0, y = 0, res = 0;
  24. char c;
  25. bool stop = false;
  26. vector<point> coor(1);
  27. while(cin >> c and stop == false){
  28. if(c == 'L'){
  29. counter = (counter + 1)%4;
  30. }
  31. else if(c == 'R'){
  32. counter = ((counter - 1) + 4)%4;
  33. }
  34. else{
  35. if(counter == 0){
  36. y++;
  37. }
  38. else if(counter == 1){
  39. x--;
  40. }
  41. else if(counter == 2){
  42. y--;
  43. }
  44. else{
  45. x++;
  46. }
  47. res++;
  48. point a(x,y);
  49. for(int j = 1; j < coor.size();j++){
  50. if(my_point.isequal(coor[j],a)){
  51. stop = true;
  52. }
  53. }
  54. coor.push_back(a);
  55. }
  56. }
  57. cout << ((stop == true) ? res : -1);
  58. return 0;
  59. }
Success #stdin #stdout 0s 15240KB
stdin
SRLSRLSLRSLRLSLSLSLSSSLRLSSLRSLRSRSRSRSLRLSRLLLLLSRLSRL
stdout
7