Igor In the Museum(搜搜搜151515151515******************************************************1515151515151515151515)
1 second
256 megabytes
standard input
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.
First line of the input contains three integers n, m 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.
Print k integers — the maximum number of pictures, that Igor can see if he starts in corresponding position.
5 6 3 ****** *..*.* ****** *....* ****** 2 2 2 5 4 3
6 4 10
4 4 1 **** *..* *.** **** 3 2
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)的更多相关文章
- BZOJ_1615_[Usaco2008_Mar]_The Loathesome_Hay Baler_麻烦的干草打包机_(模拟+宽搜/深搜)
描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1615 一个主动轮带着一些轮子转,轮子带着轮子转,轮子带着轮子转...一个非主动轮只会被一个轮子 ...
- 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 ...
- 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 ...
- 【CodeForces - 598D】Igor In the Museum(bfs)
Igor In the Museum Descriptions 给你一个n*m的方格图表示一个博物馆的分布图.每个方格上用'*'表示墙,用'.'表示空位.每一个空格和相邻的墙之间都有一幅画.(相邻指的 ...
- [BFS]Codeforces Igor In the Museum
Igor In the Museum time limit per test 1 second memory limit per test 256 megabytes input standard ...
- PTA 7-6 列出连通集(深搜+广搜)
给定一个有N个顶点和E条边的无向图,请用DFS和BFS分别列出其所有的连通集.假设顶点从0到N−1编号.进行搜索时,假设我们总是从编号最小的顶点出发,按编号递增的顺序访问邻接点. 输入格式: 输入第1 ...
- HDU 3666 THE MATRIX PROBLEM (差分约束 深搜 & 广搜)
THE MATRIX PROBLEM Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- poj3083 Children of the Candy Corn 深搜+广搜
这道题有深搜和广搜.深搜还有要求,靠左或靠右.下面以靠左为例,可以把简单分为上北,下南,左西,右东四个方向.向东就是横坐标i不变,纵坐标j加1(i与j其实就是下标).其他方向也可以这样确定.通过上一步 ...
- DFS-BFS(深搜广搜)原理及C++代码实现
深搜和广搜是图很多算法的基础,很多图的算法都是从这两个算法中启发而来. 深搜简单地说就是直接一搜到底,然后再回溯,再一搜到底,一直如此循环到没有新的结点. 广搜简单地说就是一层一层的搜,像水的波纹一样 ...
随机推荐
- EPiServer 简单项目总结
国内用到的EPiServer应该不多,所以记录点用到过的东西,以便分享 1.EPiServer office site http://www.episerver.com/ 2.EPiServer CM ...
- Java泛型介绍!!!
Java总结篇系列:Java泛型 转自:http://www.cnblogs.com/lwbqqyumidi/p/3837629.html 一. 泛型概念的提出(为什么需要泛型)? 首先,我们看下下 ...
- DB操作用法总结。
用到了慢慢总结.用到了随时更新. 其实可以看手册了.但是看了完了手册之后,还是记不住. 1. mysql select * from table where id in(1,2,3,3,4) 怎么能显 ...
- fabric 安装及使用
官网地址 1.安装 pip install fabric 依赖 Paramiko .PyCrypto库 以下依赖肯能要手动安装 #安装 pycrypto 密码库pip install pycrypto ...
- Js节点属性与方法
属性: Attributes 存储节点的属性列表(只读) childNodes 存储节点的子节点列表(只读) dataType 返回此节点的数据类型 Definition 以D ...
- c#中(int)、int.Parse()、int.TryParse、Convert.ToInt32的区别
本文来自:http://blog.csdn.net/tangjunping/article/details/5443337 以前经常为这几种数据类型转换方式而迷茫,这次为了彻底搞清它们之间的区别和优缺 ...
- 【MFC学习笔记-作业5-小数据库】【单选框,复选框,滚动条,列表框】
界面已经实现完毕. 要完成的操作就是1.性别分组(2选1) 2.属性勾选 3.年龄通过滚动条调整 4.职称通过下方的列表框选择 5.输入姓名 6.存入左方的列表框 7.当选择左方列表框的人时,可以显示 ...
- 【迪杰斯特拉双关键字最短路】【HDU3790】【最短路径问题】
题目大意: 给你n个点,m条无向边,每条边都有长度d和花费p,给你起点s终点t,要求输出起点到终点的最短距离及其花费,如果最短距离有多条路线,则输出花费最少的. 只需要再更新的时候判断一下就好 voi ...
- C# 利用反射动态调用类成员
用反射动态调用类成员,需要Type类的一个方法:InvokeMember.对该方法的声明如下(摘抄于MSDN): publicobject InvokeMember( string name, ...
- Android学习之sqlite与listview
在android系统中使用的是sqlite数据库,前面的简易登录系统已经讲述了数据库的应用.本例的重点是实现数据库与listview的绑定.demo的数据是将个人的信息绑定到listview中,并存在 ...