-
Notifications
You must be signed in to change notification settings - Fork 0
/
Perimeter.java
52 lines (50 loc) · 1 KB
/
Perimeter.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
import java.util.Scanner;
public class Perimeter{
static int a[][],R=3,C=5;
/*static int mat[R][C] =
{
{0, 1, 0, 0, 0},
{1, 1, 1, 0, 0},
{1, 0, 0, 0, 0}
};*/
public static int count(int i,int j){
int count=0;
//up
if(i>0&&a[i-1][j]==1)
count++;
//right
if(j<C-1&&a[i][j+1]==1)
count++;
//down
if(i<R-1&&a[i+1][j]==1)
count++;
//left
if(j>0&&a[i][j-1]==1)
count++;
return count;
}
public static int perimeter(int r,int c){
int i,j,per=0;
for(i=0;i<r;i++)
for (j=0;j<c;j++ )
if(a[i][j]==1)
{
per+=4-count(i,j);
}
return per;
}
public static void main(String[] args) {
int i,j;
Scanner in=new Scanner(System.in);
a=new int[R][C];
System.out.println("Enter rows=");
R=in.nextInt();
System.out.println("Enter columns=");
C=in.nextInt();
for(i=0;i<R;i++)
for(j=0;j<C;j++)
a[i][j]=in.nextInt();
System.out.println("Result=");
System.out.println(perimeter(R,C));
}
}