链接:

https://vjudge.net/problem/CodeForces-598D

题意:

Igor is in the museum and he wants to see as many pictures as possible.

Museum can be represented as a rectangular field of n × m cells. Each cell is either empty or impassable. Empty cells are marked with '.', impassable cells are marked with '*'. Every two adjacent cells of different types (one empty and one impassable) are divided by a wall containing one picture.

At the beginning Igor is in some empty cell. At every moment he can move to any empty cell that share a side with the current one.

For several starting positions you should calculate the maximum number of pictures that Igor can see. Igor is able to see the picture only if he is in the cell adjacent to the wall with this picture. Igor have a lot of time, so he will examine every picture he can see.

思路:

Bfs加染色,先求出每个空格单个位置能看到画,Bfs的时候数一个联通快能看到的画,同时给一个联通快染色,优化重复查询的问题

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
//#include <memory.h>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <math.h>
#include <stack>
#include <string>
#include <assert.h>
#define MINF 0x3f3f3f3f
using namespace std;
typedef long long LL; const int MAXN = 1e3+10;
const int Next[4][2] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
char Map[MAXN][MAXN];
int Value[MAXN][MAXN];
int Sum[MAXN*MAXN];
int Vis[MAXN][MAXN];
int n, m, q; void Bfs(int x, int y, int p)
{
int res = 0;
queue<pair<int, int> > que;
que.push(make_pair(x, y));
Vis[x][y] = p;
res += Value[x][y];
while (!que.empty())
{
x = que.front().first;
y = que.front().second;
que.pop();
for (int i = 0;i < 4;i++)
{
int tx = x+Next[i][0];
int ty = y+Next[i][1];
if (tx < 1 || tx > n || ty < 1 || ty > m)
continue;
if (Map[tx][ty] == '*' || Vis[tx][ty] != 0)
continue;
res += Value[tx][ty];
Vis[tx][ty] = p;
que.push(make_pair(tx, ty));
}
}
Sum[p] = res;
// cout << Sum[p] << endl;
} int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m >> q;
for (int i = 1;i <= n;i++)
{
for (int j = 1;j <= m;j++)
cin >> Map[i][j];
}
for (int i = 1;i <= n;i++)
{
for (int j = 1;j <= m;j++)
{
if (Map[i][j] == '*')
continue;
for (int k = 0;k < 4;k++)
{
int x = i+Next[k][0];
int y = j+Next[k][1];
if (x < 1 || x > n || y < 1 || y > m)
continue;
if (Map[x][y] == '.')
continue;
Value[i][j]++;
}
}
}
for (int i = 1;i <= q;i++)
{
int x, y;
cin >> x >> y;
if (Vis[x][y] != 0)
cout << Sum[Vis[x][y]] << endl;
else
{
Bfs(x, y, i);
cout << Sum[Vis[x][y]] << endl;
}
} return 0;
}

最新文章

  1. 基于Metronic的Bootstrap开发框架经验总结(6)--对话框及提示框的处理和优化
  2. JSLint JavaScript代码质量审查工具汉化中文版隆重发布
  3. JavaEE基础(十二)
  4. c#学习之Socket网络编程
  5. [原]Unity3D深入浅出 - 认识开发环境中的Project面板
  6. JavaScript要点(七) 函数调用
  7. ExtJS4中initComponent和constructor的区别
  8. 九张图让你的PPT立刻高大上
  9. Java基础--IO
  10. 简易的C/S系统(实现两个数的和)
  11. C# 窗体打开拖动到窗体的文件
  12. Mac解决某些命令失效问题
  13. aspx 页面中 js 引用与页面后台的数据交互 --【 js 调后台】
  14. Burrow 服务的安装部署
  15. python 判断变量有没有定义
  16. mybatis 的查询某个字段的特定位数(模糊查询)
  17. 8、java5线程池之动态缓存线程池newCachedThreadPool
  18. gulp-webserver
  19. 【Alpha】Daily Scrum Meeting——blog1
  20. mvc上传图片(上传和预览)webuploader

热门文章

  1. cocos2dx[3.2](2) 3.x巨变
  2. 基于element表格的合并多个行实例
  3. CF498B Name That Tune(动态规划dp)
  4. 升级tableau版本
  5. Virtual DOM和snabbdom.js
  6. Java作业 题目:16版.真实员工数统计
  7. 洛谷 P2023 维护序列 题解
  8. springboot2.0自适应效果错误响应
  9. java tomcat服务器
  10. Git+码云安装