DFS JAVA 模板

来源:互联网 发布:懒人做饭知乎 编辑:程序博客网 时间:2024/05/21 05:40

很好用

import java.awt.*;import java.util.ArrayList;/** * Created by zhangzexiang on 2016/8/9. */public class SmartRorbot_1048 {static int maxn=100;    static boolean vst[][]  = new boolean[maxn][maxn]; // 访问标记    static int map[][] = new int[maxn][maxn]; // 坐标范围    ArrayList<Point> dir = new ArrayList<>(); //方向向量,(x,y)周围的四个方向   public static boolean CheckEdge(int x,int y) //边界条件和约束条件的判断    {        if(!vst[x][y] && ) // 满足条件        return true;    else // 与约束条件冲突        return false;    }    void dfs(int x,int y)    {        vst[x][y]=true; // 标记该节点被访问过        if(map[x][y]==) // 出现目标态G        {        ...... // 做相应处理            return;        }        for(int i=0; i<4; i++)        {            if(CheckEdge(x+dir[i][0],y+dir[i][1])) // 按照规则生成下一个节点                dfs(x+dir[i][0],y+dir[i][1]);        }        return; // 没有下层搜索节点,回溯    }    public static void main(String[] args)    {    }}
0 0