Test

来源:互联网 发布:推荐系统算法模型 编辑:程序博客网 时间:2024/05/16 18:09
package com.company;import java.util.HashMap;import java.util.Map;/** *  */public class CountAreaTest {    public static void main(String[] args) {        int solution = solution();        System.out.println(solution);    }    public static int solution() {        Map<Integer, Map<Integer, Boolean>> map = new HashMap<>();        return helper(0, 0, map);    }    public static int helper(int x, int y, Map<Integer, Map<Integer, Boolean>> map) {        int up = 0, down = 0, left = 0, right = 0;        if (map.containsKey(x)) {            if (map.get(x).containsKey(y)) {                return 0;            } else {                map.get(x).put(y, true);            }        } else {            Map<Integer, Boolean> temp = new HashMap<>();            temp.put(y, true);            map.put(x, temp);        }        if (move(0,x - 1)) {//0            up = helper(x - 1, y, map);        }        if (move(1,x + 1)) {//1            down = helper(x + 1, y, map);        }        if (move(2,y - 1)) {//2            left = helper(x, y - 1, map);        }        if (move(3,y + 1)) {//3            right = helper(x, y + 1, map);        }        return up + down + left + right + 1;    }    //移动的函数    public static boolean move(int i, int direction) {        int xLeft = -1, xRight = 4;        int yLeft =-3, yRight = 7;        boolean isOK = true;        switch (i) {            case 0:                if (direction >= xLeft) {                    isOK=true;                } else {                    isOK=false;                }                break;            case 1:                if (direction < xRight) {                    isOK=true;                } else {                    isOK=false;                }                break;            case 2:                if (direction >= yLeft) {                    isOK= true;                } else {                    isOK=false;                }                break;            case 3:                if (direction < yRight) {                    isOK=true;                } else {                    isOK= false;                }                break;        }        return isOK;    }}