我正在尝试建立一个程序,比较15个游戏中算法BFS,DFS,A *(有两个启发式)的笔画数。
我的计数器对BFS和DFS的两个计数器阵列以及两个A *计数相同的结果。然而,我实际上正在使用来自main(类Project)的四个不同数组,并且我为这些笔划分配了四个不同的变量。
在我看来,不正确的代码部分是一个while循环,它尽可能地探索顶点的子(对于BFS)或发现每个后续节点(对于BFS)。最重要的区别是代码的最后一行,它是frontier.push(child);,对于DFS或frontier.add(child);对于BFS。
每次进入循环时,笔画数都会递增
number_of_strokes_DFS+=1;
或
number_of_strokes_BFS+=1;
当我们找到最终状态时,我们将结果添加到笔画数的数组中:
array_number_of_strokes_dfs.add(number_of_strokes_DFS);
或
array_number_of_strokes_bfs.add(number_of_strokes_BFS);
最后是有罪的代码(实际上只有DFS,就BFS来说,除了最后一行之外真的很相似)。
while(!frontier.isEmpty()){
number_of_strokes_DFS+=1;
if(System.currentTimeMillis()-startTime>10000)break;
// We remove the current state from the frontier
current_state = frontier.pop();
// We get all possible actions from the current state
actions = current_state.getActions();
// We add the current state to already explored nodes
explored_nodes.add(current_state);
//System.out.println(current_state);
path.add(current_state);
// we found the goal
if(goal_test(current_state)){
/*for(State visited :path){
System.out.println(visited);
}*/
array_number_of_strokes_dfs.add(number_of_strokes_DFS);
System.out.println("nombres de coups DFS"+number_of_strokes_DFS);
number_of_strokes_DFS=0;
return current_state;
}
// We create every child
for (Action action : actions){
//System.out.println("action : " + action);
// we get a child from the execution of the current_state
State child = current_state.execute(action);
//System.out.println("we executed the action");
if(!explored_nodes.contains(child)&&!frontier.contains(child)){
// This child not being already explored nor int the frontier we add it to the last one
frontier.push(child);
}
}
}
array_number_of_strokes_dfs.add(-1);
return finalState;
}
这就是实际问题,因为当我让array_number_of_strokes_dfs.add(number_of_strokes_DFS);,例如,我总是得到与数组中的BFS相同的结果。它可能会发生一次,但不是每次都发生! 而如果我加零
array_number_of_strokes_dfs.add(0);
我确实看到了差异......
你有什么想法吗?
结果如下:
strokes BFS : [3, 27, 27, 26, 26, 2631, 7]
strokes DFS[3, 27, 27, 26, 26, 2631, 7]
答案 0 :(得分:2)
如果public class StartActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start);
AuthController.initAuth(new ResultListener<AuthController>() {
@Override
public void onResult(AuthController authController) {
Intent intent;
if ( authController.getUser() != null ) {
intent = new Intent(getApplicationContext(), MainNavigationActivity.class);
} else {
intent = new Intent(getApplicationContext(), LoginActivity.class);
}
startActivity(intent);
finish();
}
});
}
}
public class AuthController {
private static AuthController authController;
private User user;
public User getUser() {
return user;
}
public static AuthController getInstance() {
if (authController == null) {
throw new IllegalStateException("The auth system is not initializet. Restart the app.");
}
return authController;
}
public static void initAuth(final ResultListener<AuthController> authListener) {
final AuthController authController = new AuthController();
FirebaseAuth mAuth = FirebaseAuth.getInstance();
mAuth.addAuthStateListener(new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
final FirebaseUser authUser = firebaseAuth.getCurrentUser();
if (authUser != null) {
String authUserId = authUser.getUid();
FirebaseDatabase firebaseDatabase = FirebaseDatabase.getInstance();
firebaseDatabase.getReference("users").child(authUserId).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
authController.user = dataSnapshot.getValue(User.class);
AuthController.authController = authController;
authListener.onResult(authController);
}
@Override
public void onCancelled(DatabaseError databaseError) {
authController.user = null;
AuthController.authController = authController;
authListener.onResult(authController);
}
});
} else {
authController.user = null;
AuthController.authController = authController;
authListener.onResult(authController);
}
}
});
}
}
是frontier
或类似内容,则Stack
与add
类似,因此您的push
实际上也在做深度 - 第一次搜索。如果你真的想在开头插入(如果你在每次迭代时BFS
元素想要做什么),你想要调用pop
(注意.add(0, elem)
- 索引at要插入的,而不是0
,以便它实际上插入到开头。