How to calculate which tiles to affect in tile based game?

Hi all, sorry if this is fairly simple, but I’ve been working on it for a week or so now and can’t seem to find a solution based on my limited programming skills.

So in the game, a bunch of tiles are instantiated in grid formation, labeled with their x and y location, added to an array, and then player can move from tile to tile one at a time.

Once you reach an edge tile, the game should calculate which overall section/collection of tiles from the first edge tile you left to the final edge tile you landed on, has the smaller number of tiles (or other similar queries). It should then change these tiles to whatever you’d like (for example, if all the tiles represent dirt, the new smaller section will change to grass tiles). So it isn’t just the ones you’ve collided with, but each tile you’ve collided with closest edge neighbor and all the tiles in between.

It’s a little hard to explain so I made an example gif here of the process, I hope it makes sense.

My problem is I’m struggling to find a way for the game to understand which tiles to affect. I’ve tried recording each tile you land on, then having all those tiles calculate which edge is closer, collect those tiles, and affect the tiles based on that. But the problem is if you walk in an irregular path, or occasionally close to one edge, it will affect tiles outside of the collective area, instead of keeping it all inside.

An example of this is here with a simple path, and with a more complex irregular path, the blue shows how my current attempts calculates it. The final image in green is the intended behavior.

I’m kind of losing my mind and I’m sure there’s a simple way to do this that I just can’t get to. I’d love any / all suggestions on how to possible accomplish this? Thank you all so much in advance for giving this question a look. I’m using C#.

Use a an array int[,] map to flag the walked tiles with 1 and 0 the rest. After you finish the walk by reaching an edge you can run a flood algorithm to figure your regions, save em on an array and compare the Counts. After that you can select the one you want and do whatever to it.
For flood algorthm check the Procedural Cave Generator Tutorial (GetRegion()). The hard part on that is how you gonna know from where to start the algorithm run an ,kinda, efficient way would be to start with the (0,0) tile get the region it produce. Then get the next edge tile check if it is contained in the region you already have, if not run the algorithm with that tile as starting point, now you have a second region, note that region would be different type from your first one (if first one is 0 as unwalked this one will be type 1, your path). At that point you have the one unwalked region and the walked one. Continue with the next edge tile. Check again if it is contained by the other two regions if not run again the algorithm. As soon you have 3 regions you are done. Compare the 2 unwalked ones figure which one you want to change. For a small map you shouldn’t have any proble with performance, although for a large map it will be a bit slow and require more memory. Hope that helps. (Note: There are probably more efficient ways to solve this.)