fork download
  1. import java.util.*;
  2. import java.lang.*;
  3. import java.math.*;
  4. import java.io.*;
  5.  
  6. import static java.lang.Math.*;
  7. import static java.util.Arrays.*;
  8.  
  9. public class B{
  10. Scanner sc=new Scanner(System.in);
  11.  
  12. int INF=1<<28;
  13. double EPS=1e-9;
  14.  
  15. int n, m;
  16. String[] map;
  17.  
  18. void run(){
  19. n=sc.nextInt();
  20. m=sc.nextInt();
  21. map=new String[n];
  22. for(int j=0; j<n; j++){
  23. map[j]=sc.next();
  24. }
  25. int x=0;
  26. int step=0;
  27. int last=0;
  28. for(int j=0; j<n; j++){
  29.  
  30. int right;
  31. for(right=m-1; right>=0; right--){
  32. if(map[j].charAt(right)=='W'){
  33. break;
  34. }
  35. }
  36. int left;
  37. for(left=0; left<m; left++){
  38. if(map[j].charAt(left)=='W'){
  39. break;
  40. }
  41. }
  42.  
  43. if(j%2==0){
  44. if(left<m&&right>=0){
  45. step+=abs(x-left)+abs(left-right);
  46. x=right;
  47. last=j;
  48. }
  49. }else{
  50. if(left<m&&right>=0){
  51. step+=abs(x-right)+abs(right-left);
  52. x=left;
  53. last=j;
  54. }
  55. }
  56. }
  57. step+=last;
  58. println(step+"");
  59. }
  60.  
  61. void println(String s){
  62. System.out.println(s);
  63. }
  64.  
  65. void print(String s){
  66. System.out.print(s);
  67. }
  68.  
  69. void debug(Object... os){
  70. System.err.println(Arrays.deepToString(os));
  71. }
  72.  
  73. public static void main(String[] args){
  74. new B().run();
  75. }
  76. }
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty