-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSecret message encrypting.txt
167 lines (109 loc) · 4.63 KB
/
Secret message encrypting.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import java.io.*;
import java.util.*;
public class Solution{
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int m=sc.nextInt();
int row_width=n-1;
int col_width=m-1;
int starting_row=0;
int starting_column=0;
String enc[][]=new String[n][m];
for(int i=0;i<n;i++){
String tempo=sc.next();
for(int j=0;j<m;j++){
enc[i][j]=Character.toString(tempo.charAt(j));
// System.out.println(enc[i][j]);
}
}
String message="";
// System.out.println(message.length());
while((starting_row <= row_width) && (starting_column <= col_width)){
if(message.length() == 0){
message = traverseUp(enc,row_width,starting_column,starting_row);
// System.out.println(message);
}else{
message += traverseUp(enc,row_width,starting_column,starting_row);
}
// System.out.println(message + "UP");
starting_column++;
if(starting_column <= col_width){
message += traverseRight(enc,starting_row,starting_column,col_width);
starting_row++;
}
// System.out.println(message + " Right");
if((starting_row <= row_width) && (starting_column <=col_width)){
message += traverseDown(enc,starting_row,col_width,row_width);
col_width--;
}
// System.out.println(message + "Down");
if(starting_column <= col_width){
message += traverseLeft(enc,row_width,col_width,starting_column);
row_width--;
}
// System.out.println(message);
}
// System.out.println(message);
String[] words=message.split("#");
ArrayList<String> wos=new ArrayList<String>();
for(String wo:words){
if(wo.length() > 0){
wos.add(wo);
}
}
System.out.println(wos.size());
}
public static String traverseUp(String encrypted[][],int a,int b,int first_row){
String mess="";
for(int x=a,y=b;x>=first_row;x--){
if(mess.length() == 0){
mess=encrypted[x][y];
// System.out.println(encrypted[x][y] + " Up ");
}else{
mess=mess+encrypted[x][y];
// System.out.println(encrypted[x][y] + " Up ");
}
}
return mess;
}
public static String traverseDown(String encrypted[][],int a,int b,int last_row){
String m="";
for(int x=a,y=b;x<=last_row;x++){
if(m.length() == 0){
m=encrypted[x][y];
// System.out.println(encrypted[x][y] + " Down ");
}else{
m=m+encrypted[x][y];
// System.out.println(encrypted[x][y] + " Down ");
}
}
return m;
}
public static String traverseLeft(String encrypted[][],int a,int b,int first_col){
String me="";
for(int x=a,y=b;y>=first_col;y--){
if(me.length() == 0){
me=encrypted[x][y];
// System.out.println(encrypted[x][y] + " Left ");
}else{
me=me+encrypted[x][y];
// System.out.println(encrypted[x][y] + " Left ");
}
}
return me;
}
public static String traverseRight(String encrypted[][],int a,int b,int last_col){
String te="";
for(int x=a,y=b;y<=last_col;y++){
if(te.length() == 0){
te=encrypted[x][y];
// System.out.println(encrypted[x][y]+"Right");
}else{
te=te+encrypted[x][y];
// System.out.println(encrypted[x][y] + "Right");
}
}
return te;
}
}