-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathblockcrusher.java
More file actions
104 lines (99 loc) · 2.63 KB
/
blockcrusher.java
File metadata and controls
104 lines (99 loc) · 2.63 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
import java.io.*;
import java.util.*;
public class BlockCrusher {
int dist[][];
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
while(true){
int rows=0, cols=0;
{
String[] ln = br.readLine().split(" ");
rows = Integer.parseInt(ln[0]);
cols = Integer.parseInt(ln[1]);
}
if(rows<1){
break;
}
dist = new int[rows][cols];
PriorityQueue<Point> queue = new PriorityQueue<Point>();
int[][] map = new int[rows][cols];
for(int i=0;i<map.length;i++){
String ln = br.readLine();
for(int j=0;j<map[0].length;j++){
map[i][j]=Integer.parseInt(""+ln.charAt(j));
if(i<1){
queue.add(new Point(i,j,map[i][j]));
}
}
}
Point end;
while((end=emptyQueue(queue,map))==null){}
while(end!=null){
map[end.x][end.y]=-1;
end = end.prev;
}
for(int i=0;i<map.length;i++){
for(int j=0;j<map[0].length;j++){
bw.write(map[i][j]>-1?Integer.toString(map[i][j]):" ");
}
bw.newLine();
bw.flush();
}
bw.newLine();
}
bw.close();
}
//WORKING ON THIS FUNCTION
public static Point emptyQueue(PriorityQueue<Point> queue, int[][] map){
Point p = queue.remove();
int x=p.x,y=p.y;
if(x+1>=map.length)
return p;
if(goTo(map,x+1,y-1)){
queue.add(new Point(x+1,y-1,map[x+1][y-1]+p.cost,p));
}
if(goTo(map,x+1,y)){
queue.add(new Point(x+1,y,map[x+1][y]+p.cost,p));
}
if(goTo(map,x+1,y+1)){
queue.add(new Point(x+1,y+1,map[x+1][y+1]+p.cost,p));
}
if(goTo(map,x,y+1)){
queue.add(new Point(x,y+1,map[x][y+1]+p.cost,p));
}
if(goTo(map,x,y+1)){
queue.add(new Point(x,y+1,map[x][y+1]+p.cost,p));
}
return null;
}
public static boolean goTo(int[][] map, int x, int y){
try{
@SuppressWarnings("unused")
int a = map[x][y];
return true;
}catch(Exception e){return false;}
}
public static class Point implements Comparable<Point>, Cloneable{
int x,y,cost;
Point prev;
public Point(int x,int y,int cost){
this.x=x;
this.y=y;
this.cost=cost;
prev = null;
}
@SuppressWarnings("unchecked")
public Point(int x,int y,int cost,Point p){
this.x=x;
this.y=y;
this.cost=cost;
prev = p;
}
@Override
public int compareTo(Point o) {
return cost-o.cost;
}
}
}