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++代码实现
深搜和广搜是图很多算法的基础,很多图的算法都是从这两个算法中启发而来. 深搜简单地说就是直接一搜到底,然后再回溯,再一搜到底,一直如此循环到没有新的结点. 广搜简单地说就是一层一层的搜,像水的波纹一样 ...
随机推荐
- nginx grok 正则错误的输出情况
nginx 配置: http { include mime.types; default_type application/octet-stream; log_format main '$http_h ...
- URL组成介绍
1.2. HTTP request ----------------- First, let's consider this HTTP request : Line Contents number 1 ...
- Unix/Linux环境C编程入门教程(22) C/C++如何获取程序的运行时间
1.问:知道程序运行时间我们可以做什么? 在<C++应用程序性能优化>一书中,如果大家读过相信大家一定对性能优化这一块非常上心,文中总是对优化前后的时间对比非常直观给我们一个感受. 那么我 ...
- Unity 之 Redux 模式(第一篇)—— 人物移动
作者:软件猫 日期:2016年12月6日 转载请注明出处:http://www.cnblogs.com/softcat/p/6135195.html 在朋友的怂恿下,终于开始学 Unity 了,于是有 ...
- OC中如何把字典中的数据拼接成url字符串
在使用objective-c语言开发iOS应用中,会向服务器通过URL请求一些数据,因此对URL的拼接肯定少不了.而在iOS中,我们一般是通过将字典中的数据拼接成我们要请求的URL字符串,那这个是怎么 ...
- Duanxx的STM32学习:STM32下载方式选择
前几天熟悉了STM32的启动方式.主要由Boot0和Boot1设置 如今须要解决的就是STM32的下载的问题. 一開始的时候,我选择的是SWD下载.这样的下载方式须要Boot0=0.Boot1=0.占 ...
- 微信朋友圈分享js代码最新2015年无错版
最近微信对分享做了进一步规范,导致很多分享都不起作用了,今天跟大家分享,2015年最新修无错的! 以下是主要微信分享页面代码:(其中红色部分主要懒友自己填写自己哈.) <?php require ...
- IntPtr问题
public aaa(IntPtr myPtr,int left, int top, int width, short height) 这里myPtr应该是对应到一块内存,你需要查看aaa函数是如何把 ...
- WindowsForm 记事本 对话框
textbox: 属性: text:文本 selectedtext:获取或设置选中文本 canundo:是否能够撤销 方法: ...
- form表单中经常用到的禁用获取值问题
<input name="country" id="country" size=12 value="disabled提交时得不到该值 " ...