码迷,mamicode.com
首页 > 其他好文 > 详细

洛谷——P2074 危险区域

时间:2017-09-21 09:47:27      阅读:324      评论:0      收藏:0      [点我收藏+]

标签:--   直线   namespace   cli   include   整数   ret   最大   har   

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;
}

 

洛谷——P2074 危险区域

标签:--   直线   namespace   cli   include   整数   ret   最大   har   

原文地址:http://www.cnblogs.com/z360/p/7566748.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!