题目来源
示例
思路
和leetcode85题有异曲同工之妙。
可以理解成放置了障碍物的格子相当于‘0’,空闲格子相当于‘1’
1.首先用boolean[][] square记录广场上放置障碍物的情况。有障碍物的格子设为true(因为boolean初始值为false)。
2.根据square,画柱形图
for(int i = 0;i < square.length;i++){
for(int j = 0;j < square[0].length;j++){
if(!square[i][j]){
dp[j]++;
}else{
dp[j] = 0;
}
}
}
3.使用单调栈,以栈顶元素为高,判断高是否>=物品行宽c,再判断宽是否>=物品列宽d
若满足条件,则输出“YES”,否则继续遍历,若遍历完后没有符合条件的位置,输出"NO"
具体代码
import java.util.Scanner;
import java.util.Stack;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int size = sc.nextInt();
while(size > 0){
int n = sc.nextInt();
int m = sc.nextInt();
int k = sc.nextInt();
boolean square[][] = new boolean[n][m];
for(int i = 0;i < k;i++){
square[sc.nextInt()-1][sc.nextInt()-1] = true;
}
int c = sc.nextInt();
int d = sc.nextInt();
if(zhuxingtu(square,c,d)){
System.out.println("YES");
}else{
System.out.println("NO");
}
//这里一定要记住--
size--;
}
}
public static boolean zhuxingtu(boolean[][]square,int c,int d){
//构建柱形图
int dp[] = new int[square[0].length];
for(int i = 0;i < square.length;i++){
for(int j = 0;j < square[0].length;j++){
if(!square[i][j]){
dp[j]++;
}else{
dp[j] = 0;
}
}
if(canPlace(dp,c,d)){
return true;
}
}
return false;
}
public static boolean canPlace(int height[],int c,int d){
//这里要记住栈内存的是高所在的索引位置
Stack<Integer> stack = new Stack<>();
stack.push(-1);
for(int i = 0;i < height.length;i++){
while(stack.peek()!=-1 && height[i]<height[stack.peek()]){
if(height[stack.pop()]>=c && (i-stack.peek()-1>=d)){
//System.out.println("YES");
return true;
}
}
stack.push(i);
}
while(stack.peek()!=-1){
if(height[stack.pop()]>=c && (height.length-stack.peek()-1>=d)){
//System.out.println("YES");
return true;
}
}
//System.out.println("NO");
return false;
}
}