http://codeforces.com/problemset/problem/598/D

分析:BFS,同一连通区域的周长一样,但查询过多会导致TLE,所以要将连通区域的答案储存,下次查询到该连通区域就可以直接得出结果

 1 #include<iostream>
2 #include<sstream>
3 #include<cstdio>
4 #include<cstdlib>
5 #include<string>
6 #include<cstring>
7 #include<algorithm>
8 #include<functional>
9 #include<iomanip>
10 #include<numeric>
11 #include<cmath>
12 #include<queue>
13 #include<vector>
14 #include<set>
15 #include<cctype>
16 const double PI = acos(-1.0);
17 const int INF = 0x3f3f3f3f;
18 const int NINF = -INF - 1;
19 typedef long long ll;
20 #define MOD 1000007
21 using namespace std;
22 typedef pair<int, int> P;
23 char map[1005][1005];
24 int n, m, ans, cnt;
25 int x, y;
26 int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
27 int vis[1005][1005];
28 int num[1000025];
29 void bfs()
30 {
31 int i, j;
32 queue<P> q;
33 q.push(P(x, y));
34 cnt++;
35 vis[x][y] = cnt;
36 ans = 0;
37 while (q.size())
38 {
39 P temp = q.front();
40 q.pop();
41 ans += 4;
42 for (i=0; i < 4; ++i)
43 {
44 int nx = temp.first + dx[i];
45 int ny = temp.second + dy[i];
46 if (nx >= 0 && nx < n && ny >= 0 && ny < m)
47 {
48 if(map[nx][ny] == '.')
49 {
50 ans--;
51 if(!vis[nx][ny])
52 {
53 q.push(P(nx, ny));
54 vis[nx][ny] = cnt;
55 }
56 }
57 }
58 else
59 ans--;
60 }
61 }
62 num[cnt] = ans;
63 cout << ans << endl;
64 }
65
66 int main()
67 {
68 int i, j, k;
69 cin >> n >> m >> k;
70 for (i = 0; i < n; ++i)
71 cin >> map[i];
72 memset(num, -1, sizeof(num));
73 memset(vis, 0, sizeof(vis));
74 cnt = 0;
75 while (k--)
76 {
77 cin >> x >> y;
78 x--, y--;
79 if (vis[x][y] == 0)
80 bfs();
81 else
82 cout << num[vis[x][y]] << endl;
83 }
84 return 0;
85 }

Codeforces 598D (ccpc-wannafly camp day1) Igor In the Museum的更多相关文章

  1. Codeforces 1167c(ccpc wannafly camp day1) News Distribution 并查集模板

    题目: In some social network, there are nn users communicating with each other in mm groups of friends ...

  2. wannafly camp day1

    题目描述: 恬恬的生日临近了.宇扬给她准备了一个大 蛋糕. 正如往常一样,宇扬在蛋糕上插了nnn支蜡烛,并把蛋糕分为mmm个区域.因为某种原因,他必须把第iii根蜡烛插在第aia\_iai​个区域或第 ...

  3. 2020 CCPC Wannafly Winter Camp Day1 C. 染色图

    2020 CCPC Wannafly Winter Camp Day1 C. 染色图 定义一张无向图 G=⟨V,E⟩ 是 k 可染色的当且仅当存在函数 f:V↦{1,2,⋯,k} 满足对于 G 中的任 ...

  4. 【CodeForces - 598D】Igor In the Museum(bfs)

    Igor In the Museum Descriptions 给你一个n*m的方格图表示一个博物馆的分布图.每个方格上用'*'表示墙,用'.'表示空位.每一个空格和相邻的墙之间都有一幅画.(相邻指的 ...

  5. Codeforces 598D:Igor In the Museum

    D. Igor In the Museum time limit per test 1 second memory limit per test 256 megabytes input standar ...

  6. Educational Codeforces Round 1 D. Igor In the Museum bfs 并查集

    D. Igor In the Museum Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/598 ...

  7. [BFS]Codeforces Igor In the Museum

     Igor In the Museum time limit per test 1 second memory limit per test 256 megabytes input standard ...

  8. Igor In the Museum(搜搜搜151515151515******************************************************1515151515151515151515)

    D. Igor In the Museum time limit per test 1 second memory limit per test 256 megabytes input standar ...

  9. 2020 CCPC Wannafly Winter Camp Day1 Div.1&amp F

    #include<bits/stdc++.h> #define forn(i, n) for (int i = 0; i < int(n); i++) #define fore(i, ...

随机推荐

  1. java的静态代码块和类变量的隐式覆盖

    静态代码块特点:随着类的加载执行一次,且仅会执行一次 作用:初始化类中的static修饰的变量(static修饰的变量称为类变量.类变量和静态代码块差不多,类变量仅会被初始化一次) 一.静态代码块写法 ...

  2. hdu5247 找连续数

    Problem Description 小度熊拿到了一个无序的数组,对于这个数组,小度熊想知道是否能找到一个k 的区间,里面的 k 个数字排完序后是连续的. 现在小度熊增加题目难度,他不想知道是否有这 ...

  3. HDU-6290 奢侈的旅行 (Dijkstra+堆优化)

    高玩小Q不仅喜欢玩寻宝游戏,还喜欢一款升级养成类游戏.在这个游戏的世界地图中一共有nn个城镇,编号依次为11到nn.这些城镇之间有mm条单向道路,第ii 条单项道路包含四个参数ui,vi,ai,biu ...

  4. MySQL 误删用户故障解决方案

    目录 "误删"所有用户 解决方式一: 停止数据库 跳过 授权表 和 网络启动(重要) 插入新的用户 重启启动数据库 解决方式二: 停止数据库 跳过 授权表 和 网络启动(重要) 授 ...

  5. Eazfuscator.NET(.net混淆工具)

    软件功能 调试支持: 在你的程序集被Eazfuscator.NET混淆后,它不会成为不可调试混乱的砖块.你总是可以得到一个行号,出现未处理的异常,查看可读的堆栈跟踪,甚至附加调试器来遍历你的模糊代码. ...

  6. wordpress 主题安装 您点击的链接已过期 nginx 出现413 Request Entity Too Large

    1 nginx 出现413 Request Entity Too Large 问题是限制上传大小,解决: 1.打开nginx配置文件 nginx.conf, 路径一般是:/etc/nginx/ngin ...

  7. git命令简写配置

    在使用git工具时,有些命令比较常用,为了加快输入速度,可以自定义一些简写配置,如下所示: git st # git status git ci # git commit git br # git b ...

  8. Caffe入门:对于抽象概念的图解分析

    Caffe的几个重要文件 用了这么久Caffe都没好好写过一篇新手入门的博客,最近应实验室小师妹要求,打算写一篇简单.快熟入门的科普文. 利用Caffe进行深度神经网络训练第一步需要搞懂几个重要文件: ...

  9. mybaits(十)mybatis常见面试

      面试题总结 1.MyBatis 解决了什么问题? 或:为什么要用 MyBatis? 或:MyBatis 的核心特性? 1)资源管理(底层对象封装和支持数据源) 2)结果集自动映射 3)SQL 与代 ...

  10. Objective C & Swift & iOS & App

    Objective C & Swift & iOS & App https://www.runoob.com/ios/ios-objective-c.html https:// ...