-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLab1Sub1.java
More file actions
51 lines (41 loc) · 1.3 KB
/
Lab1Sub1.java
File metadata and controls
51 lines (41 loc) · 1.3 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
import java.util.*;
public class Lab1Sub1 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String state = in.nextLine().trim();
String action = in.nextLine().trim().toUpperCase();
in.close();
int blank = state.indexOf('#');
int row = blank / 3;
int col = blank % 3;
int targetRow = row;
int targetCol = col;
switch (action) {
case "UP":
targetRow--;
break;
case "DOWN":
targetRow++;
break;
case "LEFT":
targetCol--;
break;
case "RIGHT":
targetCol++;
break;
default:
System.out.println(state);
return;
}
// invalid move → return original state
if (targetRow < 0 || targetRow > 2 || targetCol < 0 || targetCol > 2) {
System.out.println(state);
return;
}
int targetIndex = targetRow * 3 + targetCol;
char[] tiles = state.toCharArray();
tiles[blank] = tiles[targetIndex];
tiles[targetIndex] = '#';
System.out.println(new String(tiles));
}
}