-
Notifications
You must be signed in to change notification settings - Fork 0
/
spoj.cpp
116 lines (91 loc) · 4.01 KB
/
spoj.cpp
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include <iostream>
#include <cstring>
#include <queue>
#define pp pair<int,int>
using namespace std;
int max(int r, int t)
{
return r > t ? r : t;
}
int main()
{
int t;
cin >> t;
while(t--) {
int n,m;
cin >> n >> m;
char s[n][m];
int b[n][m];
int p[n][m];
int x1,y1;
int x2,y2;
for(int i = 0; i < n; i++) {
cin >> s[i];
}
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
cin >> b[i][j];
}
}
cin >> x1 >> y1 >> x2 >> y2;
//memset(p,10000,sizeof(p));
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
p[i][j] = 99999;
}
}
p[x1][y1] = 0;
queue<pp> q;
q.push(pp(x1,y1));
pp num;
int x,y;
while(!q.empty()) {
num = q.front();
q.pop();
x = num.first;
y = num.second;
if(x > 0 && max(p[x][y]+1,b[x-1][y])<p[x-1][y]) {
p[x-1][y] = max(p[x][y]+1,b[x-1][y]);
q.push(pp(x-1,y));
cout << "pushed value are " << x-1 << " and " << y << endl;
cout << "cost is " << p[x-1][y] << endl;
}
if(x < (n-1) && max(p[x][y]+1,b[x+1][y])<p[x+1][y]) {
p[x+1][y] = max(p[x][y]+1,b[x+1][y]);
q.push(pp(x+1,y));
cout << "pushed value are " << x+1 << " and " << y << endl;
cout << "cost is " << p[x+1][y] << endl;
}
if(y > 0 && max(p[x][y]+1,b[x][y-1])<p[x][y-1]) {
p[x][y-1] = max(p[x][y]+1,b[x][y-1]);
q.push(pp(x,y-1));
cout << "pushed value are " << x << " and " << y-1 << endl;
cout << "cost is " << p[x][y-1] << endl;
}
if(y < (m-1) && max(p[x][y]+1,b[x][y+1])<p[x][y+1]) {
p[x][y+1] = max(p[x][y]+1,b[x][y+1]);
q.push(pp(x,y+1));
cout << "pushed value are " << x << " and " << y+1 << endl;
cout << "cost is " << p[x][y+1] << endl;
}
}
/*cout << "\n\n";
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
cout << b[i][j] << " ";
}
cout << endl;
}
cout << "\n\n";
cout << "\n\n";
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
cout << p[i][j] << " ";
}
cout << endl;
}
cout << "\n\n";*/
cout << p[x2][y2] << endl;
}
return 0;
}