From 3db9d45cf4fb7c25739590f53c7a67d4d42384b9 Mon Sep 17 00:00:00 2001 From: Jagpreet Singh Khurana <119691733+Jagpreet153@users.noreply.github.com> Date: Mon, 15 Apr 2024 12:34:16 +0530 Subject: [PATCH] feat: solved day 20 medium --- Medium/Day 20/solution.cpp | 51 +++++++++++++++++++++++++++++++++++++- 1 file changed, 50 insertions(+), 1 deletion(-) diff --git a/Medium/Day 20/solution.cpp b/Medium/Day 20/solution.cpp index 2084d29..d960957 100644 --- a/Medium/Day 20/solution.cpp +++ b/Medium/Day 20/solution.cpp @@ -1 +1,50 @@ -// Write your code here. \ No newline at end of file +// Write your code here. +//Code by Jagpreet153 +#include +using namespace std; +typedef long long ll; + +int main() +{ + int h, w; + cin >> h >> w; + vector > m(h, vector(w)), dp(h, vector(w, 0)); + for(int i = 0; i < h; i++) + { + string s; + cin >> s; + for(int j = 0; j < w; j++) { + if(s[j] == '#') m[i][j] = 1; + else m[i][j] = 0; + } + } + if(m[0][0] == 1) dp[0][0] = 1; + else dp[0][0] = 0; + + for(int i = 0; i < h; i++) for(int j = 0; j < w; j++) + { + if(i == 0 && j == 0) + continue; + if(i == 0) + { + if(m[i][j-1]==0 && m[i][j]) + dp[i][j] = dp[i][j-1] + 1; + else + dp[i][j] = dp[i][j-1]; + } + else if(j == 0) + { + if(m[i-1][j]==0 && m[i][j]) + dp[i][j] = dp[i-1][j] + 1; + else + dp[i][j] = dp[i-1][j]; + } + else + { + dp[i][j] = min(dp[i-1][j]+((m[i-1][j]==0 && m[i][j])?1:0), dp[i][j-1]+((m[i][j-1]==0 && m[i][j])?1:0)); + } + } + + cout << dp[h-1][w-1] << endl; + return 0; +} \ No newline at end of file