#include #include #include #include #include #include #include using namespace std; typedef long long ll; typedef pair Pii; typedef pair P; int H, W; char a[2000][2000]; ll dist[2000][2000]; ll dist1[2000][2000]; const ll INF = 1e15; int dh[4] = {-1, 1, 0, 0}; int dw[4] = {0, 0, -1, 1}; bool in_field(int h, int w){ return h >= 0 && h < H && w >= 0 && w < W; } void dijkstra(){ priority_queue, greater

> que; for(int i = 0; i < H; i++){ for(int j = 0; j < W; j++){ dist[i][j] = INF; } } dist[0][0] = 0; que.push(P(0, Pii(0, 0))); while(!que.empty()){ P p = que.top();que.pop(); Pii v = p.second; int h = v.first, w = v.second; if(dist[h][w] < p.first) continue; for(int i = 0; i < 4; i++){ int h_to = h+dh[i], w_to = w+dw[i]; if(!in_field(h_to, w_to)) continue; ll cost = (a[h][w] == '.' ? 1 : (dist1[h][w]+1)); if(dist[h][w] + cost < dist[h_to][w_to]){ dist[h_to][w_to] = dist[h][w] + cost; dist1[h_to][w_to] = dist1[h][w] + 1; que.push(P(dist[h_to][w_to], Pii(h_to, w_to))); } } } } int main(){ ios::sync_with_stdio(false); cin.tie(0); cout << setprecision(10) << fixed; cin >> H >> W; for(int i = 0; i < H; i++){ for(int j = 0; j < W; j++){ cin >> a[i][j]; } } dijkstra(); cout << dist[H-1][W-1] << endl; }