[刷题笔记]Codeforces 15B

本文记录了作者在解决Codeforces 15B激光问题时的心得体会,强调了考虑数据范围的重要性,并提醒避免使用无限循环。文章提供了具体的解题代码。
摘要由CSDN通过智能技术生成

题目:Codeforces 15B laser

地址:http://codeforces.com/problemset/problem/15/B

心得:

  1. 仔细考虑数据范围,包括过程量与最终结果
  2. 尽量不要写for(int  i=0;;)


代码:

#include<string>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
int main()
{
    long long  n, m ,x1, y1, x2, y2, cnt, h, l, i, t ;
    long long v[4];
    cin>>t;
    while(t--)
    {
        cnt = 0;
        cin>>n>>m>>x1>>y1>>x2>>y2;
        v[0] = min(y1,y2) - 1;//上移
        v[1] = min(x1,x2) - 1;//左移
        v[2] = m - max(y1,y2);//下移
        v[3] = n - max(x1,x2);//右移
        for(i=0;i<4;i++)
        {
            cnt += v[i];
            if(i==3)
                cnt += v[3]*v[0];
            else
                cnt += v[i]*v[i+1];
        }
        if((max(y1,y2)-v[0])<=(min(y1,y2)+v[2]))//计算重复部分
            h = v[2]+v[0]-abs(y1-y2) + 1;
        else
            h = 0;
        if((min(x1,x2)+v[3])>=(max(x1,x2)-v[1]))
            l = v[1]+v[3] - abs(x1-x2) +1;
        else
            l = 0;
        cnt = m*n - cnt*2 - 2 + l*h;
        cout<<cnt<<endl;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值