1
//
数据结构 -队列运用(农夫过河问题)
2 // 2009-6-23 by Larman Yuan C语言实现
3 // 用四位二进制数分别顺序表示农夫、狼、白菜和羊
4 // 用0表示在此岸 1表示在彼岸,那么初始状态为0000,终了状态为1111
5
6 #include " stdafx.h "
7 #include < stdlib.h >
8
9 #define MAXNUM 100
10
11 typedef int DataType;
12
13 struct SeqQueue
14 {
15 DataType q[MAXNUM];
16 int f,r; // f队头,r队尾
17 };
18 typedef struct SeqQueue * PSeqQueue;
19
20 PSeqQueue createEmptyQueue_seq( void )
21 {
22 PSeqQueue paqu;
23 paqu = (PSeqQueue)malloc( sizeof ( struct SeqQueue));
24 if (paqu == NULL)
25 {
26 printf( " Out of space!!\n " );
27 }
28 else
29 {
30 paqu -> f = 0 ;
31 paqu -> r = 0 ;
32 }
33 return (paqu);
34 }
35
36 int isEmptyQueue_seq(PSeqQueue paqu)
37 {
38 return (paqu -> f == paqu -> r);
39 }
40
41 // 是循环队列进队
42 void enQueue_seq(PSeqQueue paqu, DataType x)
43 {
44 if ((paqu -> r + 1 ) % MAXNUM == paqu -> f)
45 printf( " Full queue.\n " );
46 else
47 {
48 paqu -> q[paqu -> r] = x;
49 paqu -> r = (paqu -> r + 1 ) % MAXNUM;
50 }
51 }
52
53 void deQueue_seq(PSeqQueue paqu)
54 {
55 if (paqu -> f == paqu -> r)
56 printf( " Empty Queue.\n " );
57 else
58 paqu -> f = (paqu -> f + 1 ) % MAXNUM;
59 }
60
61 DataType frontQueue_seq(PSeqQueue paqu)
62 {
63 return (paqu -> q[paqu -> f]);
64 }
65
66 int farmer( int location)
67 {
68 return ( 0 != (location & 0x08 ));
69 }
70
71 int wolf( int location)
72 {
73 return ( 0 != ( location & 0x04 ));
74 }
75
76 int cabbage( int location)
77 {
78 return ( 0 != (location & 0x02 ));
79 }
80
81 int goat( int location)
82 {
83 return ( 0 != (location & 0x01 ));
84 }
85
86 int safe( int location)
87 {
88 if ((goat(location) == cabbage(location)) &&
89 (goat(location) != farmer(location)))
90 return ( 0 );
91 if ((goat(location) == wolf(location)) &&
92 (goat(location) != farmer(location)))
93 return ( 0 );
94 return ( 1 );
95 }
96
97 void framerRiverProblem()
98 {
99 int movers, location, newLocation;
100 int route[ 16 ]; // 记录已考虑状态的路径
101 PSeqQueue moveTo;
102 moveTo = createEmptyQueue_seq();
103 enQueue_seq(moveTo, 0x00 );
104 for ( int i = 0 ; i < 16 ; i ++ )
105 {
106 route[i] = - 1 ;
107 }
108 route[ 0 ] = 0 ;
109 while ( ! isEmptyQueue_seq(moveTo) && (route[ 15 ] == - 1 ))
110 {
111 location = frontQueue_seq(moveTo);
112 deQueue_seq(moveTo);
113 for (movers = 1 ; movers <= 8 ; movers <<= 1 ) // 农夫总是在移动,随农夫移动的也只能是与农夫同侧的东西
114 {
115 if (( 0 != (location & 0x08 )) == ( 0 != (location & movers)))
116 {
117 newLocation = location ^ ( 0x08 | movers);
118 if (safe(newLocation) && (route[newLocation] == - 1 ))
119 {
120 route[newLocation] = location;
121 enQueue_seq(moveTo, newLocation);
122 }
123 }
124 }
125
126 }
127 if (route[ 15 ] != - 1 )
128 {
129 printf( " The reverse path is:\n " );
130
131 for (location = 15 ; location >= 0 ; location = route[location])
132 {
133 printf( " The location is: %d \n " ,location);
134 if (location == 0 )
135 return ;
136 }
137 }
138 else
139 {
140 printf( " No solution.\n " );
141 }
142 }
143 int _tmain( int argc, _TCHAR * argv[])
144 {
145 framerRiverProblem();
146 return 0 ;
147 }
148
2 // 2009-6-23 by Larman Yuan C语言实现
3 // 用四位二进制数分别顺序表示农夫、狼、白菜和羊
4 // 用0表示在此岸 1表示在彼岸,那么初始状态为0000,终了状态为1111
5
6 #include " stdafx.h "
7 #include < stdlib.h >
8
9 #define MAXNUM 100
10
11 typedef int DataType;
12
13 struct SeqQueue
14 {
15 DataType q[MAXNUM];
16 int f,r; // f队头,r队尾
17 };
18 typedef struct SeqQueue * PSeqQueue;
19
20 PSeqQueue createEmptyQueue_seq( void )
21 {
22 PSeqQueue paqu;
23 paqu = (PSeqQueue)malloc( sizeof ( struct SeqQueue));
24 if (paqu == NULL)
25 {
26 printf( " Out of space!!\n " );
27 }
28 else
29 {
30 paqu -> f = 0 ;
31 paqu -> r = 0 ;
32 }
33 return (paqu);
34 }
35
36 int isEmptyQueue_seq(PSeqQueue paqu)
37 {
38 return (paqu -> f == paqu -> r);
39 }
40
41 // 是循环队列进队
42 void enQueue_seq(PSeqQueue paqu, DataType x)
43 {
44 if ((paqu -> r + 1 ) % MAXNUM == paqu -> f)
45 printf( " Full queue.\n " );
46 else
47 {
48 paqu -> q[paqu -> r] = x;
49 paqu -> r = (paqu -> r + 1 ) % MAXNUM;
50 }
51 }
52
53 void deQueue_seq(PSeqQueue paqu)
54 {
55 if (paqu -> f == paqu -> r)
56 printf( " Empty Queue.\n " );
57 else
58 paqu -> f = (paqu -> f + 1 ) % MAXNUM;
59 }
60
61 DataType frontQueue_seq(PSeqQueue paqu)
62 {
63 return (paqu -> q[paqu -> f]);
64 }
65
66 int farmer( int location)
67 {
68 return ( 0 != (location & 0x08 ));
69 }
70
71 int wolf( int location)
72 {
73 return ( 0 != ( location & 0x04 ));
74 }
75
76 int cabbage( int location)
77 {
78 return ( 0 != (location & 0x02 ));
79 }
80
81 int goat( int location)
82 {
83 return ( 0 != (location & 0x01 ));
84 }
85
86 int safe( int location)
87 {
88 if ((goat(location) == cabbage(location)) &&
89 (goat(location) != farmer(location)))
90 return ( 0 );
91 if ((goat(location) == wolf(location)) &&
92 (goat(location) != farmer(location)))
93 return ( 0 );
94 return ( 1 );
95 }
96
97 void framerRiverProblem()
98 {
99 int movers, location, newLocation;
100 int route[ 16 ]; // 记录已考虑状态的路径
101 PSeqQueue moveTo;
102 moveTo = createEmptyQueue_seq();
103 enQueue_seq(moveTo, 0x00 );
104 for ( int i = 0 ; i < 16 ; i ++ )
105 {
106 route[i] = - 1 ;
107 }
108 route[ 0 ] = 0 ;
109 while ( ! isEmptyQueue_seq(moveTo) && (route[ 15 ] == - 1 ))
110 {
111 location = frontQueue_seq(moveTo);
112 deQueue_seq(moveTo);
113 for (movers = 1 ; movers <= 8 ; movers <<= 1 ) // 农夫总是在移动,随农夫移动的也只能是与农夫同侧的东西
114 {
115 if (( 0 != (location & 0x08 )) == ( 0 != (location & movers)))
116 {
117 newLocation = location ^ ( 0x08 | movers);
118 if (safe(newLocation) && (route[newLocation] == - 1 ))
119 {
120 route[newLocation] = location;
121 enQueue_seq(moveTo, newLocation);
122 }
123 }
124 }
125
126 }
127 if (route[ 15 ] != - 1 )
128 {
129 printf( " The reverse path is:\n " );
130
131 for (location = 15 ; location >= 0 ; location = route[location])
132 {
133 printf( " The location is: %d \n " ,location);
134 if (location == 0 )
135 return ;
136 }
137 }
138 else
139 {
140 printf( " No solution.\n " );
141 }
142 }
143 int _tmain( int argc, _TCHAR * argv[])
144 {
145 framerRiverProblem();
146 return 0 ;
147 }
148
运行结果:
The reverse path is:
The location is: 15
The location is: 6
The location is: 14
The location is: 2
The location is: 11
The location is: 1
The location is: 9
The location is: 0