Use the event command to switch on/off the random battles.
An autorun on map enter to to check if puzzle is solved, switching off if not and then erase event for the autorun.
And an additional switch on on the puzzle itself as well as on every transfer out of the map.