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

9/22/2019 Computed Diff - Diff Checker

- 20
1 Removals + 51 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; 16 String path,action;
17 HashSet<String> visited;
17 Point(int x, int y, int a){ 18 Point(int x, int y, int a){
19 this.visited = new HashSet<>();
18 this.val = a; 20 this.val = a;
19 this.x = x; 21 this.x = x;
20 this.y = y; 22 this.y = y;
23 this.action = "E";
21 } 24 }
22 Point(int x, int y, int a, Point prev, int g, String action){ 25 Point(int x, int y, int a, Point prev, int g, String action){
26 this.visited = prev.visited;
27 this.visited.add(prev.x + " " + prev.y);
23 this.val = a; 28 this.val = a;
24 this.x = x; 29 this.x = x;
25 this.y = y; 30 this.y = y;
26 this.parent = prev; 31 this.parent = prev;
27 this.g = g; 32 this.g = g;
28 this.path = prev.path + action; 33 this.path = prev.path + action;
34 this.action = action;
29 } 35 }
30 36
31 String print(){ 37 String print(){
32 return this.x + " " + this.y; 38 return this.x + " " + this.y;
33 } 39 }
34 } 40 }
35 41
36 class WMap{ 42 class WMap{
37 ArrayList< ArrayList<Point> > grid = new ArrayList<>(); 43 ArrayList< ArrayList<Point> > grid = new ArrayList<>();
38 WMap(int m){ 44 WMap(int m){
39 for(int i = 0; i < m; i++){ 45 for(int i = 0; i < m; i++){
40 grid.add(new ArrayList<Point>()); 46 grid.add(new ArrayList<Point>());
41 } 47 }
42 } 48 }
43 49
44 50
45 } 51 }
46 52
47 class Agent{ 53 class Agent{
48 WMap wMap; 54 WMap wMap;
49 ArrayList<Point> fm; 55 ArrayList<Point> fm;
50 56 Boolean isGoal(int x, int y){

https://www.diffchecker.com/diff 1/4
9/22/2019 Computed Diff - Diff Checker
57 for(Point p : fm){
58 if(p.x == x && p.y == y) return true;
59 }
60 return false;
61 }
- 20 Removals + 51 Additions 62 Integer getCost(int val1, int val2){
63
64 if(val1 >= val2){
65 return (val1-val2) + 1;
66 }else{
67 return 2*(val2 - val1) + 1;
68 }
69 }
70
51 Integer getH(Point p){ 71 Integer getH(Point p){
52 int m = wMap.grid.size();
53 int n = wMap.grid.get(0).size();
54 int min = Integer.MAX_VALUE; 72 int min = Integer.MAX_VALUE;
55 int temp = 0; 73 int temp = 0;
56 for(int i = 0; i < fm.size(); i++){ 74 for(int i = 0; i < fm.size(); i++){
57 temp = Math.abs(fm.get(i).x - p.x) + Math.abs(fm.get(i).y - p.y); 75 temp = Math.abs(fm.get(i).x - p.x) + Math.abs(fm.get(i).y - p.y);
58 if( temp < min) min = temp; 76 if( temp < min) min = temp;
59 } 77 }
60 return min; 78 return min;
61 } 79 }
62 80
63 ArrayList<Point> nextPossibleStates(Point p){ 81 ArrayList<Point> nextPossibleStates(Point p){
64 int m = wMap.grid.size(); 82 int m = wMap.grid.size();
65 int n = wMap.grid.get(0).size(); 83 int n = wMap.grid.get(0).size();
66 ArrayList<Point> child = new ArrayList<>(); 84 ArrayList<Point> child = new ArrayList<>();
67 85
68 if( p.x > 0 && wMap.grid.get(p.x - 1).get(p.y).val != 0){ 86 if( p.x > 0){
69 87
70 child.add(new Point(p.x - 1 , p.y, wMap.grid.get(p.x - 1).get(p.y).val, p, p.g, "A")); 88 child.add(new Point(p.x - 1 , p.y, wMap.grid.get(p.x - 1).get(p.y).val, p, p.g, "A"));
71 89
72 } 90 }
73 if( p.y < (n-1) && wMap.grid.get(p.x).get(p.y+1).val != 0){ 91 if( p.y < (n-1)){
74
75 92
76 child.add(new Point(p.x , p.y + 1, wMap.grid.get(p.x).get(p.y + 1).val, p, p.g, "B")); 93 child.add(new Point(p.x , p.y + 1, wMap.grid.get(p.x).get(p.y + 1).val, p, p.g, "B"));
77 } 94 }
78 if( p.x < (m-1) && wMap.grid.get(p.x+1).get(p.y).val != 0){ 95 if( p.x < (m-1)){
79
80 96
81 child.add(new Point(p.x + 1, p.y, wMap.grid.get(p.x+1).get(p.y).val, p, p.g, "C")); 97 child.add(new Point(p.x + 1, p.y, wMap.grid.get(p.x+1).get(p.y).val, p, p.g, "C"));
82 } 98 }
83 if( p.y > 0 && wMap.grid.get(p.x).get(p.y-1).val != 0){ 99 if( p.y > 0 ){
84 100
85 child.add(new Point(p.x, p.y-1, wMap.grid.get(p.x).get(p.y -1).val, p, p.g, "D")); 101 child.add(new Point(p.x, p.y-1, wMap.grid.get(p.x).get(p.y -1).val, p, p.g, "D"));
86 } 102 }
87 return child; 103 return child;
88 } 104 }
89 105
90 106
91 } 107 }
92 class PointComparator implements Comparator<Point>{ 108 class PointComparator implements Comparator<Point>{
93 109
94 @Override 110 @Override
95 public int compare(Point o1, Point o2) { 111 public int compare(Point o1, Point o2) {
96 112
97 if(o1.f == o2.f) return o1.path.compareTo(o2.path); 113 if(o1.f == o2.f) return o1.path.compareTo(o2.path);

https://www.diffchecker.com/diff 2/4
9/22/2019 Computed Diff - Diff Checker
98 else return o1.f - o2.f; 114 else return o1.f - o2.f;
99 } 115 }
100 } 116 }
101 117
102 class TestClass { 118 class TestClass {
- 103
20 Removals + 51
static class Additions
quickRead 119 static class quickRead
104 { 120 {
105 BufferedReader br; 121 BufferedReader br;
106 StringTokenizer st; 122 StringTokenizer st;
107 123
108 public quickRead() 124 public quickRead()
109 { 125 {
110 br = new BufferedReader(new 126 br = new BufferedReader(new
111 InputStreamReader(System.in)); 127 InputStreamReader(System.in));
112 } 128 }
113 129
114 public String next() 130 public String next()
115 { 131 {
116 while (st == null || !st.hasMoreElements()) 132 while (st == null || !st.hasMoreElements())
117 { 133 {
118 try 134 try
119 { 135 {
120 st = new StringTokenizer(br.readLine()); 136 st = new StringTokenizer(br.readLine());
121 } 137 }
122 catch (IOException e) 138 catch (IOException e)
123 { 139 {
124 e.printStackTrace(); 140 e.printStackTrace();
125 } 141 }
126 } 142 }
127 return st.nextToken(); 143 return st.nextToken();
128 } 144 }
129 145
130 int nextInt() 146 int nextInt()
131 { 147 {
132 return Integer.parseInt(next()); 148 return Integer.parseInt(next());
133 } 149 }
134 } 150 }
135 public static void main(String args[] ) throws Exception { 151 public static void main(String args[] ) throws Exception {
136 quickRead s = new quickRead(); 152 quickRead s = new quickRead();
137 int t,m,n; 153 int t,m,n;
138 t = s.nextInt(); 154 t = s.nextInt();
139 while( t-- > 0) { 155 while( t-- > 0) {
140 m = s.nextInt(); 156 m = s.nextInt();
141 n = s.nextInt(); 157 n = s.nextInt();
142 WMap wmap = new WMap(m); 158 WMap wmap = new WMap(m);
143 ArrayList<Point> foodMap = new ArrayList<>(); 159 ArrayList<Point> foodMap = new ArrayList<>();
144 int temp; 160 int temp;
145 for(int i = 0 ; i < m; i++){ 161 for(int i = 0 ; i < m; i++){
146 for(int j = 0; j < n; j++){ 162 for(int j = 0; j < n; j++){
147 temp = s.nextInt(); 163 temp = s.nextInt();
148 wmap.grid.get(i).add(new Point(i, j, temp)); 164 if(temp < 0) foodMap.add(new Point(i,j, Math.abs(temp) ));
149 if(temp == 2) foodMap.add(new Point(i,j,temp)); 165 wmap.grid.get(i).add(new Point(i, j, Math.abs(temp) ));
166
150 } 167 }
151 } 168 }
152 int sx = s.nextInt(); 169 int sx = s.nextInt();
153 int sy = s.nextInt(); 170 int sy = s.nextInt();
154 Agent a = new Agent(); 171 Agent a = new Agent();
155 a.wMap = wmap; 172 a.wMap = wmap;
156 a.fm = foodMap; 173 a.fm = foodMap;
157 PriorityQueue<Point> pq = new PriorityQueue<>(new PointComparator()); 174 PriorityQueue<Point> pq = new PriorityQueue<>(new PointComparator());

https://www.diffchecker.com/diff 3/4
9/22/2019 Computed Diff - Diff Checker
158 wmap.grid.get(sx).get(sy).parent = new Point(-1,-1,-2); 175 wmap.grid.get(sx).get(sy).parent = new Point(-1,-1,-2);
159 pq.add(wmap.grid.get(sx).get(sy)); 176 pq.add(wmap.grid.get(sx).get(sy));
160 pq.peek().g = 0; 177 pq.peek().g = 0;
161 pq.peek().path = ""; 178 pq.peek().path = "";
179
- 20 Removals + 51 Additions 180 Boolean flag = false;
162 HashSet<String> visited = new HashSet<>(); 181 HashSet<String> visited = new HashSet<>();
182
183
163 while(!pq.isEmpty()){ 184 while(!pq.isEmpty()){
185
164 Point curr = pq.poll(); 186 Point curr = pq.poll();
165 if(visited.contains(curr.print())){ 187 if(curr.visited.contains(curr.print())){
166 continue; 188 continue;
167 } 189 }
168 visited.add(curr.x + " " + curr.y); 190
169 191 if (a.isGoal(curr.x, curr.y)){
170 if (curr.val == 2){ 192 int cost = curr.g;
193 System.out.println(cost);
171 ArrayList<String> output = new ArrayList<>(); 194 ArrayList<String> output = new ArrayList<>();
195
172 while(curr.val != -2){ 196 while(curr.val != -2){
173 output.add(curr.x + " " + curr.y); 197 output.add(curr.x + " " + curr.y);
174 curr = curr.parent; 198 curr = curr.parent;
175 } 199 }
200 String path = "";
176 for(int k = output.size() - 1; k >= 0; k--){ 201 for(int k = output.size() - 1; k >= 0; k--){
177 System.out.println(output.get(k)); 202 System.out.println(output.get(k));
178 } 203 }
204 flag = true;
179 break; 205 break;
180 } 206 }
181 207
182 ArrayList<Point> children = a.nextPossibleStates(curr); 208 ArrayList<Point> children = a.nextPossibleStates(curr);
183 for(Point pc : children){ 209 for(Point pc : children){
184 if(visited.contains(pc.print())) continue; 210 if(curr.visited.contains(pc.print())) continue;
185 pc.f = curr.g + a.getH(pc); 211 pc.g = curr.g + a.getCost(curr.val, pc.val);
212 pc.f = pc.g + a.getH(pc);
186 pq.add(pc); 213 pq.add(pc);
187 } 214 }
188 215
189 216
190 217
191 218
192 } 219 }
220 if(!flag) System.out.println("NIL");
221
193 } 222 }
194 } 223 }
195 } 224 }
225

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

You might also like