题意:
m*n的矩形区域,有k块石头,给出每块石头坐标。蠕虫横躺或竖趟在空地上休息,蠕虫可以伸缩,最短2个单位。求蠕虫可以躺在几个地方。
本题要点:
虫子至少占据两个空格,按横向和纵向分类讨论,这里按纵向的来说明(显然,m >= 2, 才有计算的必要)
1、 x 坐标不相等,按x排序。x相等,则按 y 坐标排序
2、 第一个点 stones[0], 前面有很多空列, ans += stones[0].x - 1;
3、 stones[0] 的下边,有很多空位(2个以上的空格), if(stones[0].y > 2) ++ans;
4、 两点的横坐标相等, stones[i].x == stones[i - 1].x, 中间有两个以上的空格 ++ans;
5、 两点的横坐标不相等, stones[i].x != stones[i - 1].x,
a) 前一点的后面有2个以上的空格, m - stones[i - 1].y >= 2 ++ans;
b) 中间有很多空的列, ans += stones[i].x - stones[i - 1].x - 1;
c) 后一点的前面有2个以上的空格, if(stones[i].y > 2) ++ans;
6、 最后一点 stones[k - 1] 上面有两个以上的空格, ++ans
7、 最后一点 stones[k - 1] 后面有两个以上的空列,
ans += n - stones[k - 1].x;
最后,注意一下,k == 0 的特殊情况
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int MaxN = 131072 + 10;
int t, m, n, k;
struct Stone
{
int x, y;
}stones[MaxN];
bool cmp_col(const Stone& a, const Stone& b)
{
if(a.x != b.x)
{
return a.x < b.x;
}
return a.y < b.y;
}
bool cmp_row(const Stone& a, const Stone& b)
{
if(a.y != b.y)
{
return a