9.22蚂蚁研发笔试
第一题记忆化搜索为啥直接是0%
鼠鼠我直接自闭了
import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static int a, b, c;
public static boolean[] vis = new boolean[105];
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int t = in.nextInt();
Arrays.fill(vis, true);
while (t > 0) {
t--;
int n, m, k;
a = 0;
b = 0;
c = 0;
n = in.nextInt();
m = in.nextInt();
k = in.nextInt();
boolean res = dfs(100, n, m, k);
if (res) {
System.out.println("YES");
System.out.println(a + " " + b + " " + c);
} else {
System.out.println("NO");
}
}
}
public static boolean dfs(int goal, int n, int m, int k) {
boolean f1 = false;
boolean f2 = false;
boolean f3 = false;
if(!vis[goal]){
return false;
}
if (goal == 0) {
return true;
}
if (goal > 0 && goal < n && goal < m && goal < k) {
vis[goal] = false;
return false;
}
if (goal >= n && n > 0) {
a++;
f1 = dfs(goal - n, n, m, k);
if (f1) {
return true;
} else {
a--;
}
}
if (goal >= m && m > 0) {
b++;
f2 = dfs(goal - m, n, m, k);
if (f2) {
return true;
} else {
b--;
}
}
if (goal >= k && k > 0) {
c++;
f3 = dfs(goal - k, n, m, k);
if (f3) {
return true;
} else {
c--;
}
}
vis[goal] = false;
return false;
}
}
鼠鼠我直接自闭了
import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static int a, b, c;
public static boolean[] vis = new boolean[105];
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int t = in.nextInt();
Arrays.fill(vis, true);
while (t > 0) {
t--;
int n, m, k;
a = 0;
b = 0;
c = 0;
n = in.nextInt();
m = in.nextInt();
k = in.nextInt();
boolean res = dfs(100, n, m, k);
if (res) {
System.out.println("YES");
System.out.println(a + " " + b + " " + c);
} else {
System.out.println("NO");
}
}
}
public static boolean dfs(int goal, int n, int m, int k) {
boolean f1 = false;
boolean f2 = false;
boolean f3 = false;
if(!vis[goal]){
return false;
}
if (goal == 0) {
return true;
}
if (goal > 0 && goal < n && goal < m && goal < k) {
vis[goal] = false;
return false;
}
if (goal >= n && n > 0) {
a++;
f1 = dfs(goal - n, n, m, k);
if (f1) {
return true;
} else {
a--;
}
}
if (goal >= m && m > 0) {
b++;
f2 = dfs(goal - m, n, m, k);
if (f2) {
return true;
} else {
b--;
}
}
if (goal >= k && k > 0) {
c++;
f3 = dfs(goal - k, n, m, k);
if (f3) {
return true;
} else {
c--;
}
}
vis[goal] = false;
return false;
}
}
全部评论
为啥写这么复杂呢,暴力也可以过的
三重for循环
相关推荐
10-07 10:41
天津理工大学中环信息学院 前端工程师 点赞 评论 收藏
分享
11-12 11:38
山东大学 嵌入式工程师 点赞 评论 收藏
分享