import
java.util.List;
import
java.util.ArrayList;
/**
* □ + □ = □
* □ - □ = □
* □ * □ = □
* □ / □ = □
*
* 将 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 13 填入上面方框,不能重复使用,使等式成立。
*
*
* @author 1234
*
*/
public
class
_dfs深搜
01
{
static
boolean
[] flags =
new
boolean
[
14
];
static
boolean
[] fuhao =
new
boolean
[
4
];
static
List<ArrayList<Integer>> result =
new
ArrayList<>();
public
static
void
dfs(ArrayList<Integer> arr) {
if
(arr.size() ==
12
) {
System.out.println(
"in "
+ arr);
List<Integer> arr1 =
new
ArrayList<>();
arr1.addAll(arr);
result.add((ArrayList<Integer>) arr1);
return
;
}
for
(
int
i =
1
; i <=
13
; i++) {
if
(!flags[i] && i !=
11
) {
flags[i] =
true
;
for
(
int
j =
1
; j <=
13
; j++) {
if
(!flags[j] && i !=
11
) {
flags[j] =
true
;
if
(!fuhao[
0
] && i + j <=
13
&& !flags[i+j]) {
fuhao[
0
] =
true
;
flags[i+j] =
true
;
arr.add(i);
arr.add(j);
arr.add(i+j);
dfs(arr);
arr.remove(
new
Integer(i));
arr.remove(
new
Integer(j));
arr.remove(
new
Integer(i + j));
fuhao[
0
] =
false
;
flags[i+j] =
false
;
}
if
(fuhao[
0
] && !fuhao[
1
] && i - j >
0
&& !flags[i-j]) {
fuhao[
1
] =
true
;
flags[i-j] =
true
;
arr.add(i);
arr.add(j);
arr.add(i-j);
dfs(arr);
arr.remove(
new
Integer(i));
arr.remove(
new
Integer(j));
arr.remove(
new
Integer(i-j));
fuhao[
1
] =
false
;
flags[i-j] =
false
;
}
if
(fuhao[
0
] && fuhao[
1
] && !fuhao[
2
] && i * j <=
13
&& !flags[i*j]) {
fuhao[
2
] =
true
;
flags[i*j] =
true
;
arr.add(i);
arr.add(j);
arr.add(i*j);
dfs(arr);
arr.remove(
new
Integer(i));
arr.remove(
new
Integer(j));
arr.remove(
new
Integer(i*j));
fuhao[
2
] =
false
;
flags[i*j] =
false
;
}
if
(fuhao[
0
] && fuhao[
1
] && fuhao[
2
] && !fuhao[
3
] && i / j >
0
&& i % j ==
0
&& !flags[i/j]) {
fuhao[
3
] =
true
;
flags[i/j] =
true
;
arr.add(i);
arr.add(j);
arr.add(i/j);
dfs(arr);
arr.remove(
new
Integer(i));
arr.remove(
new
Integer(j));
arr.remove(
new
Integer(i/j));
fuhao[
3
] =
false
;
flags[i/j] =
false
;
}
flags[j] =
false
;
}
}
flags[i] =
false
;
}
}
return
;
}
public
static
void
main(String[] args) {
long
startTime = System.currentTimeMillis();
dfs(
new
ArrayList<Integer>());
System.out.println(result.size());
System.out.println(result);
System.out.println(
"共花时: "
+ (System.currentTimeMillis() - startTime) +
" 毫秒"
);
}
}