链接: https://www.nowcoder.com/acm/contest/106/L
来源:牛客网

Fresh Air
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 32768K,其他语言65536K
64bit IO Format: %lld

题目描述

It’s universally acknowledged that there’re innumerable trees in the campus of HUST.

And you know that, trees have the special ability to refresh the air. If an area, which is surrounded by trees, is separated from the outer atmosphere, you can call it “the supercalifragilisticexpialidocious area”. Students can enjoy the healthiest air there. Then, you happened to know that HUST will plant N trees in a bare plain, so you want to calculate the total size of “the supercalifragilisticexpialidocious area” after each operation.
We describe the position of trees with a coordinate.(Xi,Yi).
For example, after 9 trees were planted, the green area is a supercalifragilisticexpialidocious area, its size is 3.

After planting a new tree in (3,2), its size is 2.

输入描述:

The first line is an integer N as described above.
Then following N lines, each line contains two integer Xi and Yi, indicating a new tree is planted at (Xi,Yi).

输出描述:Output N lines, each line a integer indicating the total size of supercalifragilisticexpialidocious areas after each operation.

题意:计算被树围起来的区域大小

思路:反向DFS,一开始先把所以树种好,再一颗一颗拔掉遍历一遍;

AC代码:

#include <algorithm>
#include <string.h>
#include <cstring>
#include <stdio.h>
#include <queue>
using namespace std;
struct NODE{int x,y;}a[100005];
//supercalifragilisticexpialidocious area被树围住 从外部无法到达
//cnt为无法到达的区域大小 无法到达的区域设为0
int n,cnt,G[2005][2005],ans[100005];
int mov[4][2]={{0,1},{1,0},{0,-1},{-1,0}};
bool bound(int x,int y)
{return x<0||x>2000||y<0||y>2000;
}
void bfs(int x,int y)
{/// bfs过程中 遇到1就停止 /// 能从该点出发而到达的0都变为2 无法到达的0则不变queue <NODE> q;q.push((NODE){x,y});G[x][y]=2; cnt--;while(!q.empty()){NODE tmp=q.front(); q.pop();for(int i=0;i<4;i++){int nowx=tmp.x+mov[i][0],nowy=tmp.y+mov[i][1];if(bound(nowx,nowy)) continue;//越界if(G[nowx][nowy]) continue;//能到达或已访问过G[nowx][nowy]=2; cnt--; //该点为能够到达的0 cnt--即去掉该点q.push((NODE){nowx,nowy});}}
}
bool check(int x,int y)
{for(int i=0;i<4;i++){ // 若存在因该点的树而停止继续搜索的情况int nowx=x+mov[i][0],nowy=y+mov[i][1];if(bound(nowx,nowy)) continue;//越界if(G[nowx][nowy]==2) return 1;///即该点四周有bfs时被变为2的点}return 0;
}
int main ()
{while(~scanf("%d",&n)){for(int i=1;i<=n;i++){scanf("%d%d",&a[i].x,&a[i].y);a[i].x+=1000, a[i].y+=1000;G[a[i].x][a[i].y]=1;///标记1为树  2为空地;}cnt=2001*2001-n;bfs(0,0);//先bfs一遍 ///此时res等于减去1和2后余下的无法到达的0的个数for(int i=n;i>0;i--) //从最后一点向前遍历{ans[i]=cnt++;G[a[i].x][a[i].y]=0; ///删除该点放置的树if(check(a[i].x,a[i].y))bfs(a[i].x,a[i].y); //从该点继续bfs}for(int i=1;i<=n;i++)printf("%d\n",ans[i]);}return 0;
}

2018 华中科技大学校赛 L Fresh Air BFS相关推荐

  1. 2018 华中科技大学校赛 L Fresh Air 思维BFS

    链接:https://www.nowcoder.com/acm/contest/106/L 来源:牛客网 It's universally acknowledged that there're inn ...

  2. 第十四届华中科技大学程序设计竞赛-L—Fresh Air,bfs拓展,倒着bfs

    链接: https://www.nowcoder.com/acm/contest/106/L 来源:牛客网 It's universally acknowledged that there're in ...

  3. 2018——广东工业大学校赛题解

    链接:https://www.nowcoder.com/acm/contest/90/A 来源:牛客网 A 小明在坐景驰科技研发的无人车到达了目的地. 景驰科技(JingChi.ai)是一家由人工智能 ...

  4. 2018南京网络赛L题 Magical Girl Haze(分层图+优先队列优化的dijkstra)

    使用优先队列优化过的dijkstra时间复杂度可以达到O(v*logn),还是很快的. #include <iostream>                //最好是用long long ...

  5. 2018年天梯赛-全国总决赛

    L1-1 天梯赛座位分配 (20 分)(待补) 题意: 天梯赛每年有大量参赛队员,要保证同一所学校的所有队员都不能相邻,分配座位就成为一件比较麻烦的事情.为此我们制定如下策略:假设某赛场有 N 所学校 ...

  6. 福建农林大学校赛(同步赛)

    福建农林大学校赛(同步赛) 目录 A 派蒙之灵 题目思路 题目代码 B 派蒙家的荧女仆 题目思路 题目代码 C 派蒙的奇妙冒险------石之海 题目思路 题目代码 D 派蒙游戏世界对旅行荧妹很不友好 ...

  7. 蓝桥杯 2018年省赛真题 (Java 大学C组)

    蓝桥杯 2018 年省赛真题(Java 大学 C 组 ) #1 哪天返回 #2 猴子分香蕉 #3 字母阵列 #4 第几个幸运数 #5 书号验证 #6 打印大X #7 缩位求和 #8 等腰三角形 #9 ...

  8. 2018宁夏网络赛 B Goldbach (米勒拉宾素数测试)

    2018宁夏网络赛 B Goldbach (米勒拉宾素数测试) 题目链接 题目大意: 给你一个偶数n (2<n<=1e18) 让你把n分解成两个素数的和.(如果有多个输出任意一个) 解题思 ...

  9. 网络技能大赛-2018年国赛真题[2018年全国职业技能大赛高职组计算机网络应用赛项真题-I卷]路由交换部分答案详解

    网络技能大赛-2018年国赛路由交换部分答案详解 2022年全国职业技能大赛网络系统管理赛项相较2021年再次做出改动,Linux部分从之前的Debian又换回了CentOS,不过相应增加了UOS国产 ...

最新文章

  1. Linux下配置jupyter notebook远程访问实战:配置Jupyter的连接密码、启动jupyter服务、远程访问jupyter(关闭防火墙)
  2. 【中级软考】软件质量模型的六大特性27个子特性(软件质量特性:功能性、可靠性、易用性、效率性、软件维护性、软件可移植性)
  3. 算法训练营08-分治和回溯
  4. java 面试 概率论_编程培训-115个Java面试题和答案B.pdf
  5. 服务器云采购,从发展角度看小企业需要上云还是采购服务器
  6. Java生产环境下性能监控与调优详解 第4章 基于Btrace的监控调试
  7. 吴恩达机器学习笔记3——线性代数
  8. GitHub 撤销 commit
  9. Shell脚本学习-阶段七-信息过滤磁盘分区
  10. BSD Socket~TCP~Example Code
  11. _DEVOBJ_EXTENSION结构体
  12. 【GIS导论】实验五 缓冲区分析和网络分析
  13. C语言小项目——电子秒表(毫秒级)
  14. StartDT奇点云邀您参加2022云栖大会,11月3-5日杭州见
  15. 常用软件开发安装包+破解分享
  16. MyEclipse 10.5下载地址及破解方法
  17. Mac:TexStudio 中文论文模版
  18. 2023超好用的Mac清理优化工具CleanMyMacX
  19. performSelector和传递参数
  20. java抢答器代码_分享一下我的51单片机抢答器代码

热门文章

  1. html中img图片截取显示中间部分
  2. 能向入口函数传入多个参数的 QueueUserWorkItem
  3. 在鬼语者中看到米帅(温特沃斯·米勒)
  4. ulimit命令用法详解
  5. U盘制作WINXP启动盘
  6. 如何在python中计算天干地支的五行旺衰
  7. 计算机控制技术大林算法计算题,计算机控制技术作业
  8. ios 线条球_iOS 圆球加载动画解析
  9. 计算机网络-第4章 4.4.2-网际协议-IPv4编址-同步电大进度
  10. 【存储数据恢复】NetApp存储误删文件夹的数据恢复案例