-
Notifications
You must be signed in to change notification settings - Fork 12
/
K1.cc
93 lines (86 loc) · 1.98 KB
/
K1.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <set>
#include <map>
#include <vector>
#include <string>
#include <queue>
#include <cstring>
#include <algorithm>
#include <tr1/unordered_map>
#include <tr1/unordered_set>
using namespace std;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef pair<int, int> II;
typedef vector<II> VII;
typedef long long LL;
#define TCASE int __t; cin >> __t; while (__t--)
#define FOR(i, a, b) for(int i = a; i < b; i++)
#define REP(i, b) FOR(i, 0, b)
#define PB push_back
#define MP make_pair
#define ALL(a) a.begin(), a.end()
#define CLR(a) memset(a, 0, sizeof(a))
#define GC getchar_unlocked
inline int read_int() {
register int c = GC(), x = 0;
while ((c < 48 || c > 57) && c != '-') {
c = GC();
}
int sign = 1;
if (c == '-') {
c = GC();
sign = -1;
}
while (c >= 48 && c <= 57) {
x = x * 10 + c - 48;
c = GC();
}
return sign * x;
}
char grid[1010][1010];
int dis[1010][1010];
int dx[4] = {1, -1, 0, 0}, dy[4] = {0, 0, 1, -1};
int r, c;
inline bool val(int x, int y) {
return x >= 0 && y >= 0 && x < r && y < c;
}
int main() {
TCASE {
r = read_int(), c = read_int();
REP(i, r) {
REP(j, c) {
do {
grid[i][j] = GC();
} while (grid[i][j] < 'a' || grid[i][j] > 'z');
}
}
REP(i, r)
REP(j, c)
dis[i][j] = 100000000;
dis[0][0] = 0;
set<pair<int, II> > q;
q.insert(MP(0, MP(0, 0)));
while (!q.empty()) {
II p = (*(q.begin())).second;
q.erase(q.begin());
int x = p.first, y = p.second;
REP(_i, 4) {
int nx = x + dx[_i], ny = y + dy[_i];
if (val(nx, ny)) {
int d = (grid[x][y] != grid[nx][ny]);
if (dis[x][y] + d < dis[nx][ny]) {
q.erase(MP(dis[nx][ny], MP(nx, ny)));
dis[nx][ny] = d + dis[x][y];
q.insert(MP(dis[nx][ny], MP(nx, ny)));
}
}
}
}
cout << dis[r - 1][c - 1] << endl;
}
return 0;
}