Repository navigation
Expand file tree
/
Copy pathSofaState.java
More file actions
155 lines (150 loc) · 4.77 KB
/
Copy pathSofaState.java
File metadata and controls
155 lines (150 loc) · 4.77 KB
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
import java.util.*;
class sofa{
int fsr,fsc,ssr,ssc;
char dir;
int moves;
public sofa(int fsr,int fsc,int ssr,int ssc,char d,int m) {
this.fsr = fsr;this.fsc = fsc;
this.ssr = ssr;this.ssc = ssc;
this.dir = d;this.moves = m;
}
}
public class SofaState{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int R = sc.nextInt();int C = sc.nextInt();
Queue<sofa> q = new LinkedList<>();
char grid[][] = new char[R][C];
int fsr,fsc,ssr,ssc,sofacount =0;
fsc=fsr=ssr=ssc = -1;
for(int row=0;row<R;row++) {
for(int col =0;col<C;col++) {
char ch = sc.next().charAt(0);
grid[row][col] = ch;
if(ch == 's') {
sofacount++;
if(sofacount == 1)
fsr = row;
fsc = col;
}
else {
ssr = row;
ssc = col;
sofa s = new sofa(fsr,fsc,ssr,ssc,(fsr == ssr)?'H':'V',0);
q.add(s);
}
}
}
Set<String> vis = new HashSet<>();
while(!q.isEmpty()) {
sofa s = q.poll();
if(grid[s.fsr][s.fsc] == 'S' && grid[s.ssr][s.ssc] == 'S') {
System.out.println(s.moves);return;
}
if(s.dir == 'H') {
//check drag right
if(s.ssc<C-1 && grid[s.ssr][s.ssc+1] != 'H') {
if(canAdd(s.ssr,s.ssc,s.ssr,s.ssc+1,vis)) {
q.add(new sofa(s.ssr,s.ssc,s.ssr,ssc+1,'H',s.moves+1));
}
}
//check drag left
if(s.fsc>0 && grid[s.fsr][s.fsc-1] != 'H') {
if(canAdd(s.fsr,s.fsc-1,s.fsr,s.fsc,vis)) {
q.add(new sofa(s.fsr,s.fsc-1,s.fsr,s.fsc,'H',s.moves+1));
}
}
//check drag top
if(s.fsr>0 && grid[s.fsr-1][s.fsc] !='H'&& grid[s.ssr-1][s.ssc] != 'H') {
if(canAdd(s.fsr-1,s.fsc,s.fsr-1,s.ssc,vis)) {
q.add(new sofa(s.fsr-1,s.fsc,s.fsr-1,s.ssc,'H',s.moves+1));
}
}
//check drag to bottom
if(s.fsc<R-1 && grid[s.fsr+1][s.fsc] !='H' && grid[s.ssc+1][s.ssc] != 'H') {
if(canAdd(s.fsr+1,s.fsc,s.ssr+1,s.ssc,vis)) {
q.add(new sofa(s.fsr+1,s.fsc,s.ssr+1,s.ssc,'H',s.moves+1));
}
}
//rotation
if(s.fsr>0 && grid[s.fsr-1][s.fsc] != 'H' && grid[s.ssr-1][s.ssc] != 'H') {
//First seat is going to be on the top of second seat clockwise
if(canAdd(s.fsr-1,s.fsc,s.ssr-1,s.ssc,vis)) {
q.add(new sofa(s.ssr-1,s.ssc,s.ssr,s.ssc,'V',s.moves+1));
}
//second seat is going to be on the top of first seat anti clockwise
if(canAdd(s.fsr,s.fsc,s.fsr-1,s.fsc,vis)) {
q.add(new sofa(s.fsr,s.fsc,s.fsr-1,s.fsc,'V',s.moves+1));
}
}
if(s.fsr<R-1 && grid[s.fsr+1][s.fsc] != 'H' && grid[s.ssr+1][s.ssc] != 'H') {
//First seat is going to be on the BOTTOM of second seat Anti clockwise
if(canAdd(s.ssr+1,s.ssc,s.ssr,s.ssc,vis)) {
q.add(new sofa(s.ssr+1,s.ssc,s.ssr,s.ssc,'V',s.moves+1));
}
//second seat is going to be on the bottom of first seat clockwise
if(canAdd(s.fsr,s.fsc,s.fsr+1,s.fsc,vis)) {
q.add(new sofa(s.fsr,s.fsc,s.fsr+1,s.fsc,'V',s.moves+1));
}
}
}
if(s.dir == 'V') {
//check drag top
if(s.fsr>0 && grid[s.ssr-1][s.fsc] != 'H') {
if(canAdd(s.fsr-1,s.fsc,s.fsr,s.fsc,vis)){
q.add(new sofa(s.fsr-1,s.fsc,s.fsr,s.fsc,'V',s.moves+1));
}
}
//check drag bottom
if(s.fsr <R-1 && grid[s.fsr+1][s.fsc] != 'H') {
if(canAdd(s.fsr+1,fsc,fsr,fsc,vis)) {
q.add(new sofa(s.fsr+1,s.fsc,s.fsr,s.fsc,'V',s.moves+1));
}
}
//check for drag left
if(s.fsc >0 && grid[s.fsr][s.fsc-1] != 'H' && grid[s.ssr][s.ssc-1] != 'H') {
if(canAdd(s.fsr,s.fsc-1,s.ssr,s.ssc-1,vis)) {
q.add(new sofa(s.fsr,s.fsc-1,s.ssr,s.ssc-1,'V',s.moves+1));
}
}
//check for drag right
if(s.fsc<C-1 && grid[s.fsr][s.ssc+1] != 'H' && grid[s.ssr][s.ssc+1] != 'H') {
if(canAdd(s.fsr,s.fsc+1,s.ssr,s.ssc+1,vis)){
q.add(new sofa(s.fsr,s.fsc+1,s.ssr,s.ssc+1,'V',s.moves+1));
}
}
//Rotation
if(s.fsc>0 && s.fsc<C && grid[s.fsr][s.fsc-1] != 'H' && grid[s.fsr][s.fsc+1] != 'H') {
if(canAdd(s.fsr,s.fsc,s.fsr,s.fsc-1,vis)) {
q.add(new sofa(s.fsr,s.fsc,s.fsr,s.fsc-1,'H',s.moves+1));
}
if(canAdd(s.fsr,s.fsc,s.fsr,s.fsc+1,vis)) {
q.add(new sofa(s.fsr,s.fsc,s.fsr,s.fsc+1,'H',s.moves+1));
}
}
if(s.ssc>0 && s.ssc<C && grid[s.ssr][s.ssc-1] != 'H' && grid[s.ssr][s.ssc+1] != 'H') {
if(canAdd(s.ssr,s.ssc-1,s.ssc,s.ssc,vis)) {
q.add(new sofa(s.ssr,s.ssc-1,s.ssr,s.ssc,'H',s.moves+1));
}
if(canAdd(s.ssr,s.ssc+1,s.ssr,s.ssc,vis)) {
q.add(new sofa(s.ssr,s.ssc+1,s.ssr,s.ssc,'H',s.moves+1));
}
}
}
}
System.out.println("Impossible");
sc.close();
}
static final String DELIM = "-";
private static boolean canAdd(int fsr,int fsc,int ssr,int ssc,Set<String> vis) {
StringBuilder sb = new StringBuilder();
sb.append(fsr).append(DELIM).append(fsc).append(DELIM);
sb.append(ssr).append(DELIM).append(ssc);
String key = sb.toString();
if(vis.contains(key)) {
return false;
}
vis.add(key);
return true;
}
}