HDU 4543
这道题感觉很坑。。不过,注意一些小问题。
参考http://www.cnblogs.com/Lattexiaoyu/archive/2013/03/31/2992553.html改进了原来自己的复杂度。
当无被占领时,其实枚举边缘也可以。但有计算公式,直接用了。
当有占领时,BFS出每个空的格到被占领的最小距离,然后枚举求出最小的D。
1)起先自己也是如上面的做,但做法不够优美T了。BFS时,起初是把占领的和未占的分开存枚举计算。其实这样复杂度就很高了。直接按一般的BFS就可以了,因为先到达空格的必定是距离短的,所以,复杂度变成了o(NM)。
2)枚举求D也有小技巧。直接三重循环必须T。可以改为枚举D,三重循环判断D是否符合要求,符合则D++,不符合即能得到答案了。
为什么不符合就能得到答案?因为排序后的点当枚举到q时,剩下的三重循环会把所有情况都列举到,所以不符合D-1就是解
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
int n,m; struct Node{
int x,y,dis;
Node(){}
Node(int xx,int yy,int d){x=xx,y=yy,dis=d;}
}que[10300],dic[5300];
int cd,head,tail;
char s[100];
int dir[4][2]={
{0,1},
{0,-1},
{1,0},
{-1,0}
}; int cal(int i,int j,int x,int y){
return abs(i-x)+abs(j-y);
} int map[80][80]; int uslove(){/*
int ans=1;
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
for(int k=0;k<m;k++){
int t=min(cal(0,i,j,m-1),cal(j,m-1,n-1,k));
ans=max(ans,min(cal(0,i,n-1,k),t));
}
for(int k=0;k<n;k++){
int t=min(cal(0,i,j,m-1),cal(j,m-1,k,0));
ans=max(ans,min(cal(0,i,k,0),t));
}
}
}
// printf("%d\n",ans);*/
// return ans;
if(n == 1) return m / 3;
if(m == 1) return n / 3;
return (2*n + 2*m - 4)/3;
} bool cmp(Node a,Node b){
if(a.dis<b.dis) return true;
return false;
} bool check(int x,int y){
if(x>=0&&x<n&&y>=0&&y<m&&map[x][y]==0) return true;
return false;
} void BFS(){
/* for(int i=0;i<cd;i++){
dic[i].dis=200;
for(int j=0;j<co;j++){
dic[i].dis=min(dic[i].dis,cal(dic[i].x,dic[i].y,occ[j].x,occ[j].y));
// cout<<dis[i]<<endl;
}
}*/
Node p,tmp;
while(head<tail){
p=que[head++];
for(int i=0;i<4;i++){
tmp.x=p.x+dir[i][0];
tmp.y=p.y+dir[i][1];
if(check(tmp.x,tmp.y)){
tmp.dis=p.dis+1;
map[tmp.x][tmp.y]=tmp.dis;
dic[cd++]=Node(tmp.x,tmp.y,p.dis);
que[tail++]=tmp;
}
}
}
} void slove(){
/*
int ans=-1;
int t;
for(int i=0;i<cd;i++){
if(dis[i]<ans) continue;
for(int j=i+1;j<cd;j++){
t=min(dis[j],dis[i]);
if(t<ans) continue;
for(int k=j+1;k<cd;k++){
int t1=min(dis[k],t);
if(t1<ans) continue;
t1=min(t1,cal(dic[i].x,dic[i].y,dic[j].x,dic[j].y));
t1=min(t1,cal(dic[i].x,dic[i].y,dic[k].x,dic[k].y));
t1=min(t1,cal(dic[j].x,dic[j].y,dic[k].x,dic[k].y));
ans=max(t1,ans);
}
}
}
printf("%d\n",ans);*/
sort(dic,dic+cd,cmp);
int q=0,d=1;
while(q<cd){
bool flag=false;
for(int i=q;i<cd;i++){
for(int j=i+1;j<cd;j++){
if(cal(dic[i].x,dic[i].y,dic[j].x,dic[j].y)<d) continue;
for(int k=j+1;k<cd;k++){
if(cal(dic[i].x,dic[i].y,dic[k].x,dic[k].y)>=d&&cal(dic[k].x,dic[k].y,dic[j].x,dic[j].y)>=d){
flag=true;
break;
}
}
if(flag) break;
}
if(flag) break;
}
if(!flag) break;
d++;
while(q<cd&&dic[q].dis<d)
q++;
}
printf("%d\n",d-1);
} int main(){
int T,icase=0;
scanf("%d",&T);
while(T--){
scanf("%d%d",&n,&m);
cd=0; int cc=0;
head=tail=0;
memset(map,0,sizeof(map));
for(int i=0;i<n;i++){
scanf("%s",s);
for(int j=0;j<m;j++){
if(s[j]=='F'){
map[i][j]=1;
que[tail++]=Node(i,j,1);
}
else cc++;
}
}
printf("Case %d: ",++icase);
if(cd==n*m){
printf("%d\n",uslove());
}
else{
BFS();
slove();
}
}
return 0;
}
HDU 4543的更多相关文章
- hdu 2594 Simpsons’ Hidden Talents(KMP入门)
Simpsons’ Hidden Talents Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java ...
- HDOJ 2111. Saving HDU 贪心 结构体排序
Saving HDU Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- 【HDU 3037】Saving Beans Lucas定理模板
http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...
- hdu 4859 海岸线 Bestcoder Round 1
http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...
- HDU 4569 Special equations(取模)
Special equations Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u S ...
- HDU 4006The kth great number(K大数 +小顶堆)
The kth great number Time Limit:1000MS Memory Limit:65768KB 64bit IO Format:%I64d & %I64 ...
- HDU 1796How many integers can you find(容斥原理)
How many integers can you find Time Limit:5000MS Memory Limit:32768KB 64bit IO Format:%I64d ...
- hdu 4481 Time travel(高斯求期望)(转)
(转)http://blog.csdn.net/u013081425/article/details/39240021 http://acm.hdu.edu.cn/showproblem.php?pi ...
- HDU 3791二叉搜索树解题(解题报告)
1.题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=3791 2.参考解题 http://blog.csdn.net/u013447865/articl ...
随机推荐
- Java经典算法之冒泡排序(Bubble Sort)
原理:比较相邻的两个值,将值大的元素交换至右端 思路:依次比较相邻的两个数,将小数放在前面,大数放在后面.即在第一趟:首先比较第1个和第2个数,将小数放前,大数放后.然后比较第2个数和第3个数,将小数 ...
- 题解报告:hdu 1232 畅通工程(并查集)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1232 Problem Description 某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了 ...
- 题解报告:hdu 1162 Eddy's picture
Problem Description Eddy begins to like painting pictures recently ,he is sure of himself to become ...
- 笨拙而诡异的 Oracle
有这样一段 SQL 代码: 通过 C# 获取查询结果: SQL 代码中有两个参数,且都是字符串类型,以上的 C# 代码是生成 Oracle SQL 代码所需要的参数.运行结果如下: 居然发生 ...
- 阿里云虚拟主机针对恶意频繁攻击式访问造成CPU爆满的解决方法
最近网站CPU经常爆满,到阿里云提交了工单,工程师给我的处理意见: 您好,虚拟主机CPU占用比较高通常这种情况有两种可能: 一是网站应用程序代码逻辑较复杂,或业务架构效率比较低,在请求了某个网 ...
- c++枚举变量初始值
#include <iostream> // std::cout, std::boolalpha, std::noboolalpha enum foo { c = -1, a = 1, b ...
- (转) Hibernate检索方式概述
http://blog.csdn.net/yerenyuan_pku/article/details/70554816 Hibernate检索方式概述 我们在对数据库的操作中,最常用的是select, ...
- 抓包工具Fiddler及Charles
一.抓包工具介绍 1.charles抓包如何抓取手机端数据包(安卓手机) (1)获取pc的IP地址 (2)打开charles里的[Proxy]-[Proxy setting],设置端口号,默认为888 ...
- R语言图表
条形图 在R语言中创建条形图的基本语法是 barplot(H, xlab, ylab, main, names.arg, col) H是包含在条形图中使用的数值的向量或矩阵 xlab是x轴的标签 yl ...
- vue项目中使用百度地图的方法
1.在百度地图申请密钥: http://lbsyun.baidu.com/ 将 <script type="text/javascript" src="http: ...