Educational Codeforces Round 1(D. Igor In the Museum) (BFS+离线访问)
题目链接:http://codeforces.com/problemset/problem/598/D
题意是 给你一张行为n宽为m的图 k个询问点 ,求每个寻问点所在的封闭的一个上下左右连接的块所能看到的壁画有多少(大概这样吧)。
我的做法是bfs(dfs也可以)这个为'.'的点,要是遇到上下左右其中有'*'的话就加起来。要是每次询问然后bfs一下肯定超时,所以我用一个ans[1005][1005]记录每个点的值,ok[1005][1005]数组判断是否访问过,初始化为false。然后开始遍历一次,遇到'*'的ans则为0,遇到'.' 且 ok[i][j] = false的就bfs周围相连的'.',算出答案然后再逐个赋值给一个房间的ans[i][j],都标记经过 即ok[i][j] = true ...。
代码如下:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue> using namespace std;
char map[MAXN][MAXN];
bool ok[MAXN][MAXN];
int ans[MAXN][MAXN];
int n , m , res , tx[] = {- , , , } , ty[] = { , , , -};
struct data {
int x , y;
}; void init() {
memset(ok , false , sizeof(ok));
} bool judge(int x , int y) { //判断点是否是'.' 且未经过
if(x < n && y < m && x >= && y >= && map[x][y] != '*' && !ok[x][y]) {
return true;
}
return false;
} bool judge2(int x , int y) { //判断点是否是'*'
if(x < n && y < m && x >= && y >= && map[x][y] == '*') {
return true;
}
return false;
} int get(int x , int y) { //壁画的相加
int num = ;
for(int i = ; i < ; i++) {
if(judge2(x + tx[i] , y + ty[i])) {
num++;
}
}
return num;
} void bfs(int x , int y) {
vector<data > v; //用来存相连的'.'
queue <data> Q;
data a;
a.x = x , a.y = y;
Q.push(a);
while(!Q.empty()) {
data temp = Q.front();
v.push_back(temp);
Q.pop();
if(map[temp.x][temp.y] == '.') {
res += get(temp.x , temp.y);
ok[temp.x][temp.y] = true;
}
for(int i = ; i < ; i++) {
a.x = temp.x + tx[i] , a.y = temp.y + ty[i];
if(judge(a.x , a.y)) {
Q.push(a);
ok[a.x][a.y] = true;
}
}
}
for(int i = ; i < v.size() ; i++) {
ans[v[i].x][v[i].y] = res;
}
v.clear(); //清空
} int main()
{
int q , sx , sy;
ios::sync_with_stdio(false);
while(cin >> n >> m >> q) {
memset(ans , , sizeof(ans));
for(int i = ; i < n ; i++)
cin >> map[i];
init();
for(int i = ; i < n ; i++) {
for(int j = ; j < m ; j++) {
res = ;
if(!ok[i][j] && map[i][j] == '.') {
bfs(i , j);
}
}
}
while(q--) {
cin >> sx >> sy;
cout << ans[sx - ][sy - ] << endl;
}
}
}
Educational Codeforces Round 1(D. Igor In the Museum) (BFS+离线访问)的更多相关文章
- 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 ...
- [Educational Codeforces Round 16]E. Generate a String
[Educational Codeforces Round 16]E. Generate a String 试题描述 zscoder wants to generate an input file f ...
- [Educational Codeforces Round 16]D. Two Arithmetic Progressions
[Educational Codeforces Round 16]D. Two Arithmetic Progressions 试题描述 You are given two arithmetic pr ...
- [Educational Codeforces Round 16]C. Magic Odd Square
[Educational Codeforces Round 16]C. Magic Odd Square 试题描述 Find an n × n matrix with different number ...
- [Educational Codeforces Round 16]B. Optimal Point on a Line
[Educational Codeforces Round 16]B. Optimal Point on a Line 试题描述 You are given n points on a line wi ...
- [Educational Codeforces Round 16]A. King Moves
[Educational Codeforces Round 16]A. King Moves 试题描述 The only king stands on the standard chess board ...
- Educational Codeforces Round 6 C. Pearls in a Row
Educational Codeforces Round 6 C. Pearls in a Row 题意:一个3e5范围的序列:要你分成最多数量的子序列,其中子序列必须是只有两个数相同, 其余的数只能 ...
- Educational Codeforces Round 9
Educational Codeforces Round 9 Longest Subsequence 题目描述:给出一个序列,从中抽出若干个数,使它们的公倍数小于等于\(m\),问最多能抽出多少个数, ...
- Educational Codeforces Round 37
Educational Codeforces Round 37 这场有点炸,题目比较水,但只做了3题QAQ.还是实力不够啊! 写下题解算了--(写的比较粗糙,细节或者bug可以私聊2333) A. W ...
随机推荐
- 函数os_file_pread
/*******************************************************************//** Does a synchronous read ope ...
- UVa 11762 (期望 DP) Race to 1
设f(x)表示x转移到1需要的次数的期望,p(x)为不超过x的素数的个数,其中能整除x的有g(x)个 则有(1-g(x)/p(x))的概率下一步还是转移到x,剩下的情况各有1/p(x)的概率转移到x/ ...
- Test语言编译器V0.8
感觉这个挺好耍的,书上的代码有错误,而且功能有限. 一.词法分析 特点: (1)可对中文进行识别:(2)暂不支持负数,可以在读入‘-'时进行简单标记后就能对简单负数进行识别了. #include &l ...
- Js 读写cookies
//写cookies函数 function setCookie(name, value)//两个参数,一个是cookie的名子,一个是值 { var Days = 30; //此 cookie 将被保 ...
- js 写成类的形式 js 静态变量 js方法 属性 json类
function ClassStudentList() { //[{"Cid":"0d","Students":[{"Sid&qu ...
- 【转】JAVA之网络编程
转自:火之光 网络编程 网络编程对于很多的初学者来说,都是很向往的一种编程技能,但是很多的初学者却因为很长一段时间无法进入网络编程的大门而放弃了对于该部分技术的学习. 在 学习网络编程以前,很多初学者 ...
- Windows 下音频数据采集和播放
音频操作所需头文件和链接库 #include<mmsystem.h>#include<mmreg.h>#pragma comment(lib, "winmm.lib ...
- Nosql释义
NoSQL不是产品,是一项运动 ---->NoSQL(NoSQL = Not Only SQL ),意即反SQL运动,是一项全新的数据库革命性运动,早期就有人提出,发展至2009年 ...
- 【转】 CATransform3D 矩阵变换之立方体旋转实现细节
原文网址:http://blog.csdn.net/ch_soft/article/details/7351896 第一部分.前几天做动画,使用到了CATransform3D ,由于没有学过计算机图形 ...
- Spring4整合Hibernate4
首先,要明确Spring整合Hibernate可以做什么? 答案是: 1.由IOC容器来管理Hibernate的SessionFactory 2.让Hibernate使用上Spring的声明式事务 整 ...