Skip to content

Commit

Permalink
Merge pull request #108 from Jagpreet153/day_20_medium_Jagpreet153
Browse files Browse the repository at this point in the history
feat: solved day 20 medium
  • Loading branch information
shiwangi-07 authored Apr 15, 2024
2 parents 77ef960 + 3db9d45 commit 496a47e
Showing 1 changed file with 50 additions and 1 deletion.
51 changes: 50 additions & 1 deletion Medium/Day 20/solution.cpp
Original file line number Diff line number Diff line change
@@ -1 +1,50 @@
// Write your code here.
// Write your code here.
//Code by Jagpreet153
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int main()
{
int h, w;
cin >> h >> w;
vector<vector<int> > m(h, vector<int>(w)), dp(h, vector<int>(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;
}

0 comments on commit 496a47e

Please sign in to comment.