-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVanyaAndTreasure.cc
76 lines (67 loc) · 1.47 KB
/
VanyaAndTreasure.cc
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
#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;
void print(int *matrix,int n,int m);
int distanc(int m,int n, int p,int pointX, int pointY, int *arr, int *save);
int main(){
int n;
int m;
int p;
scanf("%d %d %d",&n,&m,&p);
int arr[301][301];
int save[301][301];
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
scanf("%d",&arr[i][j]);
save[i][j] = -1;
}
}
int pointX;
int pointY;
for(int i = 1;i<=n;i++){
for(int j=1;j<=m;j++){
if(arr[i][j] == p){
pointX = i;
pointY = j;
break;
}
}
}
int dist = distanc(m,n,p,pointX,pointY, (int*)arr, (int*)save);
cout<<dist;
return 0;
}
int distanc(int m,int n, int p,int pointX,int pointY, int* arr, int* save){
int* pointCoordinate = (save + 301*pointX + pointY);
if(*(pointCoordinate) != -1){
return *pointCoordinate;
}
if(p == 1){
*(save + m * pointX + pointY) = abs(pointX - 1) + abs(pointY - 1);
return abs(pointX - 1) + abs(pointY - 1) ;
}
int minDist = 10000;
for(int i = 1;i<=n;i++){
for(int j = 1;j<=m;j++){
int q = p-1;
if(*(arr+ i*301 + j) == q){
int dist = abs(pointX - i)+abs(pointY - j) + distanc(m,n,q,i,j,(int*)arr,(int*)save);
minDist = min(dist, minDist);
}
}
}
*pointCoordinate = minDist;
return minDist;
}
void print(int *matrix,int n,int m)
{
int i, j;
for (i = 1; i <= n; ++i)
{
for (j = 1; j <= m; ++j){
printf("%d ", *(matrix + i*301 + j));
}
printf("\n");
}
}