-
Notifications
You must be signed in to change notification settings - Fork 1
/
FarmerLamp.java
73 lines (69 loc) · 2.45 KB
/
FarmerLamp.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
package Ishank;
import java.sql.SQLOutput;
import java.util.*;
public class FarmerLamp {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter the length of the row");
int row = scan.nextInt();
System.out.println("Enter the length of column");
int col = scan.nextInt();
int[][] arr = new int[row][col];
int r = position(arr);
System.out.println("There are " + r + " unused lamps.");
}
static int position(int[][] arr) {
Scanner sc = new Scanner(System.in);
char[][] back=new char[arr.length][arr[0].length];
char c;
int t=0;
do {
t++;
System.out.println("Enter the position of lamp");
String s = sc.nextLine();
int row = (int) (s.charAt(0)) - 48;
int col = (int) (s.charAt(s.length() - 1)) - 48;
arr[row][col] = arr[row][col] + 10;
back[row][col]='!';
for (int i = row - 1; i <= row + 1; i++) {
if (i >= 0 && i < arr.length) {
for (int j = col - 1; j <= col + 1; j++) {
if (j >= 0 && j < arr[0].length) {
arr[i][j]++;
}
}
}
}
System.out.println("Do You want anymore lamps? (Y-yes, N-no)");
c = sc.next().charAt(0);
String q = sc.nextLine();
} while (c == 'Y');
int l = 0, k = 0, d = 0;
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[0].length; j++) {
if (arr[i][j] >= 10) {
arr[i][j] = arr[i][j] - 10;
abc: for (k = i - 1; k <= i + 1; k++) {
if (k >= 0 && k < arr.length) {
for (l = j - 1; l <= j + 1; l++) {
if (l >= 0 && l < arr[0].length) {
arr[k][l]--;
if (arr[k][l] == 0) {
d++;
back[i][j]='X';
break abc;
}
}
}
}
}
}
}
}
for (int i = 0; i < back.length; i++) {
System.out.println(Arrays.toString(back[i]));
}
System.out.println("Here 'X' signifies the useful lamps and '!' signifies unused lamps.");
return t-d;
}
}