01
02
03
04
05
06
07
08
09
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
|
{ 输入箱子的容积;
输入物品种数n;
按体积从大到小顺序,输入各物品的体积;
预置已用箱子链为空;
预置已用箱子计数器box_count为 0 ;
for ( i = 0 ;i { 从已用的第一只箱子开始顺序寻找能放入物品i 的箱子j;
if (已用箱子都不能再放物品i)
{ 另用一个箱子,并将物品i放入该箱子;
box_count + + ;
}
else
将物品i放入箱子j;
}
}
|
001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
|
# include
# include
typedef struct ele
{ int vno;
struct ele * link;
} ELE;
typedef struct hnode
{ int remainder;
ELE * head;
Struct hnode * next;
} HNODE;
void main ( )
{ int n , i , box_count , box_volume , * a;
HNODE * box_h , * box_t , * j;
ELE * p , * q;
Printf ( “输入箱子容积\n” ) ;
Scanf ( “%d” , & box_volume ) ;
Printf ( “输入物品种数\n” ) ;
Scanf ( “%d” , & n ) ;
A = ( int * ) malloc ( sizeof ( int ) * n ) ;
Printf ( “请按体积从大到小顺序输入各物品的体积:” ) ;
For ( i = 0 ;i Box_h = box_t = NULL;
Box_count = 0 ;
For ( i = 0 ;i { p = ( ELE * ) malloc ( sizeof ( ELE ) ) ;
p - > vno = i;
for ( j = box_h;j! = NULL;j = j - > next )
if ( j - > remainder > = a[i] ) break;
if ( j = = NULL )
{ j = ( HNODE * ) malloc ( sizeof ( HNODE ) ) ;
j - > remainder = box_volume - a[i];
j - > head = NULL;
if ( box_h = = NULL ) box_h = box_t = j;
else box_t = boix_t - > next = j;
j - > next = NULL;
box_count + + ;
}
else j - > remainder - = a[i];
for ( q = j - > next;q! = NULL & & q - > link! = NULL;q = q - > link ) ;
if ( q = = NULL )
{ p - > link = j - > head;
j - > head = p;
}
else
{ p - > link = NULL;
q - > link = p;
}
}
printf ( “共使用了%d只箱子”,box_count ) ;
printf ( “各箱子装物品情况如下:” ) ;
for ( j = box_h , i = 1 ;j! = NULL;j = j - > next , i + + )
{ printf ( “第% 2 d只箱子,还剩余容积% 4 d,所装物品有;\n” , I , j - > remainder ) ;
for ( p = j - > head;p! = NULL;p = p - > link )
printf ( “% 4 d” , p - > vno + 1 ) ;
printf ( “\n” ) ;
}
}
|
001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
|
# include
int delta_i[ ] = { 2 , 1 , -1 , -2 , -2 , -1 , 1 , 2 } ;
int delta_j[ ] = { 1 , 2 , 2 , 1 , -1 , -2 , -2 , -1 } ;
int board[ 8 ][ 8 ];
int exitn ( int i , int j , int s , int a[ ] )
{ int i 1 , j 1 , k , count;
for ( count = k = 0 ;k < 8 ;k + + )
{ i 1 = i + delta_i[ ( s + k ) % 8 ];
j 1 = i + delta_j[ ( s + k ) % 8 ];
if ( i 1 > = 0 & & i 1 < 8 & & j 1 > = 0 & & j 1 < 8 & & board[I 1 ][j 1 ] = = 0 )
a[count + + ] = ( s + k ) % 8 ;
}
return count;
}
int next ( int i , int j , int s )
{ int m , k , mm , min , a[ 8 ] , b[ 8 ] , temp;
m = exitn ( i , j , s , a ) ;
if ( m = = 0 ) return – 1 ;
for ( min = 9 , k = 0 ;k { temp = exitn ( I + delta_i[a[k]] , j + delta_j[a[k]] , s , b ) ;
if ( temp { min = temp;
kk = a[k];
}
}
return kk;
}
void main ( )
{ int sx , sy , i , j , step , no , start;
for ( sx = 0 ;sx < 8 ;sx + + )
for ( sy = 0 ;sy < 8 ;sy + + )
{ start = 0 ;
do {
for ( i = 0 ;i < 8 ;i + + )
for ( j = 0 ;j < 8 ;j + + )
board[i][j] = 0 ;
board[sx][sy] = 1 ;
I = sx; j = sy;
|