hdu5301and15年多校集训1002题(builds)(dfs)
来源:互联网 发布:mac必备软件 知乎 编辑:程序博客网 时间:2024/06/16 16:19
题目大意:给你矩形的长和宽,他被很多单位为1的小正方形组成,还有一个黑块给了它的坐标,代表窗户,把这个矩形分割,每个分割的块,挨着黑块,或者边沿才行
解题思路:一种是根据图形来拼接而成,一种是dfs(q神的代码)
注意:其实分割之后,只能是长条状的块(n*1),如果不是,则可以分割为长条状的块
方法一:
#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespace std;int n,m,x,y,r;int main(){ while(scanf("%d%d%d%d",&n,&m,&x,&y)!=EOF) { if(n>m) {swap(n,m);swap(x,y);} r=(n+1)/2; int c=min(y,m-y+1); int d=max(x-1,n-x); if(r<c&&x - 1 != n - x) r=min(c,d); if(n==m&&(n&1)&&x==y&&(x*2-1==n)) r=n/2; printf("%d\n",r); }}方法二:dfs(code from quailty)
#include<cstdio>//q神制造#include<cstring>#include<cstdlib>#include<cmath>#include<iostream>#include<algorithm>#include<set>using namespace std;const int d[4][2]={{1,0},{0,1},{-1,0},{0,-1}};const int INF=0x3f3f3f3f;int n,m,ans;set<pair<int,int> >mp;int get_dis(int x,int y,int dir){ if(dir==0)return n+1-x; if(dir==1)return m+1-y; if(dir==2)return x; if(dir==3)return y;}void dfs(int x,int y,int dir,int cur){ if(dir==4) { ans=min(ans,cur); return; } int tx=x+d[dir][0]; int ty=y+d[dir][1]; // if(tx>n||ty>m) return; for(int i=0;i<4;i++) { if(i==(dir+2)%4) continue;//如果是他的反方向 if(mp.find(make_pair(tx+d[i][0],ty+d[i][1]))==mp.end()) { mp.insert(make_pair(tx+d[i][0],ty+d[i][1])); dfs(x,y,dir+1,max(cur,get_dis(tx,ty,i))); mp.erase(make_pair(tx+d[i][0],ty+d[i][1])); } }}int main(){ int x,y; while(scanf("%d%d%d%d",&n,&m,&x,&y)!=EOF) { mp.clear(); ans=INF; dfs(x,y,0,0); if(n%2==1 && m%2==1 && x==(n+1)/2 && y==(m+1)/2) { } else ans=max(ans,min((n+1)/2,(m+1)/2)); printf("%d\n",ans); } return 0;}
0 0
- hdu5301and15年多校集训1002题(builds)(dfs)
- dfs+剪枝(暑假集训第三周周测E)
- 集训第一天(2017/7/31):深度优先搜索dfs专项练习
- hdu5323&15年多校集训(3)h题
- 压缩Gradle构建 (Squeezing your Gradle builds)
- 暑假集训(bellman)
- 暑期集训(首篇博文)
- 暑假集训--训练2 DFS
- 集训第二十六天(2017/8/25):集训总结
- (吐槽)寒假集训总结
- 暑假集训题目(bellman)
- 暑期集训-dp(1)
- 算法集训(一)水题
- 陕西省集训(数位dp)
- 陕西省集训(单调队列)
- 集训-可割点(树形DP)
- hdu5336&15年多校集训(4)j题(bfs+模拟)
- hdu5305and杭电多校集训第二题(1006题 friend)
- 多余字以省略号显示的css样式
- Linux 用户管理
- windows线程与进程的关系
- HDOJ 5306 Gorgeous Sequence 线段树
- 散列表之完美散列
- hdu5301and15年多校集训1002题(builds)(dfs)
- Mysql常用函数汇总
- Win7系统下修改VC6.0的字体
- final in Java DEMO
- js下 移动端底部下拉菜单
- cocos2dx中的内存管理机制
- MFC加载位图资源的实现
- 深入理解JavaI/O流
- 计算机编码世界(一)