Educational Codeforces Round 1 (D) 搜索(优化剪枝)
2015-11-14 17:08
337 查看
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 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.
Output
Print k integers — the maximum number of pictures, that Igor can see if he starts in corresponding position.
Sample test(s)
input
output
input
output
题意:一个人在博物馆看画,他只能看到相邻的画(上下左右),人可以移动,“.”代表地面,“*”代表墙(墙上有画),问当人在某一个点,他最多可以看到多少画(人是可以移动的,不可以穿墙)。
题解:好吧,这一题敲了接近3个小时,
![](http://static.blog.csdn.net/xheditor/xheditor_emot/default/wail.gif)
,不过终于还是优化出来了,开始题意看错了
![](https://oscdn.geek-share.com/Uploads/Images/Content/201707/56e56926852ec0c53514c80d0ebce71b.gif)
不说了。
1.开始当然是想到暴力搜索,搞起来,每查询一次,就搜一次。提交超时,一下为超时代码
2。我想不行啊,每次都搜一遍太耗时间了,先吧所有的情况都搜出来,打个表,直接输出。又 超时了。。。超时代码
3.好了,思考了一下发现,在同一个区域的点根本不需要重新搜,可以直接把这个区域当成一个集合(一个块),在这个集合里的所有的点的答案都是一样的啊。这样一个区域块就只需要搜一遍了,减去了无用点的搜索。嗯,于是这样处理,在搜索过程中把这个区域里的所有的点的坐标信息都存入数组里,然后搜索结束一一对这些点坐标赋值,然后终于是过了,一下AC代码
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 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.
Output
Print k integers — the maximum number of pictures, that Igor can see if he starts in corresponding position.
Sample test(s)
input
5 6 3 ****** *..*.* ****** *....* ****** 2 2 2 5 4 3
output
6 4 10
input
4 4 1 **** *..* *.** **** 3 2
output
8
题意:一个人在博物馆看画,他只能看到相邻的画(上下左右),人可以移动,“.”代表地面,“*”代表墙(墙上有画),问当人在某一个点,他最多可以看到多少画(人是可以移动的,不可以穿墙)。
题解:好吧,这一题敲了接近3个小时,
![](http://static.blog.csdn.net/xheditor/xheditor_emot/default/wail.gif)
,不过终于还是优化出来了,开始题意看错了
![](https://oscdn.geek-share.com/Uploads/Images/Content/201707/56e56926852ec0c53514c80d0ebce71b.gif)
不说了。
1.开始当然是想到暴力搜索,搞起来,每查询一次,就搜一次。提交超时,一下为超时代码
#include<iostream> #include<cstdio> #include<cstring> #include<vector> #include<algorithm> using namespace std; #define N 1005 char s ; int vis ; int n,m; struct point { int x,y; point (int _x,int _y):x(_x),y(_y){} point (){} }; vector<point>eg; vector<point>eg2; int vx[]={0,0,1,-1}; int vy[]={1,-1,0,0}; int ans; void dfs(int x,int y) { if(s[x][y]=='*') { ans++; return ; } else if(s[x][y]=='#') { return ; } else if (s[x][y]=='.') { eg2.push_back(point(x,y)); s[x][y]='#'; for(int i=0;i<4;i++) { int xx=x+vx[i],yy=y+vy[i]; if(xx<0||yy<0||xx>=n||yy>=m)continue; dfs(xx,yy); } } return ; } int main() { int k; int x,y; scanf("%d%d%d",&n,&m,&k); for(int i=0;i<n;i++) scanf("%s",s[i]); while(k--) { eg2.clear(); eg.clear(); ans=0; scanf("%d%d",&x,&y); dfs(x-1,y-1); printf("%d\n",ans); for(int i=0;i<eg.size();i++) { s[eg[i].x][eg[i].y]='*'; } for(int i=0;i<eg2.size();i++) { s[eg2[i].x][eg2[i].y]='.'; } } return 0; }
2。我想不行啊,每次都搜一遍太耗时间了,先吧所有的情况都搜出来,打个表,直接输出。又 超时了。。。超时代码
#include<iostream> #include<cstdio> #include<cstring> #include<vector> #include<algorithm> using namespace std; #define N 2005 char s ; int n,m; struct point { int x,y; point (int _x,int _y):x(_x),y(_y){} point (){} }; vector<point>eg; vector<point>eg2; int vx[]={0,0,1,-1}; int vy[]={1,-1,0,0}; int answer ; int ans; void dfs(int x,int y) { if(s[x][y]=='*') { ans++; return ; } else if(s[x][y]=='#') { return ; } else if (s[x][y]=='.') { eg2.push_back(point(x,y)); s[x][y]='#'; for(int i=0;i<4;i++) { int xx=x+vx[i],yy=y+vy[i]; if(xx<0||yy<0||xx>=n||yy>=m)continue; dfs(xx,yy); } } return ; } int main() { int k; int x,y; scanf("%d%d%d",&n,&m,&k); for(int i=0;i<n;i++) scanf("%s",s[i]); for(int i=0;i<n;i++) for(int j=0;s[i][j];j++) if(s[i][j]=='.') { eg2.clear(); ans=0; dfs(i,j); answer[i][j]=ans; for(int i=0;i<eg2.size();i++) { s[eg2[i].x][eg2[i].y]='.'; } } while(k--) { scanf("%d%d",&x,&y); printf("%d\n",answer[x-1][y-1]); } return 0; }
3.好了,思考了一下发现,在同一个区域的点根本不需要重新搜,可以直接把这个区域当成一个集合(一个块),在这个集合里的所有的点的答案都是一样的啊。这样一个区域块就只需要搜一遍了,减去了无用点的搜索。嗯,于是这样处理,在搜索过程中把这个区域里的所有的点的坐标信息都存入数组里,然后搜索结束一一对这些点坐标赋值,然后终于是过了,一下AC代码
#include<iostream> #include<cstdio> #include<cstring> #include<vector> #include<algorithm> using namespace std; #define N 2005 char s ; int n,m; struct point { int x,y; point (int _x,int _y):x(_x),y(_y){} point (){} }; vector<point>eg; vector<point>eg2; int vx[]={0,0,1,-1}; int vy[]={1,-1,0,0}; int answer ; int ans; void dfs(int x,int y) { if(s[x][y]=='*') { ans++; return ; } else if(s[x][y]=='#') { return ; } else if (s[x][y]=='.') { eg2.push_back(point(x,y)); s[x][y]='#'; for(int i=0;i<4;i++) { int xx=x+vx[i],yy=y+vy[i]; if(xx<0||yy<0||xx>=n||yy>=m)continue; dfs(xx,yy); } } return ; } int main() { int k; int x,y; scanf("%d%d%d",&n,&m,&k); for(int i=0;i<n;i++) scanf("%s",s[i]); for(int i=0;i<n;i++) for(int j=0;s[i][j];j++) if(s[i][j]=='.') { eg2.clear(); ans=0; dfs(i,j); answer[i][j]=ans; for(int i=0;i<eg2.size();i++) { answer[eg2[i].x][eg2[i].y]=ans; } } while(k--) { scanf("%d%d",&x,&y); printf("%d\n",answer[x-1][y-1]); } return 0; }
相关文章推荐
- struct和typedef struct彻底明白了
- 多线程编程之二——MFC中的多线程开发
- 指针作为函数参数传递
- CentOS 7 WiFi连接
- 遍历容器 Java风格和STL风格 foreach
- phpstorm常用快捷键
- 杭电1072 猜数字
- 用C语言编写函数,实现strlen计算字符串长度的功能
- php工作目录
- cenos7.1 安装openstack kilo 心得之三添加网络时间协议
- VC多线程编程之一——问题提出
- return *this和return this有什么区别?
- vertical-align属性
- 划分型动态规划 之 CODE[VS] 1039 数的划分 2001年NOIP全国联赛提高组
- 总结iOS 多线程学习过程四
- Linux下自定义字符串数组作为文件名,创建文件
- Spark修炼之道(基础篇)——Linux大数据开发基础:第十节:Shell编程入门(二)
- 安卓实现简单的发送短信功能
- java基础一
- 旭说数据结构之散列表(哈希表)