D. Igor In the Museum
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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.

Input

First line of the input contains three integers nm and k (3 ≤ n, m ≤ 1000, 1 ≤ k ≤ min(n·m, 100 000)) — the museum dimensions and the number of starting positions to process.

Each of the next n lines contains m symbols '.', '*' — the description of the museum. It is guaranteed that all border cells are impassable, so Igor can't go out from the museum.

Each of the last k lines contains two integers x and y (1 ≤ x ≤ n, 1 ≤ y ≤ m) — the row and the column of one of Igor's starting positions respectively. Rows are numbered from top to bottom, columns — from left to right. It is guaranteed that all starting positions are empty cells.

Output

Print k integers — the maximum number of pictures, that Igor can see if he starts in corresponding position.

Examples
input
5 6 3 ****** *..*.* ****** *....* ****** 2 2 2 5 4 3
output
6 4 10
input
4 4 1 **** *..* *.** **** 3 2
output
8
题解:我去他个jj,这15组数据跟我有仇,最后一组数据啊。为毛。。。。。。。大神们求解啊,疯了。。。。。
今天看了大神的写法,感觉处理的很是巧妙;学习了;
修改ac的代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
const int INF=0x3f3f3f3f;
#define mem(x,y) memset(x,y,sizeof(x))
#define SI(x) scanf("%d",&x)
#define PI(x) printf("%d",x)
#define P_ printf(" ")
int n,m,k;
const int MAXN=;
char mp[MAXN][MAXN];
int vis[MAXN][MAXN];
int ans,tp;
int disx[]={,,,-};
int disy[]={,-,,};
int dp[];
void dfs(int x,int y){
if(mp[x][y]=='*'){
ans++;return ;
}
vis[x][y]=tp;
for(int i=;i<;i++){
int nx=x+disx[i],ny=y+disy[i];
if(nx<||ny<||nx>=n||ny>=m)continue;
if(vis[nx][ny])continue;
dfs(nx,ny);
}
}
int main(){
while(~scanf("%d%d%d",&n,&m,&k)){
mem(mp,);
for(int i=;i<n;i++)scanf("%s",mp[i]);
int x,y;
mem(dp,);
tp=;
for(int i=;i<n;i++){
for(int j=;j<m;j++){
if(!vis[i][j]&&mp[i][j]=='.'){
ans=;
dfs(i,j);
dp[tp]=ans;
tp++;
}
}
}
while(k--){
scanf("%d%d",&x,&y);
//printf("%d\n",mark[x-1][y-1]);
printf("%d\n",dp[vis[x-][y-]]);
}
}
return ;
}
我的wa代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
const int INF=0x3f3f3f3f;
#define mem(x,y) memset(x,y,sizeof(x))
#define SI(x) scanf("%d",&x)
#define PI(x) printf("%d",x)
#define P_ printf(" ")
int n,m,k;
const int MAXN=;
char mp[MAXN][MAXN];
int vis[MAXN][MAXN];
int mark[MAXN][MAXN];
int ans;
int disx[]={,,,-};
int disy[]={,-,,};
int dp[MAXN*MAXN];
int a[MAXN],b[MAXN];
int tp;
int kk;
void dfs(int x,int y){
if(mp[x][y]=='*'){
ans++;return ;
}
vis[x][y]=;
a[kk]=x;b[kk]=y;
kk++;
for(int i=;i<;i++){
int nx=x+disx[i],ny=y+disy[i];
if(nx<||ny<||nx>=n||ny>=m)continue;
if(vis[nx][ny])continue;
dfs(nx,ny);
}
}
int main(){
while(~scanf("%d%d%d",&n,&m,&k)){
mem(mp,);
for(int i=;i<n;i++)scanf("%s",mp[i]);
int x,y;
mem(dp,);
mem(mark,);
int tp=;
for(int i=;i<n;i++){
for(int j=;j<m;j++){
if(!vis[i][j]&&mp[i][j]=='.'){
ans=;
kk=;
dfs(i,j);
for(int c=;c<kk;c++){
mark[a[c]][b[c]]=tp;
dp[tp]=ans;
tp++;
}
}
}
}
while(k--){
scanf("%d%d",&x,&y);
//printf("%d\n",mark[x-1][y-1]);
printf("%d\n",dp[mark[x-][y-]]);
}
}
return ;
}

Igor In the Museum(搜搜搜151515151515******************************************************1515151515151515151515)的更多相关文章

  1. BZOJ_1615_[Usaco2008_Mar]_The Loathesome_Hay Baler_麻烦的干草打包机_(模拟+宽搜/深搜)

    描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1615 一个主动轮带着一些轮子转,轮子带着轮子转,轮子带着轮子转...一个非主动轮只会被一个轮子 ...

  2. 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 ...

  3. 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 ...

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

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

  5. [BFS]Codeforces Igor In the Museum

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

  6. PTA 7-6 列出连通集(深搜+广搜)

    给定一个有N个顶点和E条边的无向图,请用DFS和BFS分别列出其所有的连通集.假设顶点从0到N−1编号.进行搜索时,假设我们总是从编号最小的顶点出发,按编号递增的顺序访问邻接点. 输入格式: 输入第1 ...

  7. HDU 3666 THE MATRIX PROBLEM (差分约束 深搜 & 广搜)

    THE MATRIX PROBLEM Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  8. poj3083 Children of the Candy Corn 深搜+广搜

    这道题有深搜和广搜.深搜还有要求,靠左或靠右.下面以靠左为例,可以把简单分为上北,下南,左西,右东四个方向.向东就是横坐标i不变,纵坐标j加1(i与j其实就是下标).其他方向也可以这样确定.通过上一步 ...

  9. DFS-BFS(深搜广搜)原理及C++代码实现

    深搜和广搜是图很多算法的基础,很多图的算法都是从这两个算法中启发而来. 深搜简单地说就是直接一搜到底,然后再回溯,再一搜到底,一直如此循环到没有新的结点. 广搜简单地说就是一层一层的搜,像水的波纹一样 ...

随机推荐

  1. WINDOWS API 函数(超长,值得学习)

    一.隐藏和显示光标 函数: int ShowCursor ( BOOL bShow );  参数 bshow,为布尔型,bShow的值为False时隐藏光标,为True时显示光标:该函数的返回值为整型 ...

  2. CC++初学者编程教程(16) 搭建Xcode cocos2dx开发环境

    1.下载cocos2dx,也可以从共享目录复制 2.解压缩 3.进入目录 cd Desktop/cocos2d-x-2.2.0/tools/project-creator/ 4.创建项目 ./crea ...

  3. hdu 2102 A计划_bfs搜索

    题意:略 思路:此题陷阱超多,当##,#*,*#时不能走进去,套下模板就行了. #include <iostream> #include<cstdio> #include< ...

  4. Scrapy URLError

    错误信息如下: 2015-12-03 16:05:08 [scrapy] INFO: Scrapy 1.0.3 started (bot: LabelCrawler) 2015-12-03 16:05 ...

  5. PHP - Mysql数据库备份类

    使用方法: require_once("backdata.class.php"); $link =@mysql_connect("localhost",&quo ...

  6. 采用dlopen、dlsym、dlclose加载动态链接库【总结】

    摘自http://www.cnblogs.com/Anker/p/3746802.html 采用dlopen.dlsym.dlclose加载动态链接库[总结]   1.前言 为了使程序方便扩展,具备通 ...

  7. Android SDK目录结构和工具介绍

    Android SDK目录结构和工具介绍是本文要介绍的内容,主要是来了解并学习Android SDK的内容,具体关于Android SDK内容的详解来看本文. AD: Android SDK目录结构和 ...

  8. Git 和 SVN之间的五个基本区别

    GIT不仅仅是个版本控制系统,它也是个内容管理系统(CMS),工作管理系统等.如果你是一个具有使用SVN背景的人,你需要做一定的思想转换,来适应GIT提供的一些概念和特征.所以,这篇文章的主要目的就是 ...

  9. Search in Sorted Array,Search in Rotated Sorted Array,Search in Rotated Sorted ArrayII

    一:Search in Sorted Array 二分查找,可有重复元素,返回target所在的位置,只需返回其中一个位置,代码中的查找范围为[low,high),左闭右开,否则容易照成死循环. 代码 ...

  10. PCL点云库增加自定义数据类型

    #include <pcl/filters/passthrough.h> #include <pcl/filters/impl/passthrough.hpp> // the ...