-
Notifications
You must be signed in to change notification settings - Fork 0
/
kaththi.cpp
65 lines (51 loc) · 1.31 KB
/
kaththi.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
#include <bits/stdc++.h>
using namespace std;
#ifdef DEBUG
#include "Competitive-Programming/debug.cpp"
#else
#define dbg(...)
#endif
#define all(x) x.begin(), x.end()
using ll = long long;
const vector<pair<int, int>> dir4{{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
const vector<pair<int, int>> dir8{{1, 0}, {-1, 0}, {0, 1}, {0, -1}, {-1, -1}, {-1, 1}, {1, -1}, {1, 1}};
void solve() {
int n, m;
cin >> n >> m;
vector<string> g(n);
for (auto &x : g) cin >> x;
deque<pair<int,int>> q;
q.emplace_back(0, 0);
vector<vector<int>> dist(n, vector<int>(m, INT_MAX));
dist[0][0] = 0;
while (not q.empty()) {
auto [cx, cy] = q.front();
if (cx == n-1 and cy == m-1) break;
q.pop_front();
for (auto [dx, dy] : dir4) {
auto x = cx + dx;
auto y = cy + dy;
if (x < 0 or x >= n or y < 0 or y >= m) continue;
auto new_cost = dist[cx][cy] + (g[cx][cy] != g[x][y]);
if (dist[x][y] > new_cost) {
dist[x][y] = new_cost;
if (g[cx][cy] == g[x][y])
q.emplace_front(x, y);
else
q.emplace_back(x, y);
}
}
}
cout << dist[n-1][m-1] << '\n';
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int t = 1;
cin >> t; cin.ignore();
for (auto i = 1; i <= t; i++) {
solve();
}
return 0;
}