2021-07-09

Pass-Muraille
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 13172 Accepted: 4178

Description
In modern day magic shows, passing through walls is very popular in which a magician performer passes through several walls in a predesigned stage show. The wall-passer (Pass-Muraille) has a limited wall-passing energy to pass through at most k walls in each wall-passing show. The walls are placed on a grid-like area. An example is shown in Figure 1, where the land is viewed from above. All the walls have unit widths, but different lengths. You may assume that no grid cell belongs to two or more walls. A spectator chooses a column of the grid. Our wall-passer starts from the upper side of the grid and walks along the entire column, passing through every wall in his way to get to the lower side of the grid. If he faces more than k walls when he tries to walk along a column, he would fail presenting a good show. For example, in the wall configuration shown in Figure 1, a wall-passer with k = 3 can pass from the upper side to the lower side choosing any column except column 6.

Given a wall-passer with a given energy and a show stage, we want to remove the minimum number of walls from the stage so that our performer can pass through all the walls at any column chosen by spectators.
在这里插入图片描述

Input
The first line of the input file contains a single integer t (1 <= t <= 10), the number of test cases, followed by the input data for each test case. The first line of each test case contains two integers n (1 <= n <= 100), the number of walls, and k (0 <= k <= 100), the maximum number of walls that the wall-passer can pass through, respectively. After the first line, there are n lines each containing two (x, y) pairs representing coordinates of the two endpoints of a wall. Coordinates are non-negative integers less than or equal to 100. The upper-left of the grid is assumed to have coordinates (0, 0). The second sample test case below corresponds to the land given in Figure 1.

Output
There should be one line per test case containing an integer number which is the minimum number of walls to be removed such that the wall-passer can pass through walls starting from any column on the upper side.

Sample Input

2
3 1
2 0 4 0
0 1 1 1
1 2 2 2
7 3
0 0 3 0
6 1 8 1
2 3 6 3
4 4 6 4
0 5 1 5
5 6 7 6
1 7 3 7

Sample Output

1
1

题目有点坑。有对输入以下两个情况:
1、第一个输入的不一定是左端点
2、同一层纵坐标可以有多面墙。//体现在输出中的不同行可以对应相同的纵坐标

思想:贪心从左到右扫,对每一列进行遍历。选择最长的墙删除。细节见下代码,相关注释已经给出(参考了某位大佬给的思想)。

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
const int N=105;
int dp[N][N];
int main(){
	int t;
	scanf("%d",&t);
	while(t--){
		memset(dp,0,sizeof(dp));
		int m,n;
		scanf("%d%d",&m,&n);//m为墙数,n为可穿过的最大墙数 
		int x=0,y=0;
		for(int i=0;i<m;i++){
			int x1,y1,x2,y2;
			scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
			x=max(x,max(x1,x2)); //最大的x :遍历区域 
			y=max(y,y1);//最大的y :遍历区域 
			int len;
			len=max(x1,x2)-min(x1,x2)+1;//墙的长度 :方便处理同一纵坐标有任意面墙的情况 
			for(int j=min(x1,x2);j<=max(x1,x2);j++){
				dp[y1][j]=max(dp[y1][j],len--);//有墙的就标记,数据代表其后面还占据的方格数 
			}	
		}
		int ans=0;
		for(int i=0;i<=x;i++){
			int cnt=0;
			for(int j=0;j<=y;j++){
				if(dp[j][i])
					cnt++;//有墙的就记录		
			}
			while(cnt>n){//直到这一列可以穿过 
				int pos=0,maxx=0;
				for(int j=0;j<=y;j++){
					if(maxx<dp[j][i]){
						maxx=dp[j][i];
						pos=j;//记录纵坐标 
					}
				} 
				int p=dp[pos][i];
				for(int j=0;j<p;j++)
					dp[pos][i+j]=0;//清空 
				cnt--;//清空后数减1 
				ans++;//记录我们的操作数 
			}
		} 
		printf("%d\n",ans);	
	}
	return 0; 
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值