Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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.
- It is guaranteed that only one ‘X’ is present in each of the layer.
- Example 1
- Input
- 5
- O I M U R
- J V U X A
- X W T S R
- K Z F H D
- Q W K V M
- Output
- -2 2
- X J O I M
- K X S H U
- Q U T F R
- W V W Z A
- K V M D R
- Example 2
- Input
- 7
- L E A R K X G
- N Z N Z Q B S
- O P E B M S I
- A Z R T X M U
- A W S S O K U
- A X B A G M O
- D F Q A C U U
- Output
- 5 -4 3
- X G S I U U O
- K X W Z P Z U
- R B X O S N U
- A A M T S Z C
- E G B E R Q A
- L M K M S B Q
- N O A A A D F
- My solution:
- import java.io.*;
- import java.util.*;
- public class Solution {
- public static void main(String[] args)throws IOException {
- /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
- BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
- BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(System.out));
- int n=Integer.parseInt(br.readLine());
- char m[][]=new char[n][n];
- for(int i=0;i<n;i++)
- { String s[]=br.readLine().split(" ");
- for(int j=0;j<n;j++)
- m[i][j]=s[j].charAt(0);
- }
- for(int k=0;k<(n/2);k++)
- { int s1=0,r=k,c=k,f=0,i=0;
- int te=2*(n-2*k)+2*(n-2*k-2);//total elements
- char temp[]=new char[te];
- for(;c<(n-k);c++)
- { temp[i]=m[k][c];
- i++;
- }
- for(r=k+1;r<(n-k);r++)
- { temp[i]=m[r][c-1];
- i++;
- }
- for(c=c-2;c>k;c--)
- { temp[i]=m[r-1][c];
- i++;
- }
- for(r=r-1;r>k;r--)
- { temp[i]=m[r][k];
- i++;
- }
- r=k;c=k;
- for(;c<(n-k);c++)
- { if(m[k][c]=='X')
- { f=1;
- break;
- }
- s1++;
- }
- for(r=k+1;r<(n-k)&&f==0;r++)
- { if(m[r][c-1]=='X')
- { f=1;
- break;
- }
- s1++;
- }
- for(c=c-2;c>k && f==0;c--)
- { if(m[r-1][c]=='X')
- { f=1;
- break;
- }
- s1++;
- }
- for(r=r-1;r>k && f==0;r--)
- { if(m[r][k]=='X')
- { f=1;
- break;
- }
- s1++;
- }
- if(s1<=(te/2))
- bw.write(s1+" ");
- else
- bw.write("-"+(te-s1)+" ");
- char temp2[]=new char[te];
- for(i=0;i<te;i++)
- { int t=(i-s1+te)%te;
- temp2[t]=temp[i];
- }
- r=k;c=k;i=0;
- for(;c<(n-k);c++)
- { m[r][c]=temp2[i];
- i++;
- }
- for(r=k+1;r<(n-k);r++)
- { m[r][c-1]=temp2[i];
- i++;
- }
- for(c=c-2;c>k;c--)
- { m[r-1][c]=temp2[i];
- i++;
- }
- for(r=r-1;r>k;r--)
- { m[r][k]=temp2[i];
- i++;
- }
- }
- bw.write("\n");
- for(int i=0;i<n;i++)
- { for(int j=0;j<n;j++)
- bw.write(m[i][j]+" ");
- bw.write("\n");
- }
- bw.flush();
- }
- }
Add Comment
Please, Sign In to add comment