fork download
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. class Main
  5. {
  6. public static void main (String[] args) throws IOException
  7. {
  8. String readSTR = br.readLine();
  9. int set = Integer.parseInt(readSTR);
  10.  
  11. for(int t = 0; t < set; t++)
  12. {
  13. readSTR = br.readLine();
  14. int N = Integer.parseInt(readSTR);
  15. readSTR = br.readLine();
  16. char[] NY = readSTR.toCharArray();
  17.  
  18. //本棚データ格納
  19. ArrayList<String> a = new ArrayList<>();
  20. ArrayList<String> b = new ArrayList<>();
  21. int NYF = 0;
  22. for(int i = 0; i < NY.length; i++)
  23. {
  24. if(NYF == 8)
  25. {
  26. NYF = 0;
  27. }
  28.  
  29. if(NYF < 4 )
  30. {
  31. a.add(String.valueOf(NY[i]));
  32. }else{
  33. b.add(String.valueOf(NY[i]));
  34. }
  35. NYF++;
  36. }
  37.  
  38. boolean[][] map = CreateMap(a , b);
  39. for(int z =0;z<map.length;z++)
  40. {
  41. System.out.println(map[z][0] +" "+map[z][1]);
  42.  
  43. }
  44. //"Y"の座標格納
  45. Moving m = new Moving();
  46. m.setEnd(map.length - 1);
  47. for(int i = 0; i < map.length; i++)
  48. {
  49. if(map[i][0] == true)
  50. {
  51. m.roots.add(Integer.toString(i) + "0");
  52. }
  53. if(map[i][1] == true)
  54. {
  55. m.roots.add(Integer.toString(i) + "1");
  56. }
  57. }
  58. m.CountSteps();
  59. System.out.println(m.steps);
  60.  
  61. }
  62.  
  63.  
  64.  
  65. System.out.println();
  66. }
  67.  
  68. public static boolean NYJudgment(String s1,String s2)
  69. {
  70. boolean f;
  71. if(s1.equals("Y") || s2.equals("Y"))
  72. {
  73. f = true;
  74. }else{
  75. f = false;
  76. }
  77. return f;
  78. }
  79.  
  80. public static boolean[][] CreateMap(ArrayList<String> a, ArrayList<String> b)
  81. {
  82. boolean[][] map = new boolean[a.size() / 2 + 1][2];
  83. map[0][0] = NYJudgment(a.get(0),"N");
  84. map[0][1] = NYJudgment(b.get(0),"N");
  85. map[map.length - 1][0] = NYJudgment(a.get(a.size() - 1),"N");
  86. map[map.length - 1][1] = NYJudgment(b.get(b.size() - 1),"N");
  87.  
  88. for(int i = 1; i <= map.length - 2; i++)
  89. {
  90. map[i][0] = NYJudgment(a.get(i*2), a.get(i*2-1));
  91. map[i][1] = NYJudgment(b.get(i*2), b.get(i*2-1));
  92. }
  93. return map;
  94. }
  95.  
  96.  
  97.  
  98.  
  99. }
  100.  
  101. class Moving
  102. {
  103. int[] location = new int[2];
  104. //location[0] = 0;
  105. //location[1] = 0;
  106. int steps = 0;
  107. int end;
  108. ArrayList<String> roots = new ArrayList<>();
  109.  
  110. public void setEnd(int end)
  111. {
  112. this.end = end;
  113. }
  114.  
  115. public void CountSteps()
  116. {
  117. for(String s: roots)
  118. {
  119. String[] str = s.split("");
  120. int x = Integer.parseInt(str[0]);
  121. int y = Integer.parseInt(str[1]);
  122. if(location[0] != x)
  123. {
  124. moveRight(x);
  125. }
  126. moveUpDown(y);
  127. }
  128.  
  129. }
  130.  
  131. public void moveUpDown(int y)
  132. {
  133. if(location[1] == 0)
  134. {
  135. if(y == 0)
  136. {
  137. steps += 1;
  138. location[1] = 1;
  139. }else if(y == 1){
  140. steps += 2;
  141. location[1] = 2;
  142. }
  143.  
  144. }
  145.  
  146. if(location[1] == 1)
  147. {
  148. if(y == 0)
  149. {
  150. location[1] = 0;
  151. }else if(y == 1)
  152. {
  153. location[1] = 2;
  154. }
  155. steps += 1;
  156. }
  157.  
  158. if(location[1] == 2)
  159. {
  160. if(y == 1)
  161. {
  162. steps +=1;
  163. location[1] = 1;
  164. }else if( y == 0){
  165. steps += 2;
  166. location[1] = 0;
  167. }
  168.  
  169. }
  170. }
  171.  
  172. public void moveRight(int x)
  173. {
  174. for(int i = location[0]; i < x; i++)
  175. {
  176. steps++;
  177. location[0]++;
  178. }
  179. }
  180.  
  181. public void moveEnd()
  182. {
  183. if(location[0] != end)
  184. {
  185. moveRight(end);
  186. }
  187. if(location[1] != 0)
  188. {
  189. moveUpDown(0);
  190. }
  191. }
  192. }
Success #stdin #stdout 0.04s 4575232KB
stdin
2
2
YNNNNYYY
4
NYNNYYNNNYNYYNNN
stdout
true false
false true
false true
9
false true
true true
false true
true false
true false
16