您的位置:首页 > 大数据 > 人工智能

Pass-Muraille

2011-03-30 17:42 169 查看
Pass-Muraille
Time Limit:1000MS Memory Limit:65536K
Total Submit:79 Accepted:30
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


[/code]

Sample Output

1
1


[/code]

Hint
Walls are parallel to X.

来源:http://acmpj.zstu.edu.cn/JudgeOnline/showproblem?problem_id=1399

题目大意:由2个坐标给出n堵墙,任意从上端一直往下走,如果挡住的墙大于他所能穿过的墙数k,则移除一堵墙,求移除次数最少的次数。

解题思路:贪心算法。依次从左往右扫描,找到wall[i]>k时,移除以他往右长度最长的一堵墙。

总结:注意交换与对移除后数据的更新,注意移除后继续判断该列的wall。

AC代码:

#include<stdio.h>
#include<string.h>
#define MAX 100
int map[MAX][2],wall[MAX];
int main()
{
int n;
scanf("%d",&n);
while(n--)
{
int k,m,max=-1,count=0,max_j,temp,t,i,j,t1,t2;
//memset(map,0,sizeof(map));
scanf("%d%d",&m,&k);
for(i=0;i<m;i++)
{
scanf("%d%d%d%d",&map[i][0],&t1,&map[i][1],&t2);
if(map[i][0]>map[i][1])
{
temp=map[i][0];		//注意交换
map[i][0]=map[i][1];
map[i][1]=temp;
}
if(map[i][1]>max) max=map[i][1];
}
memset(wall,0,sizeof(wall));
for(i=0;i<=max;i++)
{
for(j=0;j<m;j++)
if(map[j][0]<=i&&map[j][1]>=i) wall[i]++;
}
for(i=0;i<=max;i++)
{
if(wall[i]>k)
{
int max1=-1;
for(j=0;j<m;j++)
{
if(map[j][0]<=i&&map[j][1]>=i)
{
t=map[j][1]-i+1;
if(t>max1) {max1=t;max_j=j;}
}
}
count++;
for(j=i;j<=map[max_j][1];j++)
wall[j]--;
map[max_j][0]=0;		//注意处理
map[max_j][1]=0;
i--;			//注意再次判断
}
}
printf("%d/n",count);
}
return 0;
}

[/code]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: