Untitled

 avatarDarin
plain_text
a month ago
2.8 kB
1
Indexable
Never
int searchArea(int mMap[][], int mSeaLevel) {
	visited = new boolean[MAX_N + 2][MAX_N + 2];
	Queue<Coor> q = new LinkedList<>();
	for (int i = 0; i <= n + 1; i++) {
		for (int j = 0; j <= n + 1; j++) {
			if (i == 0 || i == n + 1 || j == 0 || j == n + 1) {
				q.add(new Coor(i, j));
				visited[i][j] = true;
			} else
				visited[i][j] = false;
		}
	}
	while (!q.isEmpty()) {
		Coor front = q.poll();
		for (int i = 0; i < 4; i++) {
			Coor rear = new Coor(front.x + dx[i], front.y + dy[i]);
			if (rear.x >= 1 && rear.x <= n && rear.y >= 1 && rear.y <= n) {
				if (!visited[rear.x][rear.y]
						&& mMap[rear.x][rear.y] < mSeaLevel) {
					q.add(rear);
					visited[rear.x][rear.y] = true;
				}
			}
		}
	}
	int returnValue = 0;
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= n; j++) {
			if (!visited[i][j]) {
				returnValue++;
			}
		}
	}
	return returnValue;
}

public int maxArea(int M, int mStructure[], int mSeaLevel) {
	int ret = -1;
	if (M == 1) {
		for (int i = 1; i <= n; i++) {
			for (int j = 1; j <= n; j++) {
				output[i][j] = input[i][j] + mStructure[0];
				ret = Math.max(ret, searchArea(output, mSeaLevel));
				output[i][j] = input[i][j];
			}
		}
		return ret;
	}

    int hashKey = calculateHash(M, mStructure); //tính mã băm cho cấu trúc cho trước
    
    ArrayList<Candidate> candidates = candidateMap.get(hashKey); //lấy danh sách các đối tượng Candidate từ HashMap
    
    if(candidates == null) return -1; //nếu không có khóa nào khớp thì trả về kết quả là âm

    for (Candidate structure : candidates) { //duyệt qua danh sách các đối tượng Candidate
        if (structure.isHorizontal) {
            int height = mStructure[0]
                    + (structure.isReverse ? input[structure.row][structure.col
                            + M - 1]
                            : input[structure.row][structure.col]);
            for (int i = 0; i < M; i++)
                output[structure.row][structure.col + i] = height;
            ret = Math.max(ret, searchArea(output, mSeaLevel));
            for (int i = 0; i < M; i++)
                output[structure.row][structure.col + i] = input[structure.row][structure.col
                        + i];
        } else {
            int height = mStructure[0]
                    + (structure.isReverse ? input[structure.row + M - 1][structure.col]
                            : input[structure.row][structure.col]);
            for (int i = 0; i < M; i++)
                output[structure.row + i][structure.col] = height;
            ret = Math.max(ret, searchArea(output, mSeaLevel));
            for (int i = 0; i < M; i++)
                output[structure.row + i][structure.col] = input[structure.row
                        + i][structure.col];
        }
    }
    return ret;
}