1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
| static void Main(string[] args) { Console.WriteLine("transports 开始");
//判断是否合法状态 bool ValidEndState(int state) => state != 3 && !(state >= 6 && state <= 9) && state != 12;
//判断人是否和羊在一起 bool IsPeopleWithSheep(int state) => state / 2 % 2 == 0 && state < 8 || state / 2 % 2 == 1 && state >= 8;
//判断人是否和狼在一起 bool IsPeopleWithWolf(int state) => !(state >= 4 && state <= 11);
//判断人是否和菜在一起 bool IsPeopleWithGreens(int state) => state % 2 == 0 && state < 8 || state % 2 == 1 && state >= 8;
//运输人和青菜 int TransferPeopleWithGreens(int i) => i ^ 9;
//运输人和羊 int TransferPeopleWithSheep(int i) => i ^ 10;
//运输人和狼 int TransPeopleWithWolf(int i) => i ^ 12;
//运输人和狼 int TransPeople(int i) => i ^ 8; Transport transport1 = new Transport("人和菜", IsPeopleWithGreens, ValidEndState, TransferPeopleWithGreens); Transport transport2 = new Transport("人和羊", IsPeopleWithSheep, ValidEndState, TransferPeopleWithSheep); Transport transport3 = new Transport("人和狼", IsPeopleWithWolf, ValidEndState, TransPeopleWithWolf); Transport transport4 = new Transport("人", state => true, ValidEndState, TransPeople); List<Transport> transports = new List<Transport> {transport1, transport2, transport3, transport4}; int tempState = 0; int EndState = 15; while (tempState != EndState) { foreach (var actionItem in transports) { tempState = actionItem.Transfer(tempState); } }
Console.WriteLine("transports 结束"); Console.ReadKey(); }
|