poj 1974 The Happy Worm

The Happy Worm
Time Limit:5000MS     Memory Limit:131072KB     64bit IO Format:%I64d & %I64u

Description

The Happy Worm lives in an m*n rectangular field. There are k stones placed in certain locations of the field. (Each square of the field is either empty, or contains a stone.) Whenever the worm sleeps, it lies either horizontally or vertically, and stretches so that its length increases as much as possible. The worm will not go in a square with a stone or out of the field. The happy worm can not be shorter than 2 squares. 

The question you are to answer is how many different positions this worm could be in while sleeping. 

Input

The first line of the input contains a single integer t (1 <= t <= 11), the number of test cases, followed by the input data for each test case. The first line of each test case contains three integers m, n, and k (1 <= m,n,k <= 131072). The input for this test case will be followed by k lines. Each line contains two integers which specify the row and column of a stone. No stone will be given twice.

Output

There should be one line per test case containing the number of positions the happy worm can be in.

Sample Input

1
5 5 6
1 5
2 3
2 4
4 2 
4 3
5 1

Sample Output

9


本来是一道简单的题,结果看了半天没想出来,又是百度看解析出的结果。

感觉已经对搜答案产生了依赖,一有不会就想搜,这样会影响进步,以后得注意。

回到正题,最开始的想法是直接两个for循环暴力求解,发现k太大一定会t,后来看了别人的思路后发现对石头使用两次快排(平行和垂直)即可搞定,敲完代码发现t了,发现要把cin改成scanf,真是坑啊,上交w,尼玛原来是忽略了一个石头都不放的情况,于是加了前后两个石头,继续w,看了半天不知哪错,最后才发现快排的范围忘记加1了大哭哭

这种小错误体现我还是不够有耐心不够仔细不够认真,

继续学习吧,新手~

代码如下:

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<algorithm>
#include<math.h>
using namespace std;
typedef struct ston
{
    int x,y;
}ston;
ston a[131080];
int cmprow(ston a,ston b)
{
    if(a.x<b.x)
    return 1;
    else if(a.x==b.x)
    {
        if(a.y<b.y)
        return 1;
        else
        return 0;
    }
    else
    return 0;
}
int cmpcolumn(ston a,ston b)
{
    if(a.y<b.y)
    return 1;
    else if(a.y==b.y)
    {
        if(a.x<b.x)
        return 1;
        else
        return 0;
    }
    else
    return 0;
}
int main()
{
    int t,m,n,k,sum;
    cin>>t;
    while(t--)
    {
        sum=0;
        cin>>m>>n>>k;
        for(int i=1;i<=k;i++)
            scanf("%d%d",&a[i].x,&a[i].y);
        a[0].x=1,a[0].y=0;
        a[k+1].x=m,a[k+1].y=n+1;
        sort(a+1,a+k+1,cmprow);
        for(int i=1;i<=k+1;i++)
        {
                 if(a[i-1].x==a[i].x)
                {
                    if(a[i].y-a[i-1].y-1>=2)
                    sum+=1;
                }
                else
                {
                    sum+=a[i].x-a[i-1].x-1;
                    if(n-a[i-1].y>=2)
                        sum+=1;
                    if(a[i].y-1>=2)
                    sum+=1;
                }
        }
        a[0].x=0;a[0].y=1;a[k+1].x=m+1;a[k+1].y=n;
        sort(a+1,a+k+1,cmpcolumn);
        for(int i=1;i<=k+1;i++)
        {
                 if(a[i-1].y==a[i].y)
                {
                    if(a[i].x-a[i-1].x-1>=2)
                    sum+=1;
                }
                else
                {
                    sum+=a[i].y-a[i-1].y-1;
                    if(m-a[i-1].x>=2)
                    sum+=1;
                    if(a[i].x-1>=2)
                    sum+=1;
                }
        }
        cout<<sum<<endl;
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值