-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlightson.java
132 lines (105 loc) · 3.77 KB
/
lightson.java
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
import sun.awt.image.ImageWatched;
import java.io.*;
import java.util.*;
class edges{
ArrayList<Integer> edges;
}
public class lightson {
public static void main(String[] args) throws IOException {
// write your code here
BufferedReader f = new BufferedReader(new FileReader("lightson.in"));
// input file name goes above
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("lightson.out")));
StringTokenizer st = new StringTokenizer(f.readLine());
int n = Integer.parseInt(st.nextToken());
int m = Integer.parseInt(st.nextToken());
edges[][] edge = new edges[n][n];
for(int i = 0; i<m;i++){
st = new StringTokenizer(f.readLine());
int startx = Integer.parseInt(st.nextToken())-1;
int starty= Integer.parseInt(st.nextToken())-1;
int endx = Integer.parseInt(st.nextToken())-1;
int endy = Integer.parseInt(st.nextToken())-1;
if(edge[startx][starty]==null){
edge[startx][starty] = new edges();
edge[startx][starty].edges = new ArrayList<Integer>();
}
edge[startx][starty].edges.add(endx);
edge[startx][starty].edges.add(endy);
}
boolean[][] on = new boolean[n][n];
boolean[][] visited = new boolean[n][n];
LinkedList<Integer> s = new LinkedList<Integer>();
on[0][0] = true;
s.add(0);
s.add(0);
while(s.size()!=0){
//while you still have stuff to checkc
int x = s.poll();
int y = s.poll();
//get an x and y
System.out.println(x+" "+y);
if(visited[x][y]){
continue;
}
visited[x][y] = true;
//you've visitied it now
if(edge[x][y]!=null) {
for (int i = 0; i < edge[x][y].edges.size(); i += 2) {
int endx = edge[x][y].edges.get(i);
int endy = edge[x][y].edges.get(i + 1);
if (!on[endx][endy]) {
on[endx][endy] = true;
if (endx != 0 && on[endx - 1][endy]&&visited[endx-1][endy]) {
s.add(endx);
s.add(endy);
}
if (endy != 0 && on[endx][endy - 1]&&visited[endx][endy-1]) {
s.add(endx);
s.add(endy);
}
if (endx != n - 1 && on[endx + 1][endy]&&visited[endx+1][endy]) {
s.add(endx);
s.add(endy);
}
if (endy != n - 1 && on[endx][endy + 1]&&visited[endx][endy+1]) {
s.add(endx);
s.add(endy);
}
}
}
}
if (x != 0 && on[x - 1][y]) {
s.add(x-1);
s.add(y);
}
if (y != 0 && on[x][y - 1]) {
s.add(x);
s.add(y-1);
}
if (x != n - 1 && on[x + 1][y]) {
s.add(x+1);
s.add(y);
}
if (y != n - 1 && on[x][y + 1]) {
s.add(x);
s.add(y+1);
}
}
int count = 0;
for(boolean[] x: on){
for(boolean my:x){
if(my){
count++;
System.out.print(1);
}
else{
System.out.print(" ");
}
}
System.out.println();
}
out.println(count);
out.close();
}
}