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++代码实现
深搜和广搜是图很多算法的基础,很多图的算法都是从这两个算法中启发而来. 深搜简单地说就是直接一搜到底,然后再回溯,再一搜到底,一直如此循环到没有新的结点. 广搜简单地说就是一层一层的搜,像水的波纹一样 ...
随机推荐
- 闲来瞎扯 -- 在vs2008下编写linux程序
虽说vim很强大,但是个人感觉器代码提示功能不如visual assist来的强大.如何使用visual assist来实现代码的提示功能呢? 首先说明我的环境 : 宿主机是xp(O(∩_∩)O~还是 ...
- 100个linux站点
(一) 文件下载 (二) 幽默娱乐 (三) 相关新闻 (四) 通用硬体 (五) 专用硬体 (六) 新手站点 (七) 图形/多媒体 (八) 游戏站点 (九) 网路杂志 (十) 入口(教育.链结) (十一 ...
- C++模板类中使用静态成员变量(例如Singleton模式)
一个最简单Singleton的例子: ///////// Test.h /////////template <class _T>class CTest{private:_T n;stati ...
- 顶尖数据挖掘教学案例库(TipDM-C10)产品白皮书
顶尖数据挖掘教学案例库 (TipDM-C10) 产 品 说 明 书 广州泰迪智能科技有限公司 版权所有 地址: 广州市经济技术开发区科学城232号 网址: ht ...
- Unable to resolve target 'android-XX'的问题解决
1.问题现象(Unable to resolve target 'android-XX') 将低版本的代码导入eclipse时,常遇到这样的问题:Unable to resolve target 'a ...
- Android工程师面试准备知识点
听+7哥说,把下面的全弄懂,面试必过,所以我就试着去把所有题目补充了一下,各位能耐的网友们,如果有吐槽或者补充的尽管给我留言,在这里谢过大家了. 1. android的多线程如何通信 答:Andr ...
- android 自定义圆形进度条
一.通过动画实现 定义res/anim/loading.xml如下: [html] view plain copy print ? <?xml version="1.0" ...
- ORA-32004: obsolete or deprecated parameter(s) specified for RDBMS instance
1.启动报错SQL> startupORA-32004: obsolete or deprecated parameter(s) specified for RDBMS instanceORAC ...
- ubuntu高版本环境变量问题
在ubuntu较高的版本(博主使用的是14.10)下,可以在/etc/environment中配置环境变量,这个环境变量可能会覆盖用户~/.bashrc中配置的环境变量,这个应该引起注意.
- L13 DNS
DNS: 根 root 分布式. 服务于终端客户,也服务于其他dns服务器.对其他dns服务器提供数据 是域名资料数据库,也是解析服务提供者.用户接入进来,可以得到解析的服务 仅管理下一级dns服务器 ...