Jug Problem by C-language
Introduction There are many answers. Please think freely. This slide will provide only a flow of processes. And, this is just an example. It is one of the answers.
Overall Initialization Check Goal Status Y 繰り返し N Rule Execution Finish
Initialize In the initialize process, should be prepared. Variable (x,y), that means the water in the jugs. Array to record ”History of water in the jugs”. should be prepared.
Check Goal Status X=2 is the condition
Process of a rule Satisfy conditions of the rule? Calculate next jug status Move to next rule History check Yes Record current status to the history array, and back to the goal check. No Make all rules. Please don’t forget to display messages for recognize the process of the program.
Backtrack If any rule cannot be applied, it means that there are no next state from a current state. In this case, it should back the previous state by refering the history. A It can’t move next state from B. Back to state A and choose other branches. B
Miscellaneous “Missionaries and Cannibals” is almost same as the jug problem. (Conditions becomes a little complex and it need to add the rule for judgement whether missionaries are eaten by cannibals. ) Pointers and structs must not need. It is enough using for and while. Break and continue is very useful. (These may be must.)
advanced If you can, please consider to use “recursive” concept. The program will be elegant.