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

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.

思路:

如果在一个地图上种树让你判断是否成环不太容易,但是,若把所有的树种好,依次去伐树,那么问题就简单不少,这个题目中,与边界相邻的没树的地方不算在绿化区,因此可以增加最外围一层巧妙地标记所有的这种区域。种好所有树后就可以依次砍树,如果旁边有边界区,就去bfs。

#include<stdio.h>
#include<string.h>
#include<string>
#include<queue>
#include<vector>
#include<iostream>
#include<algorithm>
using namespace std;const int maxn = 4e6+7;bool mp[2001][2001];
bool mk[2001][2001];
int sz;int Next[4][2] = {1,0,-1,0,0,1,0,-1};void bfs(int x,int y)
{queue <pair<int,int> > q;q.push(make_pair(x,y));mk[x][y] = 1;while(q.size()){pair <int,int> p = q.front();q.pop();x = p.first,y=p.second;for(int i = 0; i < 4; i++){int x_ = x+Next[i][0];int y_ = y+Next[i][1];if(x_ < 0 || y_ < 0 || x_ >= 2000 || y_ >= 2000){continue;}if(mp[x_][y_] || mk[x_][y_]) continue;mk[x_][y_] = 1;sz--;q.push(make_pair(x_,y_));}}
}int qx[100005],qy[100005];
int ans[100005];
int main()
{int n;scanf("%d",&n);int m = n;int i = 0;while(m--){int x,y;scanf("%d%d",&x,&y);i++;x+=1000;y+=1000;qx[i] = x;qy[i] = y;mp[x][y] = 1;}sz = 2000*2000 - n-1;mk[0][0] = 1;bfs(0,0);ans[n] = sz;for(int i = n; i >= 1; i--){int x = qx[i];int y = qy[i];sz++;int yes = 0;for(int k = 0; k < 4; k++){int x_ = x+Next[k][0];int y_ = y+Next[k][1];if(mp[x_][y_]) continue;if(mk[x_][y_]) yes = 1; }if(yes) sz--,bfs(x,y);ans[i-1] = sz; mp[x][y] = 0;}for(int i = 1; i <= n; i++){printf("%d\n",ans[i]);}return 0;
}

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 华中科技大学校赛 L Fresh Air BFS

    链接: https://www.nowcoder.com/acm/contest/106/L 来源:牛客网 Fresh Air 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 3276 ...

  4. 英语练习109 We need fresh air

    我们需要新鲜空气 琼 :乖乖,这里面可真闷啊. 德雷克:你说的对.我们需要些新鲜空气. 琼 :咱们打开空调吧. 德雷克:为何不干脆打开窗户呢? 琼 :那就更好了. We need fresh air ...

  5. fresh air项目记录1

    完成: 完成了程序架构的设计,了解了STM32串口相关的使用 下一步: 实现UART1的串口发送接收,用架构的方式 实现UART2的串口发送接收,用架构的方式 实现UART3的串口发送接收,用架构的方 ...

  6. fresh air项目记录3

    完成: 列出了AT设置模式的指令,发送值和应答值. 得出指令后发送0d0a,  其实,0d是跟在指令后的,所以会又反回来.0a是指令发送完标志位,所以不再发回来. 下一步: 用单片机的串口2发送AT指 ...

  7. fresh air 项目记录2

    完成; 1]串口1通信成功 2]队列的相关函数可以使用 下一步: 2]实现PM2.5指令的识别及处理 3]加串口2.3.

  8. Grails is a breath of fresh air for Java developers

    最近http://www.indicthreads.com/对Grails的创始人Graeme Rocher 做了一次访谈,其中谈及了Grails和RoR以及对动态语言的看法,从中可以看出开发者对Gr ...

  9. air什么意思中文_Air 的中文意思是什么?

    展开全部 air 释义: (1)作为名词,空气:32313133353236313431303231363533e59b9ee7ad9431333431366261天空:(飞行的)空中. (2)作为动 ...

最新文章

  1. 命名实体识别NER遗留问题----模型构建
  2. 易邮邮箱服务器的安装和配置
  3. mac git安装及github配置
  4. 剖析基于IOS的华为交换机配置口令设置
  5. 33万字!深度学习笔记在线版发布!
  6. SAP UI5 My Opportunity应用里的 currency validation
  7. python解释型语言的区别_什么是解释型语言?
  8. python利用matplotlib做饼图_python利用matplotlib库绘制饼图的方法示例
  9. 阿里高级技术专家箫逸:如何画好一张架构图?
  10. gitignore.io-程序猿值得拥有的智能生成gitignore文件的秘密武器
  11. 微服务升级_SpringCloud Alibaba工作笔记0006---spring gateway工作流程
  12. Python命令行解析工具argparse
  13. 静态绑定(前期绑定)
  14. vb6判断操作系统版本-从win95到win7
  15. 谷歌浏览器与电脑时间不同步
  16. 感谢孙权、欢迎行癫,阿里云在路上!
  17. DELETE * FROM和DELETE FROM的区别
  18. 通过K-means对iris数据集进行处理 Kmeans聚类算法实例
  19. matlab加速度积分计算速度和位移
  20. js巧用 ‘~‘运算符

热门文章

  1. Java8 利用Lambda处理List集合
  2. 【转】下载太慢?简单设置让iTunes提速十几倍
  3. O‘REILLY: Lightbulb Start Watching —— 从指定时间开始计算多组时间差之和(秒数)
  4. 精确移相电路的设计举例
  5. 小程序发力:64个小程序入口,小程序代理的机会来了!
  6. solr 插入数据后变数组_理解Solr缓存及如何设置缓存大小
  7. Solidwork许可、许可分析、分析许可
  8. 实现高效的网络营销方式-盛科广告传媒
  9. SPRING JPA配置讲解
  10. 用计算机分析翡翠赌石,翡翠赌石结构可以用X光机看?高科技在翡翠赌石中可以一展身手吗?...