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. URL,URLConnection,HttPURLConnection的使用

    URLConnection与HttPURLConnection都是抽象类,无法直接实例化对象.其对象主要通过URL的openconnection方法获得. 值得注意的是:1.openConnectio ...

  2. 在IE浏览器中iframe跨域访问cookie/session丢失的解决办法

    单点登录需要在需要进入的子系统B中添加一个类,用于接收A系统传过来的参数: @Action(value = "outerLogin", results = { @Result(na ...

  3. wordpress参考网站

    wordpress大学http://www.wpdaxue.com/post-tags-and-categories-for-pages.html

  4. 直播视频插件--sewise player

    直播视频插件 -- sewise player 2017-1-9 因为公司要开发一个关于购车直播的新项目,需要在页面引入直播视频,项目组之前都没有做过关于直播类型的项目,所以可以说是真的从各种资源中筛 ...

  5. poj 3641 Pseudoprime numbers(快速幂)

    Description Fermat's theorem states that for any prime number p and for any integer a > 1, ap = a ...

  6. poj2488 A Knight's Journey

      A Knight's Journey Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 24840   Accepted:  ...

  7. android典型监听事件实

    public class MainActivity extends Activity { int counter; Button add, sub; TextView display; @Overri ...

  8. Bellman 算法

    这道题目事实上就是在求有没有正环.与求负环的差别就是要不断的更新值,可是这个值要变大.而不是变小. Currency Exchange Time Limit: 1000MS   Memory Limi ...

  9. 固定表格行列(expression)

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head ...

  10. 【数学水题】【TOJ4113】【 Determine X】

    题目大意: yuebai has a long sequence of integers A1,A2,-,AN. He also has such a function: F(x)=∑i=1N(⌊Ai ...