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 }

最新文章

  1. Java程序员笔试、面试题目
  2. solr suggest智能提示配置
  3. 与number_format函数有关的一个bug?
  4. react input 获取/失去焦点
  5. AX ERP 真正的自动批处理
  6. 银行卡BIN码大全
  7. MITK Tutorial (三)
  8. 配置并学习微信JS-SDK(2)—扫一扫接口http://www.qq210.com/shoutu/android
  9. [实战]记一次PUT方法拿下网站shell
  10. 10- python 网络爬虫分析
  11. Triangle Problems
  12. 聪明的质监员[NOIP2011]
  13. /VAR/LOG/各个日志文件分析
  14. 如何使用 toml 配置 SpaceVim
  15. 【English】十二、英语句子种类,陈述句、疑问句、祈使句、感叹句
  16. 使用excel 数据透视表画图
  17. CSS3 媒体查询@media 查询(响应式布局)
  18. C#6.0新特性:var s = $&quot;{12}+{23}={12+23}&quot;
  19. J2EE项目集成SAP的BO报表
  20. Python协程 Gevent Eventlet Greenlet

热门文章

  1. Codeforces Round #660 (Div. 2) C. Uncle Bogdan and Country Happiness (DFS)
  2. Selenium刚玩一会儿,就感受了私人秘书的体验
  3. Kubernets二进制安装(9)之部署主控节点控制器controller-manager
  4. 服务注册与发现-Eureka (高可用设计)
  5. codeforces 9D(非原创)
  6. UVA-10815 Andy&#39;s First Dictionary (非原创)
  7. Win7环境下mysql报错1045:Access denied for user root@localhost错误解决方法
  8. javascript questions &amp; code review
  9. CURL (CommandLine Uniform Resource Locator) 简易教程!
  10. Flutter: 粘贴板