 What up YouTube Mike the Coder here. Today we are going to go over CSE as problems said grid paths. So essentially there are 88,418 paths in a seven by seven grid from the upper left corner to the lower left. So we're going from this upper left here and then we're going to go all the way down to here. There's 88,418 paths as well like whatever direction I could go up down left right whatever it is in the end I'm going to reach the bottom left corner and there's 88,418 different ways like different paths. I go through all every single square and then I come back from the top left corner all the way down to the bottom left corner. So there's 88,418 paths in a seven by seven grid. So each path corresponds to 48 character descriptions considering up down left right. So what they mean here is that if I go down up down left right in a path to go from one square to other I only could go down or I could go up or I could go left or I could go right. So if I'm at this square, I could go down right up and then right right I could go any direction there I want up down left right. What I cannot do is I cannot go diagonal. So it's not like I could start from this square upper left and then I just jump down to this one diagonal. That's not allowed. So we only could go down up left right. So yeah, this is an example of a path is just like a bunch of random path you take. This is just one example of a path is 88,418 paths in a total of different paths in a seven by seven grid. Okay, so this is one path and this is the example of going up down left right so on and so forth. Now we are given a description of a path which may contain question marks. Our task is just to calculate the number of paths that match the description given this many question marks and this many ours, whatever important it is, question mark can represent any direction. This question mark can represent any direction. And our task is now given this path which could represent any direction for the question mark, we need to calculate the number of paths that match the description any question mark. And yeah, so we have to do so in this example, we have a bunch of question marks and then we have our and then you and then more question marks and LD more question marks and down and then question marks. So question marks mean any single direction that we could have so we could have up down left right but the regular letters that represents like the path that we have to so they're asking is just now our output is just print the total number of paths. So like the total number of different paths, given this path that we could have for whatever it is right for question marks. And our output is 201. Okay, so before we even do this question, we should actually try to do a complete brute force complete search and try to get 88,418 paths in a seven by seven grid. And the reason why is because if we have all 88,418 paths in the seven by seven grid, just trying to find this path is much easier, right? If we already could brute force 88,418 paths, after we get those paths, given this input, it would be much easier to just get, you know, the output 201. So if I want to get started, just brute forcing all the seven of 88,418 path different paths. I need to start with a simple salt function. And I'm going to pass in the row in the column, because what I'm going to do is I'm going to brute force every single direction in this path that I'm going to go to. Now I'm going to have a visited Boolean with seven by seven. This is going to represent a seven by seven board present true and false. If I already visited, I'm going to mark it as true if I didn't be false in C++. If you don't initialize anything, they're automatically set as false, I believe. So this visited originally is going to be false. Okay, I'm going to create a counter called paths and just going to keep count of the number of paths. And then I'm going to have steps. Okay, the steps is going to represent the number of steps that I'm currently taking. So what do I have to do here? So first, since I'm going to go down up and left to right, I need to call solved multiple times. So that's called solved RC, and it calls solved one, two, three, four, four times, right, because I have to go down, up, left and right. So down, so down, up, left, right. Okay, so now, so if I'm at the first zero, zero at the upper left hand square, right, if I want to go down, what do I do? Well, if I'm at row zero, zero, I want to go and I want to go to row one, zero, the next one, going down is adding one to the row. So that means if I'm going to go down, I'm going to add one to the row. Likewise, if we leave the different number of columns in the rows, zero, one, two, three, four, five, six, and last row, if I want to go up, I've subtract one row because six minus one is five, and that's the one above it. So going up, I'm going to subtract one row, right. So here, this one's going to represent going down, it's going to represent going up, and I need to go to the other ones. So left, right, go left. That means I'm going left on the decreasing the number of columns by one. So here I'm going to decrease the columns by one. And if I go right, I'm going to increase the columns by one. So this is going to go left. And this is right. Okay, now, so I'm going to recursively call this going down, go up, go left and right. Now here's the issue with this is that I don't want to actually go to the same position twice, right, if I went up down left, right. So that's why I have this visited right that tells me this visited board tells me if it's true, that means I already visited if it's false, I have not visited. So I'm going to actually use, use this visited array and say, for each of these cases, I didn't visit it at the row and the column, then I'll call it, right, because I don't want to visit twice. So I'm going to do that for both. So we're going to just do this the same thing, copy this, this, this, we're going to do it for all the cases. Okay. So every four cases, and do one last one. This is actually left. And then we need one more for right. Okay, so now we have all the stuff. So yeah, this is like checking if we visited each of these, then we'll go to the if we didn't visit it yet, we're going to go to it. Okay. Now, what else do I have to check? Well, I need to check for boundaries, right? The reason why is because I don't want to go out of bounds, right? I'm at the edge of the board. So what I'm going to do is for r plus one, you know, when is the case when go to the right side of our plus symbol when it's like when your rows less than six, because if we keep going down, if we're on the last row, let's say we are on zero one, two, three, four, five, six, right, find the last one was six, we can't go down. And then for going up, you have to make sure this is greater than zero. Because if we're on the zero throw, we can't go up again, only if it's greater than zero. And so if we're on the first one, we could go up any of us greater than zero than yet. And I'm going to do the same thing for column as well. So actually, when C is greater than zero, go left, when C is less than six, we go right. Now for this part, my forgot to do is that once we've recursively call each of these, right, we actually have to set that we already visited the square every time we go through solved. So what I'm going to do is add the row and the column on the top, I'm going to set to true, which means I visited the square. And then afterwards, I'm going to set it equal to false. And the reason why is I'm going to backtrack. So how this works is let's say we're at this square, right, we're going right up down left, right, right. So let's say I already went down here, I set this position at a one zero to be true. Well, there's also a case where I didn't set this position to be true, right, and that's going to the right path, right, just going to the right instead of going down. So that's why we're doing when you said visited equal to true in the beginning, and then you're recursively call your scenario, yo, you can also have to set equal to false in the end for the other case when you didn't go down this path. So let's say if I went down my first scenario, I also have to set when I not go down. So that's why we have this visit setting to true. And then we have visits go to false after. Okay, so now that we recursively called each of the four scenarios that we could go to, and we made sure our boundaries are correct. And then said each time we visited to be true, and then backtrack out of it, when do we stop our recursive call, they want us to stop going when we reach the destination of the bottom left, right? So we start at upper left here, and then we want to go to lower left, what is the position of the lower left square. So if the top left is zero zero, the lower left is all the way on here. And it's a seven by seven grid. So let's just count we have zero, one, two, three, four, five, six. So the position at the lower left is going to be six of row six. And then since we're columns are also numbered by zero, it'll be row six, and then column zero. So when I reach, are you as equal row six, and the column is equal to zero? At this point, I'm just going to return. That means I reached the end of my recursive. So let's say I reach row six and column zero. What scenario is it a path? So when there's a path, it means that I visited all the squares. Because that means that every single square I visited all of them, since we don't really want to loop through all the visit paths and see if we visit all of them, we could check if we've visited or most of the squares, because each path contains corresponds to 48 character description, right? And what that means is that if I'm going to reach all the everything up until the last square, right, that means that since there's seven by seven, right, this table seven by seven, that means there's 49 each of these cells. And since I'm going to visit all of them up until the last square, that means that there'll be 48 steps, right, there'll be 48 steps until I reach the last square. So what that means is that if my steps is equal to 48, so if my steps is equal to 48, that means I visit all the squares, I'm going to increment my paths, 48 steps. And that means that that's consistent one path. So we're going to increment our paths. Now each time we recursively call this, we need to actually increment our steps, because every time we reach a square, and we set out as true, we need to increment that. So for each of these recursive calls, I'm going to increase steps. So before these, I'm going to increase steps plus one for each of them. But also since there is another situation where we're not going down, right? So let's say I went steps, I went up, you know, went up, right, there's another situation where I didn't go at this position, right, I didn't go down, maybe I went left or maybe I went right. So for that situation, we're going to decrement steps afterwards. And that's considering when we don't choose this position to move towards that way, we're going to go through every single possible position in the case where it works. Now let's actually run this. So if I call solved on zero zero, I should be able to get 88,000. Okay, I should be able to get 88,000. So what I'm going to do is every time I enter in paths, I'm going to see our paths. Now, there's another thing what we could do is that in order to optimize this, we could say if it's already visited, we're just returned. So if visited at the row and column, which return, the reason why we're doing this is that we already visited that square, we don't want to visit it twice. Ideally, this should give us 88,000, it will take some time. It's going to take a take some time. So we got the right answer, 88,000, 418, but it took about 10 minutes to do. So let's actually thrown it so that we optimize it a little bit more. So one optimization we could do is when we hit a wall, we know we could just return. And the reason why is because when we hit a wall, the graph, our grid ends up getting split in half. And in this situation, when it gets split in half, no matter what you choose, I decide to choose left, go left or I go right. If you go left, you only could go through the values that are unvisited on left side, and you can't go back to the right side. And it's the same thing if you go right, if you go right, you only could visit the values on the right side, and you can you have values on left side that are not can't visit it afterwards either. So if you hit a wall, right, you can never reach the final destination of bottom left. Well, how do you know when you hit a wall? Well, you hit a wall when you're at either row zero, and the columns are between one and five, because we're not counting corners, right? So let's say I'm at row zero columns between one and five, and I haven't visited the left side or the right side. So I haven't visited column minus one, and I haven't visited column plus one, but I have visited row plus. So I visited the row below me, right, but I haven't visited on the left side or the right side. So this is the huge if statement that we could set. I know this is kind of really hard to read, but bear with me. So the first part is checking if the column is between one and five. So that's the part here, right, columns are one and five, right, one, two, three, four, five, right, because we're not counting these corners, columns one and five. And we haven't visited the right side. So visited at row column plus one. And we haven't visited the left side either. So that means that we can turn left or right. So that means that this side is not visited, and this side is not visited. And we have this giant statement is essentially saying we're at row zero. That means that we are at the first row. That's true. And we check if we visited row plus one at sea. So what that means is that we visited the row below us, and it's already visited row plus one at sea, and we only can turn left and right at the situation. So that's this condition. But we also have another worst statement. And this is the same thing, except this time it's at last row, right, this last row. So in this case, if we hit a wall, let's assume this arrow was pointing downwards, hit a wall here, it's the exact same thing. Except now we're at row six, and we already checked row minus one, the one above us was already visited. So that's where we have an or statement here, row equals six, row minus one's visited. And then this whole statement, we're ending it. So we have the first statement of the one that we already checked, but we have to end this with this whole statement. And the reason why is because this whole statement is just checking we hit a wall between the top part and the bottom part. But remember, if we hit a wall on the top part, when row zero, you still have to check left and right. And if you hit it on the bottom part, you still have to check left and right, your columns left to right. So that's why this part is ended, right, this whole statement of row thing is ended with this whole statement, not visited of column between the left and the right. And that's this whole first part here, that's the first first part here. But then also I'm going to have another or so in this statement is going to be the exact opposite. And this or is representing pretty much the exact same thing of what we had above. Except now we're doing the rows and the columns on the hitting a wall on the left side here and hitting a wall on the right side here, which is essentially the exact same thing. This time we're doing what we're swapping row and column so row is greater than equal to one and rows less than equal to five. Have we not visited the right side underneath us and above us. So this is going down and going to the top, right? And our columns is that we've already visited the column plus one. So the column on the right, but this time we're going top and down, right? And the column is equal to zero, rows equal to columns equal to six. And we visited row and column minus one. So this this whole statement here is if we hit a wall on this side, hit a wall on this side, say I hit a wall here on the column zero or column six, and I could go above go top and go down. Right. So that's the same thing as hitting a wall and splitting off and column zero is going top here and going down here. So that's this whole statement here also. So yeah, I know this is really hard to read, but that's bear with me. That is this is how it is. Okay. So like we haven't visited the left side or the right side, we have visited the row above us and below us. Okay. So yeah, that's this whole part of hitting a wall. And then we could optimize it one more time to try to get. So this currently runs on in 10 seconds. Yes, it takes about 10 seconds of seven seconds. We could try to get it down to one second underneath one second and that'll be good. So I decided to put everything in a giant conditional here for visited instead of breaking earlier. Yeah, it's just because like it's easier for to read. So what we had before was up here. Okay, it's all the stuff we have up here. And the visited as if it was already visited, we have an or statement, and then we have all our additionals. Okay. So what we had before was up here is the stuff the code here. So the next optimization is a little, it's a little hard to understand, but it's okay. So the next one is that if we already visited a value in our position at the on the left side, and we could go up and down. That's essentially is that the that means that it's not possible, right, because the idea can be generalized if the path cannot continue forward, but can turn left and right. The grade still splits into two parts that contains unvisited squares. So you cannot visit anymore. So in this case, we're at this position, and there's a visited already in front of us. And we could turn to the right, go to the top and go to the bottom, turn to the situations. We cannot visit anymore. So we just return. And I'll show you guys how that works here. So in this case, this is the conditional that we would check. So first, we have to check if it's between one and five, and our columns are between one and five. So our rows have to be between one and five, because we want to make sure it's in bounds between 12345, right, we're not in the corners. Okay, and the columns cannot be in the corners, so it's between one and five. And then now we're going to check this, these four statements. So the first one is if it's already visited on the right side. And right side here, it's going to check if it's already visited below us, and it's already visited above us, and we could turn left and we haven't we can we can turn left and right. So if it's already visited below us. So here it's essentially basically, it's very similar to this one. But imagine this is at like graph below it, or it's similar to this one. So basically, you could we already visited one below us. Imagine, imagine just rotating this, actually, I could just rotate it clockwise. There. Okay, so imagine, we're at this position, right, and we already visited something above us, and below us, right, and we could turn left and right. We haven't visited left side and we haven't visited right side. So in this case, it's not possible because, as you could see, even if you turn left or right, your graph is still split into two. And you cannot visit all the squares afterwards. So that's this this scenario. Okay, so if our rows and columns are between one and five, and we visited the one in front of us, and the one below us, right, and we haven't visited it on the right side, and we haven't visited the left side, then we could just return, right? So we've the one in front of us, we already visited one before us is already visited. Left side and right side, we haven't visited, we could just return because it's not possible. So that's this, this statement here. And I'm going to put another or okay, and this is the this time, this is going to be very similar to the previous one. Except in this case, we are checking columns from right side to left side. So here, it's going to be let's just rotate backwards. So here, we already visited on the left side, already visited on right side. And we will go top and bottom, right? In this case, it's not possible because the graph still gets split into two. And we cannot visit all the squares if we go either direction. So yeah, that's why this is here, already visited the left side, left side here, already visited the right side. And we haven't visited the one above us and the one below us. Okay. So in this case, you could just return, return it. Okay. So yeah, and this one runs about two seconds now. Okay, so now what we're going to do is we're going to actually do the problem statement, because to do the problem statement, they ask us to have forced moves on certain parts. So if there's a forced move, so we read in a string, if it's a question mark, we're gonna, it could be anything if there's R, it's a value there. And then if it's you, you have to do something L and D. So now there's force moves. Okay. So let's do that. So this part, this was just checking if clocks, this is checking how long we'd run. But I could just like put this on the bottom really fast. Okay. All right, so we remember. Remember, we did a clock here and now now. Yeah, okay, so we remember we did solved at zero zero and we print out paths. So now what we're going to do is we're going to read in a string S and then we're going to go through. And we're just going to what we're going to do is we're going to have an array called reserved. And it's going to give us some states of whether we could do it. So 49. And each of these is going to tell us if we already had how we already have, we have to do if we have to go up, we have to go down, we have to go left and we have to go right. Okay. So in this case, if it's question mark, let's just make reserved at i is going to equal to negative one. If it's up, let's just do reserved at i is going to equal to zero. Now let's do reserved at i is equal to one left reserved at i is going to equal to two reds going to be reserved at i is equal to three. Okay, so now once we have this, now we just have to set our values here and then keep a track of the count and then we print out the counting again. So yeah, here I'm going to put the solved and paths at the end. Okay, so now, now that we have an array that stores in the state of each of the reserved values for each of the steps, let's actually go back to the top here. So what I'm going to do is I'm going to have if reserved steps is not equal to negative one, right, because if we said if it was a question mark, we said equal to negative one. So it's not equal to negative one. That's actually just copy all the values here and do it again. And then just return at the end. Right, just return at the end here. And what we're going to do is we're going to have like cases for each of these. Okay, so let's just do a switch statement here of reserved steps. And then we have case one. Actually, let's see what our cases was. So case zero is up case one is down case left is case two is left case three is right. So we're going to go back up here. Case one, case two, case one break, case two, break, case three, break. And let's see zero one, two, three. Yeah, so we're going to have case zero also. Okay, and then we're just going to copy this stuff. So going up is this put this here, we're going to put zero is upright. Zero is up. Yeah, so we're going to put this in here. Okay, one is down. Yeah, do one is down. So we're going to copy cut this pretty here. Actually, that was not a good idea. This one, this is down. So that's one. And then two would be was to to his left. So we're going to put left here to and then the last one is right. Okay, okay, so now that we said all the all the states and what we could do for each of them. And we also set the visited at the end be true, false. And yeah, let's just run it. Okay, so we get 201. And that's the right value here. So let's actually just submit. And we get a seed. So yeah, that's basically just the code is just pruning until we finally get the answer. And then we have to set our states to be make sure that we're actually getting the right values and reserving those spots. But yeah, I hope you guys enjoyed this video. Rate com subscribe. That's pretty much the gist of it. Peace.