It spread to Berkeley after Dr. Winkler bumped into Dr. Elwyn Berlekamp, a professor in the Berkeley math department, at a conference in New Orleans in January.

''I told him about the problem and next thing I knew he was leaving messages on my hotel phone saying, 'Great problem, haven't gotten it yet,' then finally, 'I got it,' '' Dr. Winkler said. ''I thought, with his knowledge of coding theory, he'd find that approach, and he didn't disappoint me.''

Dr. Berlekamp, a coding theory expert, said he figured out the solution to the simplest case in about half an hour, but he saw the coding theory connection only while he was falling asleep that night.

''If you look at old things that you know from a different angle, sometimes you can't see them,'' he said.

The first thing Dr. Berlekamp saw was that in the three-player case, it is possible for the group to win three-fourths of the time.

Three-fourths of the time, two of the players will have hats of the same color and the third player's hat will be the opposite color. The group can win every time this happens by using the following strategy: Once the game starts, each player looks at the other two players' hats. If the two hats are different colors, he passes. If they are the same color, the player guesses his own hat is the opposite color.

This way, every time the hat colors are distributed two and one, one player will guess correctly and the others will pass, and the group will win the game. When all the hats are the same color, however, all three players will guess incorrectly and the group will lose.