Problem Statement
There is a grid with H horizontal rows and W vertical columns. Let (i,j) denote the square at the i-th row from the top and the j-th column from the left.
Find the number of squares that share a side with Square R,C).
Here, two squares (a,b) and (c,d) are said to share a side if and only if ∣a−c∣+∣b−d∣=1 (where ∣x∣ denotes the absolute value of x).
Constraints
- All values in input are integers.
- 1≤R≤H≤10
- 1≤C≤W≤10
Input
Input is given from Standard Input in the following format:
H W R C
Output
Print the answer as an integer.
Sample 1
Inputcopy | Outputcopy |
---|---|
3 4 2 2 | 4 |
We will describe Sample Inputs/Outputs 1,21,2, and 33 at once below Sample Output 33.
Sample 2
Inputcopy | Outputcopy |
---|---|
3 4 1 3 | 3 |
Sample 3
Inputcopy | Outputcopy |
---|---|
3 4 3 4 | 2 |
When H=3 and W=4, the grid looks as follows.
- For Sample Input 1, there are 4 squares adjacent to Square 2,2).
- For Sample Input 2, there are 3 squares adjacent to Square(1,3).
- For Sample Input 3, there are 2 squares adjacent to Square (3,4).
Sample 4
Inputcopy | Outputcopy |
---|---|
1 10 1 5 | 2 |
Sample 5
Inputcopy | Outputcopy |
---|---|
8 1 8 1 | 1 |
Sample 6
Inputcopy | Outputcopy |
---|---|
1 1 1 1 | 0 |
#include <iostream>
#include <math.h>
using namespace std;
/*输入有两行:总行列:(H,W)和正方形所在位置(a,b)
要输出与正方形相邻正方形的总数
1.普通情况: H>a && W>b;
循环,i<=R,j<=W,(i-a)+(j-b)=1;cout++
2.H=a && W>b;
同1
3.H>a && W=b
同1
4.H=a=1 &7 W=b=1
0
*/
int main()
{
int array1[2];
int array2[2];
int i,j;
int count=0;
for(i=0;i<2;i++){
cin>>array1[i];
}
for(i=0;i<2;i++){
cin>>array2[i];
}
if(array1[0]==array2[0] && array1[1]==array2[1] && array1[0]==1 && array1[1]==1)cout<<0;
else{
for(i=1;i<=array1[0];i++){
for(j=1;j<=array1[1];j++){
if(abs(i-array2[0])+abs(j-array2[1])==1)count++;
}
}
cout<<count<<endl;
}
return 0;
}