Bonjour à tous, j'essaye actuellement de développer un solveur pour le jeu du compte est bon. J'ai commencé par faire un algo et je l'ai testé en Java. Cela ne va pas du tout.
Le but dans mon algo est de construire un arbre avec pour chaque nœud, une solution avec ses informations. A chaque fois que je crée un nœud je le compare avec la "meilleure" solution actuelle (la solution la plus proche avec le moins d'opération).
Pour cela j'utilise une fonction récursive. Le problème c'est que je ne l'appel pas avec les bonnes informations.
Voici mon programme pour le moment:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
 
import java.io.*;
import java.util.ArrayList;
 
class Information {
	protected long P1;
	protected char operateur;
	protected long P2;
	protected long resultat;
	protected int niveau;
	protected ArrayList<Long> plaquettes;
	protected int nbplaquettes;
 
	public Information(long P1, char operateur, long P2, long resultat, int niveau, ArrayList<Long> plaquettes, int nbplaquettes) {
		this.P1 = P1;
		this.operateur = operateur;
		this.P2 = P2;
		this.resultat = resultat;
		this.niveau = niveau;
		this.plaquettes = plaquettes;
		this.nbplaquettes = nbplaquettes;
	}
	public Information(int niv, ArrayList<Long> pla, int nbpla) {
		this.P1 = -1;
		this.operateur = ' ';
		this.P2 = -1;
		this.resultat = 
		this.niveau = niv;
		this.plaquettes = pla;
		this.nbplaquettes = nbpla;
	}
	public long getP1() {
		return P1;
	}
	public char getOperateur() {
		return operateur;
	}
	public long getP2() {
		return P2;
	}
	public long getResultat() {
		return resultat;
	}
	public int getNiveau() {
		return niveau;
	}
	public ArrayList<Long> getPlaquettes() {
		return plaquettes;
	}
	public int getNbPlaquettes() {
		return nbplaquettes;
	}
 
}
 
class Noeud {
	protected Information info;
	//protected ArrayList<Noeud> fils;
	protected Noeud pere;
 
	/*public Noeud(Information info, ArrayList fils, Noeud pere){
		this.info = info;
		this.fils = fils;
		this.pere = pere;
	}*/
	public Noeud(Information info, Noeud pere){
		this.info = info;
		this.pere = pere;
	}
	public Noeud(Information infor) {
		this.info = infor;
		//this.fils = null;
		this.pere = null;
	}
	public Noeud() {
		this.info = null;
		//this.fils = null;
		this.pere = null;
	}
	public Noeud setInfo(Information i) {
		this.info.P1 = i.P1;
		this.info.operateur = i.operateur;
		this.info.P2 = i.P2;
		this.info.resultat = i.resultat;
		this.info.niveau = i.niveau;
		this.info.plaquettes = i.plaquettes;
		this.info.nbplaquettes = i.nbplaquettes;
		return this;
	}
	public Noeud getPere() {
		return pere;
	}
	public Information getInfo() {
		return info;
	}
	/*public ArrayList getFils() {
		return fils;
	}*/
}
 
public class Solution {
 
	public static long pow(long x, long y) {
		long res = x;
		for(int i=0; i<y; i++) {
			res=res*x;
		}
		return res;
	}
 
	public static long Operation(long N1, long N2, int ope) {
		if(ope == 0) {
			return(N1+N2); 
		}
		else{
			if(ope == 1) {
				if(N1>N2) {
					return(N1-N2); 
				} 
				else {
					return(N2-N1);
				}
			}
			else{
				if(ope == 2) {
					return(N1*N2); 
				}
				else{
					if(ope == 3) {
						if((N1>N2) && (N1%N2==0) && (N2 != 0)) {
							return(N1/N2); 
						}
						else if((N2>N1) && (N2%N1==0) && (N1 != 0)) {
							return(N2/N1);
						}
					}
					else{
						if(ope == 4) {
							return(pow(N1,N2));
						}
						else{
							if(ope == 5) {
								return(pow(N2,N1)); 
							}
							else{
								if(ope == 6) {
									if(N2 != 0) {
										return(N1%N2);
									} 
								}
								else{
									if(ope == 7) {
										if(N1 != 0) {
											return(N2%N1);
										}
									}
								}
							}
						}
					}
				}
			}
		}
		return(-1);
	}
 
	public static void Affiche(Noeud best) {
		int niv = best.info.niveau;
		while(niv>1) {
			System.out.println("op1: "+best.info.getP1()+"op2: "+best.info.getP2()+"resultat: "+best.info.getResultat());
			best.info.P1 = best.pere.info.getP1();
			best.info.operateur = best.pere.info.getOperateur();
			best.info.P2 = best.pere.info.getP2();
			niv--;
		}
	}
 
	static int i=0;
	static int j=0;
	static int o=0;
	static int calc = 0;
	static int niveau = 1;
 
	public static void Find(Noeud N, Noeud best, int cible) {
		//System.out.println("Je suis dans Find");
 
		long res = 0;
		char operateurs [] = {'+','-','*','/','^','%'};
		long test = 0;
		long test2 = 0;
		long op1 = 0;
		long op2 = 0;
		int nbplaques = 0;
		char op;
		long tmp1 = 0;
		long tmp2 = 0;
 
 
		tmp1 = best.info.getResultat();
		//System.out.println("---CIBLE:"+cible);
		//System.out.println("---TMP1:"+tmp1);
		test = tmp1 - cible;		
		if(test<0){
			test=test*(-1);
		}		
		tmp2 = N.info.getResultat();
		//System.out.println("---TMP2:"+tmp2);
		test2 = tmp2 - cible;
 
		if(test2<0){
			//System.out.println("JE SUIS NEGATIF");
			test2=test2*(-1);
		}
		//System.out.println("TEST2:"+test2);
 
		if(test2 < test) {
			//System.out.println("N.P1 INFERIEUR A BEST");
			best.info.P1 = N.info.getP1();
			best.info.operateur = N.info.getOperateur();
			best.info.P2 = N.info.getP2();
			best.info.resultat = N.info.getResultat();
			best.info.niveau = N.info.getNiveau();
			best.info.plaquettes = N.info.getPlaquettes();
		}
		else {
			if(test2 == test) {
				if(N.info.niveau < best.info.niveau) {
					best.info.P1 = N.info.getP1();
					best.info.operateur = N.info.getOperateur();
					best.info.P2 = N.info.getP2();
					best.info.resultat = N.info.getResultat();
					best.info.niveau = N.info.getNiveau();
					best.info.plaquettes = N.info.getPlaquettes();
				} 
			}
		}
 
 
		int max = N.info.getNbPlaquettes();
		//System.out.println("---///"+max+"---");
 
 
 
 
 
 
		if(max>=2) {
			ArrayList<Long> tmp = new ArrayList<Long>();
			tmp.addAll(N.info.getPlaquettes());
			//System.out.println(tmp.get(1));
 
			tmp.remove(0);
 
			Information infom = new Information(0,' ',0,100000,6,N.info.getPlaquettes(),1);
			Noeud N1 = new Noeud(infom);
 
			for(i=0; i<max-1; i++) {
				for(j=i+1; j<max; j++) {					
					for(o=0; o<8; o++) {
						int actuelle = niveau + 1;						
						res = Operation(N.info.getPlaquettes().get(0), N.info.getPlaquettes().get(1), o);
						tmp.add(res);
						tmp.remove(0);						
						op1 = N.info.getPlaquettes().get(0);
						//System.out.println(op1);
						if(o==4 || o==5) {
							op = operateurs[4];
						}
						else {
							if (o==6 || o==7) {
								op = operateurs[5];
							}
							else {
								op = operateurs[o];
							}
						} 
						op2 = N.info.getPlaquettes().get(1);
						nbplaques = N.info.getNbPlaquettes()-1;
						Information infor = new Information(op1,op,op2,res,actuelle,tmp,nbplaques);
						//System.out.println(infor.P1);
						N1.setInfo(infor);
						//System.out.println("-----------------------");
						System.out.println("NIVEAU: "+best.info.getNiveau());
						System.out.println("Operateur1: "+N1.info.getP1()+" Operation: "+N1.info.getOperateur()+" Operateur2: "+N1.info.getP2()+" Resultat: "+N1.info.getResultat());
						//System.out.println(N1.info.getP1());
						N1.pere = N;								
						Find(N1, best, cible);
						calc ++;
						//System.out.println("----Calculs----"+calc);
					}
				}
			}
 
			//Affiche(best);
		}		
		if((i==max-1) && (j==max) && (o==7))  {
			//System.out.println("Je vais dans Affiche");
 
			Affiche(best);
		}
	}
 
	public static void main(String[] args) {
 
		ArrayList<Long> init = new ArrayList<Long>();
		//ArrayList fils = new ArrayList();
		long tmp = 0;
		tmp = 10;
		init.add(tmp);
 
		tmp = 5;
		init.add(tmp);
 
		tmp = 7;
		init.add(tmp);
 
		tmp = 2;
		init.add(tmp);
 
		tmp = 25;
		init.add(tmp);
 
		tmp = 9;
		init.add(tmp);
 
 
		int totalPlaquettes = 6;
		int target = 859;
		int valeurMax = 100000;
 
		Information infor = new Information(0,' ',0,0,1,init,totalPlaquettes);
		Information infom = new Information(0,' ',0,valeurMax,6,init,1);			
		//fils.add(meilleur);
		Noeud racine = new Noeud(infor,null);
		//System.out.println(racine.info.getPlaquettes().get(1));
		Noeud meilleur = new Noeud(infom,racine);
 
		Find(racine, meilleur, target);
	}
}
Donc, que je résume, avec ce programme, je gère mal mes listes de plaquettes restantes pour chaque noeud et je ne fais l'appel de la fonction Find au bon endroit.

Je vous serez donc reconnaissant de bien vouloir me venir en aide.

Merci d'avance