洛谷——P2074 危险区域

P2074 危险区域

题目背景

一个恐怖组织在一座城市中安放了定时炸弹,其威力巨大,现在这里的警长想知道最坏的情况下会有多少街区受威胁。

题目描述

在一个城市有N*M个街区,每个街区由坐标描述,如图所示:

行 列 1 2 3 … M

1 (1,1) (1,2) (1,3) … (1,M)

2 (2,1) (2,2) (2,3) … (2,M)

3 (3,1) (3,2) (3,3) … (3,M)

… … … … … …

N (N,1) (N,2) (N,3) … (N,M)

现在已知有一个恐怖组织在其中的一个街区安放了定时炸弹,其威力为T,即所有到这个街区的直线距离小于等于T的街区都会受威胁,已知有K个可能的炸弹安放位置,现在这里的警长想知道最坏的情况下会有多少街区受威胁。

输入输出格式

输入格式:

 

第一行四个正整数N,M,K和T

接下来K行每行两个正整数Xi Yi,描述每个可能安放炸弹的街区。

 

输出格式:

 

一个正整数为在最坏情况下有多少街区会受威胁。

 

输入输出样例

输入样例#1:
4 5 3 2
1 2
3 4
4 5
输出样例#1:
11

说明

对于20%的数据 K=1

对于50%的数据 N,M≤1000 K≤20 T≤100

对于100%的数据 N,M≤100000 K≤50 T≤300

 

 

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
bool vis[1100][1100];
int n,m,k,t,x,y,ans;
int xx[4]={0,0,1,-1},yy[4]={1,-1,0,0};
int read()
{
    int x=0,f=1; char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1; ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0'; ch=getchar();}
    return x*f;
}
void dfs(int x,int y,int s)
{
    if(s==t+1) return ;
    if(vis[x][y]||x<1||y<1||x>n||y>m) return ;
    vis[x][y]=true;
    if(s!=0) ans++;
    for(int i=0;i<4;i++)
    {
        int fx=x+xx[i],fy=y+yy[i];
        dfs(fx,fy,s+1);    
    }
    //vis[x][y]=false;
}
int main()
{
    n=read(),m=read();
    k=read(),t=read();
    for(int i=1;i<=k;i++)
    {
        x=read(),y=read();
        if(vis[x][y]) ans--;
        dfs(x,y,0);
     } 
    printf("%d",ans);
    return 0;
}
本来是想练练搜索的,结果发现我根本就不会写这个搜索、、、、

暴力枚举在那个位置放炸弹的造成的最大危险

#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
bool vis[11000][11000];
int n,m,k,t,x,y,ans;
int xx[4]={0,0,1,-1},yy[4]={1,-1,0,0};
int read()
{
    int x=0,f=1; char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1; ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0'; ch=getchar();}
    return x*f;
}
bool work(int x,int y,int fx,int fy)
{
    if(sqrt(pow(x-fx,2)+pow(y-fy,2))<=t) return true;
    return false;
}
int main()
{
    n=read(),m=read();
    k=read(),t=read();
    while(k--)
    {
        x=read(),y=read();
        int sum=0;
        for(int i=max(1,x-t);i<=min(n,x+t);i++)
         for(int j=max(1,y-t);j<=min(m,y+t);j++)
          { 
            if(work(i,j,x,y)) sum++;
          } 
        ans=max(ans,sum);
    } 
    printf("%d",ans);
    return 0;
}

 

转载于:https://www.cnblogs.com/z360/p/7566748.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值