meghana180799

Untitled

Nov 9th, 2019
408
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.00 KB | None | 0 0
  1. There is a maze of N x N size, each element will have an alphabet from ‘A’ to ‘Z’. (for ease of calculation N is odd). ‘T’ represents Treasure, X represents the passage. The treasure is placed at the center of the maze (e.g. for a 5 x 5 maze, treasure is at 3rd row & 3rd Column). Only way to reach the treasure is from the top left corner of the maze & the entry is allowed to next layer only if top left corner has passage ( ‘X’ ). Your aim is to find the minimum number of steps at each layer of the maze to be rotated counter clockwise ( +ve) /clockwise ( -ve) so that all ‘X’ come to the left corner & treasure can be reached.
  2. It is guaranteed that only one ‘X’ is present in each of the layer.
  3. Example 1
  4. Input
  5. 5
  6. O I M U R
  7. J V U X A
  8. X W T S R
  9. K Z F H D
  10. Q W K V M
  11. Output
  12. -2 2
  13. X J O I M
  14. K X S H U
  15. Q U T F R
  16. W V W Z A
  17. K V M D R
  18. Example 2
  19. Input
  20. 7
  21. L E A R K X G
  22. N Z N Z Q B S
  23. O P E B M S I
  24. A Z R T X M U
  25. A W S S O K U
  26. A X B A G M O
  27. D F Q A C U U
  28. Output
  29. 5 -4 3
  30. X G S I U U O
  31. K X W Z P Z U
  32. R B X O S N U
  33. A A M T S Z C
  34. E G B E R Q A
  35. L M K M S B Q
  36. N O A A A D F
  37.  
  38.  
  39.  
  40. My solution:
  41.  
  42.  
  43. import java.io.*;
  44. import java.util.*;
  45.  
  46. public class Solution {
  47.  
  48. public static void main(String[] args)throws IOException {
  49. /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
  50. BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
  51. BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(System.out));
  52. int n=Integer.parseInt(br.readLine());
  53. char m[][]=new char[n][n];
  54. for(int i=0;i<n;i++)
  55. { String s[]=br.readLine().split(" ");
  56. for(int j=0;j<n;j++)
  57. m[i][j]=s[j].charAt(0);
  58. }
  59. for(int k=0;k<(n/2);k++)
  60. { int s1=0,r=k,c=k,f=0,i=0;
  61. int te=2*(n-2*k)+2*(n-2*k-2);//total elements
  62. char temp[]=new char[te];
  63. for(;c<(n-k);c++)
  64. { temp[i]=m[k][c];
  65. i++;
  66. }
  67. for(r=k+1;r<(n-k);r++)
  68. { temp[i]=m[r][c-1];
  69. i++;
  70. }
  71. for(c=c-2;c>k;c--)
  72. { temp[i]=m[r-1][c];
  73. i++;
  74. }
  75. for(r=r-1;r>k;r--)
  76. { temp[i]=m[r][k];
  77. i++;
  78. }
  79. r=k;c=k;
  80. for(;c<(n-k);c++)
  81. { if(m[k][c]=='X')
  82. { f=1;
  83. break;
  84. }
  85. s1++;
  86. }
  87. for(r=k+1;r<(n-k)&&f==0;r++)
  88. { if(m[r][c-1]=='X')
  89. { f=1;
  90. break;
  91. }
  92. s1++;
  93. }
  94. for(c=c-2;c>k && f==0;c--)
  95. { if(m[r-1][c]=='X')
  96. { f=1;
  97. break;
  98. }
  99. s1++;
  100. }
  101. for(r=r-1;r>k && f==0;r--)
  102. { if(m[r][k]=='X')
  103. { f=1;
  104. break;
  105. }
  106. s1++;
  107. }
  108. if(s1<=(te/2))
  109. bw.write(s1+" ");
  110. else
  111. bw.write("-"+(te-s1)+" ");
  112. char temp2[]=new char[te];
  113. for(i=0;i<te;i++)
  114. { int t=(i-s1+te)%te;
  115. temp2[t]=temp[i];
  116. }
  117. r=k;c=k;i=0;
  118. for(;c<(n-k);c++)
  119. { m[r][c]=temp2[i];
  120. i++;
  121. }
  122. for(r=k+1;r<(n-k);r++)
  123. { m[r][c-1]=temp2[i];
  124. i++;
  125. }
  126. for(c=c-2;c>k;c--)
  127. { m[r-1][c]=temp2[i];
  128. i++;
  129. }
  130. for(r=r-1;r>k;r--)
  131. { m[r][k]=temp2[i];
  132. i++;
  133. }
  134. }
  135. bw.write("\n");
  136. for(int i=0;i<n;i++)
  137. { for(int j=0;j<n;j++)
  138. bw.write(m[i][j]+" ");
  139. bw.write("\n");
  140. }
  141. bw.flush();
  142. }
  143. }
Add Comment
Please, Sign In to add comment