Skip to content

Commit

Permalink
counting method changed
Browse files Browse the repository at this point in the history
  • Loading branch information
javaarchive committed Aug 3, 2020
1 parent ef7484e commit 543dea3
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions USACOClassLiveTest/src/lightson.java
Expand Up @@ -12,7 +12,7 @@ public static boolean range(int i) {
return (0 <= i) && (i <= N);
}

public static int total = 1;
public static int total = 0;

public static List<Room> adj(int x, int y) {
List<Room> out = new ArrayList<>();
Expand Down Expand Up @@ -52,11 +52,12 @@ public static void recur(int x, int y) {
// System.out.println("recur("+x+","+y+")");
visited[x][y] = true;
Room r = new Room(x, y);

if (adjlist.keySet().contains(r)) {
List<Room> connected = adjlist.get(r);
for (Room room : connected) {
if (!map[room.x][room.y]) {
total++;
//total++;
// System.out.println("Lit "+room.x+" "+room.y);
if (nearBright(room.x, room.y)) {
map[room.x][room.y] = true;
Expand All @@ -69,21 +70,22 @@ public static void recur(int x, int y) {
}
// for(int i = 0; i < 2; i ++){
if (range(x + 1) && map[x + 1][y] && !visited[x + 1][y]) {
visited[x + 1][y] = true;
//visited[x + 1][y] = true;
recur(x + 1, y);
}
if (range(x - 1) && map[x - 1][y] && !visited[x - 1][y]) {
visited[x - 1][y] = true;
//visited[x - 1][y] = true;
recur(x - 1, y);
}
if (range(y + 1) && map[x][y + 1] && !visited[x][y + 1]) {
visited[x][y + 1] = true;
//visited[x][y + 1] = true;
recur(x, y + 1);
}
if (range(y - 1) && map[x][y - 1] && !visited[x][y - 1]) {
visited[x][y - 1] = true;
//visited[x][y - 1] = true;
recur(x, y - 1);
}

// }
}

Expand Down Expand Up @@ -116,6 +118,13 @@ public static void main(String[] args) throws IOException {
visited[1][1] = true;
map[1][1] = true;
recur(1, 1);
for(int i =0; i < map.length; i ++){
for(int j = 0; j < map[i].length; j ++){
if(map[i][j]){
total ++;
}
}
}
pw.println(total);
pw.close();
}
Expand Down

0 comments on commit 543dea3

Please sign in to comment.