Educational Codeforces Round 1 (D) 搜索(优化剪枝)
来源:互联网 发布:上海证券软件下载 编辑:程序博客网 时间:2024/05/21 15:46
题意:一个人在博物馆看画,他只能看到相邻的画(上下左右),人可以移动,“.”代表地面,“*”代表墙(墙上有画),问当人在某一个点,他最多可以看到多少画(人是可以移动的,不可以穿墙)。
题解:好吧,这一题敲了接近3个小时,,不过终于还是优化出来了,开始题意看错了不说了。
1.开始当然是想到暴力搜索,搞起来,每查询一次,就搜一次。提交超时,一下为超时代码
#include<iostream>#include<cstdio>#include<cstring>#include<vector>#include<algorithm>using namespace std;#define N 1005char s[N][N];int vis[N][N];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 2005char s[N][N];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[N][N];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 2005char s[N][N];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[N][N];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;}
0 0
- Educational Codeforces Round 1 (D) 搜索(优化剪枝)
- Educational Codeforces Round 21 D ( 二分 )
- Educational Codeforces Round 21-D
- Educational Codeforces Round 5 D
- Educational Codeforces Round 26 D
- Codeforces-Educational Codeforces Round 32-(A,B,C,D)
- Educational Codeforces Round 26 D Round Subset
- Educational Codeforces Round 1
- Educational Codeforces Round 1 D. Igor In the Museum
- Educational Codeforces Round 1 D. Igor In the Museum
- Educational Codeforces Round 1 D.Igor In the Museum(DFS)
- Educational Codeforces Round 7(D)构造(STL deque)
- Educational Codeforces Round 9(D. Longest Subsequence(筛法))
- Educational Codeforces Round 5(D)尺取+SET
- Educational Codeforces Round 5 (D. Longest k-Good Segment)
- Educational Codeforces Round 7 D. Optimal Number Permutation(构造)
- Educational Codeforces Round 8 D. Magic Numbers(数位dp)
- CF Educational Codeforces Round 15(A~D)
- 上传APP store 流程
- c++primer文本查询系统
- json文件变成字符串传到后台
- Java集合Set、Map、HashSet、HashMap、TreeSet、TreeMap等
- Teamviewer QuickSupport - 在电脑上远程连接控制手机/平板的软件神器 (Android/iOS)
- Educational Codeforces Round 1 (D) 搜索(优化剪枝)
- 浅谈UML的概念和模型之UML类图关系
- iOS打包IPA 文件
- docker for Mac OS
- 9、内存管理初级
- java开发环境 jdk环境变量配置
- javascript正则表达式
- 黑盒测试和白盒测试区别
- C# Linq 未将对象引用设置到对象的实例