-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathred_knight.java
More file actions
150 lines (136 loc) · 4.17 KB
/
Copy pathred_knight.java
File metadata and controls
150 lines (136 loc) · 4.17 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
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static class pos
{
int x;
int y;
String c;
pos(int x,int y,String c)
{
this.x = x;
this.y =y;
this.c = c;
}
}
static boolean check(int i_start, int j_start, int i_end, int j_end,int r,int c)
{
if(r<i_start || c<j_start ||r>i_end||c>j_end)
return true;
else
return false;
}
static ArrayList<pos> populate(int i_start,int j_start,int i_end,int j_end)
{
ArrayList<pos> tem = new ArrayList<pos>();
if(check(i_start,j_start,i_end,j_end,i_start-2,j_start-1))
{
pos p = new pos(i_start-2,j_start-1,"UL");
tem.add(p);
}
if(check(i_start,j_start,i_end,j_end,i_start-2,j_start+1))
{
pos p = new pos(i_start-2,j_start+1,"UR");
tem.add(p);
}
if(check(i_start,j_start,i_end,j_end,i_start+2,j_start-1))
{
pos p = new pos(i_start+2,j_start+1,"LR");
tem.add(p);
}
if(check(i_start,j_start,i_end,j_end,i_start+2,j_start+1))
{
pos p = new pos(i_start+2,j_start-1,"LL");
tem.add(p);
}
if(check(i_start,j_start,i_end,j_end,i_start,j_start-2))
{
pos p = new pos(i_start,j_start-2,"L");
tem.add(p);
}
if(check(i_start,j_start,i_end,j_end,i_start,j_start+2))
{
pos p = new pos(i_start,j_start+2,"R");
tem.add(p);
}
return tem;
}
static boolean checkpathrepetition(ArrayList<pos> next , pos p)
{
for(int i=0;i<next.size();i++)
{
if(p.x == next.get(i).x && p.y == next.get(i).y)
return false;
}
return true;
}
static void printShortestPath(int n, int i_start, int j_start, int i_end, int j_end) {
// Print the distance along with the sequence of moves.
Queue<ArrayList<pos>> q = new LinkedList<ArrayList<pos>>();
pos p = new pos(i_start,j_start,"S");
ArrayList<pos> tem= new ArrayList<pos>();
tem.add(p);
ArrayList<ArrayList<pos>> result = new ArrayList<ArrayList<pos>>();
q.add(tem);
int min =10000,flg=0;
int itr=0;
while(!q.isEmpty())
{
if(itr>50)
break;
ArrayList<pos> path = q.remove();
//System.out.println(path);
pos cur = path.get(path.size()-1);
ArrayList<pos> temp = populate(cur.x,cur.y,i_end,j_end);
if(temp.size()==0)
continue;
for(int i=0;i<temp.size();i++)
{
ArrayList<pos> next = new ArrayList<pos>(path);
if(!checkpathrepetition(next,temp.get(i)))
continue;
next.add(temp.get(i));
if(flg==1 && next.size()>min)
continue;
if(temp.get(i).x == i_end && temp.get(i).y==j_end)
{
if(min>next.size())
{
flg=1;
min = next.size();
result.add(next);
}
}
else
{
q.add(next);
}
}
++itr;
}
if(result.size()==0)
System.out.println("Impossible");
else
{
ArrayList<pos> prin = result.get(0);
System.out.println(prin.size()-1);
for(int i=1;i<prin.size();i++)
{
System.out.print(prin.get(i).c+" ");
}
}
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int i_start = in.nextInt();
int j_start = in.nextInt();
int i_end = in.nextInt();
int j_end = in.nextInt();
printShortestPath(n, i_start, j_start, i_end, j_end);
in.close();
}
}