Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

9/22/2019 Computed Diff - Diff Checker

- 01 Removals + 3 Additions
import java.lang.reflect.Array; 1 import java.lang.reflect.Array;
2 import java.util.ArrayList; 2 import java.util.ArrayList;
3 import java.util.Collections; 3 import java.util.Collections;
4 import java.util.Comparator; 4 import java.util.Comparator;
5 import java.util.Scanner; 5 import java.util.Scanner;
6 import java.lang.Math; 6 import java.lang.Math;
7 import java.util.*; 7 import java.util.*;
8 import java.io.BufferedReader; 8 import java.io.BufferedReader;
9 import java.io.IOException; 9 import java.io.IOException;
10 import java.io.InputStreamReader; 10 import java.io.InputStreamReader;
11 import java.util.StringTokenizer; 11 import java.util.StringTokenizer;
12 12
13 class Point{ 13 class Point{
14 int val,x,y,f,g; 14 int val,x,y,f,g;
15 Point parent; 15 Point parent;
16 String path,action; 16 String path,action;
17 Point(int x, int y, int a){ 17 Point(int x, int y, int a){
18 this.val = a; 18 this.val = a;
19 this.x = x; 19 this.x = x;
20 this.y = y; 20 this.y = y;
21 this.action = "E"; 21 this.action = "E";
22 } 22 }
23 Point(int x, int y, int a, Point prev, int g, String action){ 23 Point(int x, int y, int a, Point prev, int g, String action){
24 this.val = a; 24 this.val = a;
25 this.x = x; 25 this.x = x;
26 this.y = y; 26 this.y = y;
27 this.parent = prev; 27 this.parent = prev;
28 this.g = g; 28 this.g = g;
29 this.path = prev.path + action; 29 this.path = prev.path + action;
30 this.action = action; 30 this.action = action;
31 } 31 }
32 32
33 String print(){ 33 String print(){
34 return this.x + " " + this.y; 34 return this.x + " " + this.y;
35 } 35 }
36 } 36 }
37 37
38 class WMap{ 38 class WMap{
39 ArrayList< ArrayList<Point> > grid = new ArrayList<>(); 39 ArrayList< ArrayList<Point> > grid = new ArrayList<>();
40 WMap(int m){ 40 WMap(int m){
41 for(int i = 0; i < m; i++){ 41 for(int i = 0; i < m; i++){
42 grid.add(new ArrayList<Point>()); 42 grid.add(new ArrayList<Point>());
43 } 43 }
44 } 44 }
45 45
46 46
47 } 47 }
48 48
49 class Agent{ 49 class Agent{
50 WMap wMap; 50 WMap wMap;
51 ArrayList<Point> fm; 51 ArrayList<Point> fm;
52 52
53 Integer getH(Point p){ 53 Integer getH(Point p){
54 54
55 int min = Integer.MAX_VALUE; 55 int min = Integer.MAX_VALUE;
56 int temp = 0; 56 int temp = 0;

https://www.diffchecker.com/diff 1/4
9/22/2019 Computed Diff - Diff Checker
57 for(int i = 0; i < fm.size(); i++){ 57 for(int i = 0; i < fm.size(); i++){
58 temp = Math.abs(fm.get(i).x - p.x) + Math.abs(fm.get(i).y - p.y); 58 temp = Math.abs(fm.get(i).x - p.x) + Math.abs(fm.get(i).y - p.y);
59 if( temp < min) min = temp; 59 if( temp < min) min = temp;
60 } 60 }
61 return min; 61 return min;
- 62
0 Removals} + 3 Additions 62 }
63 63
64 ArrayList<Point> nextPossibleStates(Point p){ 64 ArrayList<Point> nextPossibleStates(Point p){
65 int m = wMap.grid.size(); 65 int m = wMap.grid.size();
66 int n = wMap.grid.get(0).size(); 66 int n = wMap.grid.get(0).size();
67 ArrayList<Point> child = new ArrayList<>(); 67 ArrayList<Point> child = new ArrayList<>();
68 68
69 if( p.x > 0 && wMap.grid.get(p.x - 1).get(p.y).val != 0){ 69 if( p.x > 0 && wMap.grid.get(p.x - 1).get(p.y).val != 0){
70 70
71 child.add(new Point(p.x - 1 , p.y, wMap.grid.get(p.x - 1).get(p.y).val, p, p.g, "A")); 71 child.add(new Point(p.x - 1 , p.y, wMap.grid.get(p.x - 1).get(p.y).val, p, p.g, "A"));
72 72
73 } 73 }
74 if( p.y < (n-1) && wMap.grid.get(p.x).get(p.y+1).val != 0){ 74 if( p.y < (n-1) && wMap.grid.get(p.x).get(p.y+1).val != 0){
75 75
76 76
77 child.add(new Point(p.x , p.y + 1, wMap.grid.get(p.x).get(p.y + 1).val, p, p.g, "B")); 77 child.add(new Point(p.x , p.y + 1, wMap.grid.get(p.x).get(p.y + 1).val, p, p.g, "B"));
78 } 78 }
79 if( p.x < (m-1) && wMap.grid.get(p.x+1).get(p.y).val != 0){ 79 if( p.x < (m-1) && wMap.grid.get(p.x+1).get(p.y).val != 0){
80 80
81 81
82 child.add(new Point(p.x + 1, p.y, wMap.grid.get(p.x+1).get(p.y).val, p, p.g, "C")); 82 child.add(new Point(p.x + 1, p.y, wMap.grid.get(p.x+1).get(p.y).val, p, p.g, "C"));
83 } 83 }
84 if( p.y > 0 && wMap.grid.get(p.x).get(p.y-1).val != 0){ 84 if( p.y > 0 && wMap.grid.get(p.x).get(p.y-1).val != 0){
85 85
86 child.add(new Point(p.x, p.y-1, wMap.grid.get(p.x).get(p.y -1).val, p, p.g, "D")); 86 child.add(new Point(p.x, p.y-1, wMap.grid.get(p.x).get(p.y -1).val, p, p.g, "D"));
87 } 87 }
88 return child; 88 return child;
89 } 89 }
90 90
91 91
92 } 92 }
93 class PointComparator implements Comparator<Point>{ 93 class PointComparator implements Comparator<Point>{
94 94
95 @Override 95 @Override
96 public int compare(Point o1, Point o2) { 96 public int compare(Point o1, Point o2) {
97 97
98 if(o1.f == o2.f){ 98 if(o1.f == o2.f){
99 return o1.path.compareTo(o2.path); 99 return o1.path.compareTo(o2.path);
100 }else return o1.f - o2.f; 100 }else return o1.f - o2.f;
101 } 101 }
102 } 102 }
103 103
104 class TestClass { 104 class TestClass {
105 static class quickRead 105 static class quickRead
106 { 106 {
107 BufferedReader br; 107 BufferedReader br;
108 StringTokenizer st; 108 StringTokenizer st;
109 109
110 public quickRead() 110 public quickRead()
111 { 111 {
112 br = new BufferedReader(new 112 br = new BufferedReader(new
113 InputStreamReader(System.in)); 113 InputStreamReader(System.in));
114 } 114 }
115 115
116 public String next() 116 public String next()
117 { 117 {

https://www.diffchecker.com/diff 2/4
9/22/2019 Computed Diff - Diff Checker
118 while (st == null || !st.hasMoreElements()) 118 while (st == null || !st.hasMoreElements())
119 { 119 {
120 try 120 try
121 { 121 {
122 st = new StringTokenizer(br.readLine()); 122 st = new StringTokenizer(br.readLine());
- 123
0 Removals + 3 Additions
} 123 }
124 catch (IOException e) 124 catch (IOException e)
125 { 125 {
126 e.printStackTrace(); 126 e.printStackTrace();
127 } 127 }
128 } 128 }
129 return st.nextToken(); 129 return st.nextToken();
130 } 130 }
131 131
132 int nextInt() 132 int nextInt()
133 { 133 {
134 return Integer.parseInt(next()); 134 return Integer.parseInt(next());
135 } 135 }
136 } 136 }
137 public static void main(String args[] ) throws Exception { 137 public static void main(String args[] ) throws Exception {
138 quickRead s = new quickRead(); 138 quickRead s = new quickRead();
139 int t,m,n; 139 int t,m,n;
140 t = s.nextInt(); 140 t = s.nextInt();
141 while( t-- > 0) { 141 while( t-- > 0) {
142 m = s.nextInt(); 142 m = s.nextInt();
143 n = s.nextInt(); 143 n = s.nextInt();
144 WMap wmap = new WMap(m); 144 WMap wmap = new WMap(m);
145 ArrayList<Point> foodMap = new ArrayList<>(); 145 ArrayList<Point> foodMap = new ArrayList<>();
146 int temp; 146 int temp;
147 for(int i = 0 ; i < m; i++){ 147 for(int i = 0 ; i < m; i++){
148 for(int j = 0; j < n; j++){ 148 for(int j = 0; j < n; j++){
149 temp = s.nextInt(); 149 temp = s.nextInt();
150 wmap.grid.get(i).add(new Point(i, j, temp)); 150 wmap.grid.get(i).add(new Point(i, j, temp));
151 if(temp == 2) foodMap.add(new Point(i,j,temp)); 151 if(temp == 2) foodMap.add(new Point(i,j,temp));
152 } 152 }
153 } 153 }
154 int sx = s.nextInt(); 154 int sx = s.nextInt();
155 int sy = s.nextInt(); 155 int sy = s.nextInt();
156 Agent a = new Agent(); 156 Agent a = new Agent();
157 a.wMap = wmap; 157 a.wMap = wmap;
158 a.fm = foodMap; 158 a.fm = foodMap;
159 PriorityQueue<Point> pq = new PriorityQueue<>(new PointComparator()); 159 PriorityQueue<Point> pq = new PriorityQueue<>(new PointComparator());
160 wmap.grid.get(sx).get(sy).parent = new Point(-1,-1,-2); 160 wmap.grid.get(sx).get(sy).parent = new Point(-1,-1,-2);
161 pq.add(wmap.grid.get(sx).get(sy)); 161 pq.add(wmap.grid.get(sx).get(sy));
162 pq.peek().g = 0; 162 pq.peek().g = 0;
163 pq.peek().path = ""; 163 pq.peek().path = "";
164 HashSet<String> visited = new HashSet<>(); 164 HashSet<String> visited = new HashSet<>();
165 Boolean flag = false;
165 while(!pq.isEmpty()){ 166 while(!pq.isEmpty()){
166 Point curr = pq.poll(); 167 Point curr = pq.poll();
167 if(visited.contains(curr.print())){ 168 if(visited.contains(curr.print())){
168 continue; 169 continue;
169 } 170 }
170 visited.add(curr.x + " " + curr.y); 171 visited.add(curr.x + " " + curr.y);
171 172
172 if (curr.val == 2){ 173 if (curr.val == 2){
173 ArrayList<String> output = new ArrayList<>(); 174 ArrayList<String> output = new ArrayList<>();
174 while(curr.val != -2){ 175 while(curr.val != -2){
175 output.add(curr.x + " " + curr.y); 176 output.add(curr.x + " " + curr.y);
176 curr = curr.parent; 177 curr = curr.parent;
177 } 178 }

https://www.diffchecker.com/diff 3/4
9/22/2019 Computed Diff - Diff Checker
178 for(int k = output.size() - 1; k >= 0; k--){ 179 for(int k = output.size() - 1; k >= 0; k--){
179 System.out.println(output.get(k)); 180 System.out.println(output.get(k));
180 } 181 }
182 flag = true;
181 break; 183 break;
- 182
0 Removals + 3 Additions
} 184 }
183 185
184 ArrayList<Point> children = a.nextPossibleStates(curr); 186 ArrayList<Point> children = a.nextPossibleStates(curr);
185 for(Point pc : children){ 187 for(Point pc : children){
186 if(visited.contains(pc.print())) continue; 188 if(visited.contains(pc.print())) continue;
187 pc.g = curr.g + 1; 189 pc.g = curr.g + 1;
188 pc.f = pc.g + a.getH(pc); 190 pc.f = pc.g + a.getH(pc);
189 191
190 pq.add(pc); 192 pq.add(pc);
191 } 193 }
192 194
193 195
194 196
195 197
196 } 198 }
199 if(!flag) System.out.println("NIL");
197 } 200 }
198 } 201 }
199 } 202 }
200 203

https://www.diffchecker.com/diff 4/4

You might also like