-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCudoviste.java
117 lines (93 loc) · 3.19 KB
/
Cudoviste.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
import java.io.*;
import java.util.*;
import java.text.*;
import java.util.Scanner;
public class Cudoviste{
public static class ParkingScore{
public int free_slots;
public int one_car;
public int two_cars;
public int three_cars;
public int four_cars;
public ParkingScore(){
free_slots = 0;
one_car = 0;
two_cars = 0;
three_cars = 0;
four_cars = 0;
}
public void print(){
System.out.println(free_slots + "\n" + one_car + "\n" + two_cars + "\n" + three_cars + "\n" + four_cars);
}
}
public static void main(String[] args) {
//String input = "4 5\n..XX.\n.#XX.\n..#..\n.....\n";
//Scanner sc = new Scanner(input);
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
String[] rowsAndCol = str.split("\\s+");
//first time
String bottomLine = "";
int rows = Integer.parseInt(rowsAndCol[0]);
int col = Integer.parseInt(rowsAndCol[1]);
int numIterationCol = col-1;
ParkingScore score = new ParkingScore();
while(sc.hasNextLine()) {
String topLine = bottomLine;
bottomLine = sc.nextLine();
if(topLine.equals("")){
continue;
}
String[] firstRowChar = topLine.split("");
String[] secondRowChar = bottomLine.split("");
for(int i=0; i<numIterationCol; i++){
String[] firstAndSecondRow = new String [] {firstRowChar[i], firstRowChar[i+1], secondRowChar[i], secondRowChar[i+1]};
int totFreeSlots = nrFreeParkingslots(firstAndSecondRow);
switch(totFreeSlots){
case 0:
score.free_slots += 1;
break;
case 1:
score.one_car += 1;
break;
case 2:
score.two_cars += 1;
break;
case 3:
score.three_cars += 1;
break;
case 4:
score.four_cars += 1;
break;
default:
break;
}
}
}
score.print();
}
//returning number of free parking slots
public static int nrFreeParkingslots(String[] firstAndSecondRow){
int nrCars = 0;
int nrHouse = 0;
for(int i = 0; i < 4; i++){
switch(firstAndSecondRow[i]){
case "X":
nrCars += 1;
break;
case "#":
nrHouse = -1;
break;
case ".":
break;
default:
break;
}
}
if(nrHouse < 0){
return nrHouse;
} else{
return nrCars;
}
}
}