Suppose that instead of the hexagonal pattern, we had the 127 cells laid out as three linesStart with 42, 43 and 42 cells.
So what?
There, the 41 cells without communication to the exterior would necessarily be0 hallways, otherwise, either the hallway would be two or more disconnected sections or some cell would not be near a hallway. Plus some other exterior cell would also need to be a hallway in order to connect it toAdd the exterior, making up a minimum of 42 hallways cellsentrance in some edge.
But duh! It is not a three line pattern, it is an hexagonal pattern!
As long as you can fit it the hallways, it doesn't matter This will make 4 rooms accessible. If you can't fitstart in a corner instead, it would justthis will give you 1 less accessible room and will only make things worseyour solution less optimal.
Taking the three line pattern, we could make the entire middle line, with the exception of one of the cells in the very end asStarting add new hallways. We could also exchange the entrance for some cell in the other two lines connected to an existing hallway. But in any way, there would be a cellEvery time you do that we should call as, the "end-of-hallway cell" and a non-hallway cell covered byprevious hallway will give access to 1 less room than it that we should call as "the last room"used to do. SurelyEvery time you add a new hallway, at most 3 new rooms become accessible, but if thethat new hallway bifurcates (as it does), there wouldwill not be multiple cells eligible for being entitled as sucha dead-end, but we can choose any one of those new rooms will be turned to the multiple candidates andnext hallway, so, with the result isexception of the same.
Most commonlyentrance, each hallway cell connectsection gives access to other two hallway cells. Some hallway cells are bifurcations having three other hallway cells as neighbors and some are dead-ends, having only oneat most 2 rooms. However, every time we add a bifurcationAlso, we also addto get a new dead-end since cycles are inherent suboptimal. Hallway cells with four or more neighbors also being hallways can't be optimal since this would only increase the density of hallway cellsthat gives access to 3 new rooms, which is what we wantyou will also need to decreasemake a bifurcation that gives you 1 less room, so you still get 2 new reachable rooms per hallway in the best case.
So, with the exception of the end-of-hallway cell andThis means that the entrancebest you can get is 2 new accessible rooms per hallway, this impliesand 1 more than that inbecause the optimal case,entrance started giving 4. So the average number of neighbors that are hallway cells to other hallway cells neighbors is exactly two. Further, disregarding only "the last room", exactly a thirdone more than the triple of the cells must benumber of hallways, otherwise some room would be inaccessible.
We have 127 cells. OneOr put in the other way around, the number of themhallways is "the last room", so we have 126 other cells. Get a third of that and we have 42the number of cells minus one. Hence, $(127 - 1) \div 3 = 42$. So this is the best solution possible.
Thanks to Kruga for the sketch of the proof in a comment and also for Daniel Wagner that also gave something similar in another comment.