-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCount Steps in Matrix.cpp
More file actions
64 lines (48 loc) · 920 Bytes
/
Count Steps in Matrix.cpp
File metadata and controls
64 lines (48 loc) · 920 Bytes
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
/* Ashutosh Verma - ashutoshvrm8@gmail.com
I like writing neat codes :)
*/
#include<iostream>
#include<math.h>
#define M 501
#define N 250002
using namespace std;
long long int arr[M][M],n1[N],n2[N];/*heap is the best :) */
int main()
{
long long int t,n,i,j,size,sum,temp,t1,t2;
cin>>t;
while(t--)
{
cin>>n;
size=n*n;
sum=temp=t1=t2=0;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
cin>>arr[i][j];
}
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
temp=arr[i][j];
n1[temp]=i+1;
n2[temp]=j+1;
}
}
/*for(i=1;i<=size;i++)
cout<<n1[i];*/
/*for(i=1;i<=size;i++)
cout<<n1[i];*/
for(i=2;i<=size;i++)
{
t1=fabs(n1[i]-n1[i-1]);
t2=fabs(n2[i]-n2[i-1]);
sum=sum+t1+t2;
}
cout<<sum<<"\n";
}
return 0;
}