bfs最短路。

写的真丑。。。

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<iostream>
using namespace std;
const int maxn = 50;
const int INF = 0x3f3f3f3f;
const int dx[]={1,0,-1,0};
const int dy[]={0,1,0,-1}; int n,m,T;
int a[maxn][maxn],id[maxn][maxn],vid;
char s[maxn];
int xx,x2,yy,y2,h,d[1000][maxn][maxn];
int ans;
double res; struct Point {
int x,y;
} q[1000]; void bfs(int x,int y) {
h=id[x][y]=++vid;
int l=0,r=1;
q[0].x=x; q[0].y=y;
if(a[x][y]) d[h][x][y]=1;
else d[h][x][y]=0;
while(l<r) {
xx=q[l].x,yy=q[l].y; l++;
for(int i=0;i<4;i++) {
x2=xx+dx[i];
y2=yy+dy[i];
if(x2<1 || x2>n || y2<1 || y2>m) continue;
if(a[x2][y2]) {
if(d[h][x2][y2] > d[h][xx][yy]+1) {
d[h][x2][y2]=d[h][xx][yy]+1;
if(d[h][x2][y2]<=T) {
q[r].x=x2;
q[r].y=y2;
r++;
}
}
}
else if(d[h][x2][y2]>d[h][xx][yy]) {
d[h][x2][y2]=d[h][xx][yy];
if(d[h][x2][y2]<=T) {
q[r].x=x2;
q[r].y=y2;
r++;
}
}
}
}
} inline int sqr(int x) {
return x*x;
} int cal(int x1,int y1,int x2,int y2) {
return sqr(x1-x2)+sqr(y1-y2);
} int main() {
memset(d,0x3f,sizeof(d));
scanf("%d%d%d",&n,&m,&T);
for(int i=1;i<=n;i++) {
scanf("%s",s+1);
for(int j=1;j<=m;j++) if(s[j]=='1') a[i][j]=1;
else a[i][j]=0;
}
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
bfs(i,j);
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++) {
h=id[i][j];
for(int x=1;x<=n;x++)
for(int y=1;y<=m;y++)
if(d[h][x][y]<=T) ans=max(ans,cal(i,j,x,y));
}
res=sqrt(ans);
printf("%0.6f\n",res);
return 0;
}

bzoj1295: [SCOI2009]最长距离的更多相关文章

  1. [BZOJ1295][SCOI2009]最长距离 最短路+枚举

    1295: [SCOI2009]最长距离 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1683  Solved: 912[Submit][Statu ...

  2. BZOJ1295 [SCOI2009]最长距离 最短路 SPFA

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ1295 题意概括 有一块矩形土地,被分为 N*M 块 1*1 的小格子. 有的格子含有障碍物. 如果 ...

  3. 【spfa】bzoj1295 [SCOI2009]最长距离

    题意:给你一个n*m的点阵.有些点是障碍,求一个欧几里得距离最大的点对(A,B),使得在移走的障碍≤T的情况下,可以从A走到B. 建图,跑n*m次spfa,求出从 每个点 出发到 其他所有点 的 经过 ...

  4. 题解 [BZOJ1295][SCOI2009] 最长距离

    题面 解析 \(n\)只有\(30\)可以直接枚举每个矩形, 判断他们的左上角到右下角或右上角到左上角的最短路是否小于\(T\). 最短路可以用\(dijkstra\). 一开始想用\(DP\)写最短 ...

  5. 【BZOJ1295】[SCOI2009]最长距离(最短路)

    [BZOJ1295][SCOI2009]最长距离(最短路) 题面 BZOJ 洛谷 题解 这题很妙啊. 我们枚举一个点,只需要考虑到他的最远点就行了,显然只需要考虑一个点即可.那么这两个点之前联通的最小 ...

  6. BZOJ 1295: [SCOI2009]最长距离 spfa

    1295: [SCOI2009]最长距离 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=1295 Description windy有一块 ...

  7. bzoj 1295: [SCOI2009]最长距离

    题目链接 1295: [SCOI2009]最长距离 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1165  Solved: 619[Submit][ ...

  8. BZOJ 1295: [SCOI2009]最长距离( 最短路 )

    把障碍点看做点(边)权为1, 其他为0. 对于每个点跑spfa, 然后和它距离在T以内的就可以更新答案 ------------------------------------------------ ...

  9. 1295: [SCOI2009]最长距离

    1295: [SCOI2009]最长距离 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 960  Solved: 498[Submit][Status ...

随机推荐

  1. [牛感悟系列]JAVA(1)理解JAVA垃圾回收

    理解JAVA垃圾回收的好处是什么?满足求知欲是一方面,编写更好的JAVA应用是另外一方面. 如果一个人对垃圾回收过程感兴趣,那表明他在应用程序开发领域有相当程度的经验.如果一个人在思考如何选择正确的垃 ...

  2. dapper.rainbow

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...

  3. 如何应用CLR线程池来管理多线程

        class Program     {         static void Main(string[] args)         {             int intWorkerT ...

  4. 【BZOJ3262】 陌上花开

    Description 有n朵花,每朵花有三个属性:花形(s).颜色(c).气味(m),又三个整数表示.现要对每朵花评级,一朵花的级别是它拥有的美丽能超过的花的数量.定义一朵花A比另一朵花B要美丽,当 ...

  5. linux编程之线性表

    #include"stdio.h" #define MAX 100 typedef struct List{ int length; int num[MAX]; }List_seq ...

  6. linux驱动系列之makefile

    在linux环境下做嵌入式无论是编写应用程序还是驱动程序等等,都需要用make来进行程序的编译,就需要学会自己编写Makefile.Makefile主要的作用有3点:1.决定编译哪些文件 2.怎样编译 ...

  7. 为Dapper编写一个类似于EF的Map配置类

    引言 最近在用Dapper处理Sqlite.映射模型的时候不喜欢用Attribute配置,希望用类似EF的Map来配置,所以粗略的实现了一个. 实现 首先是主体的配置辅助类型: using Syste ...

  8. [BEC][hujiang] Lesson02 Unit1:Working life ---Reading

    2 1.1Working Life p7 reading attitudes to work Question6: 对于Attitude问题 1 I be willing/ unwilling to ...

  9. Hybrid App 和 React Native 开发那点事

    简介:Hybrid App(混合模式移动应用)开发是指介于Web-app.Native-App这两者之间的一种开发模式,兼具「Native App 良好用户交互体验的优势」和「Web App 跨平台开 ...

  10. Spring MVC Checkbox And Checkboxes Example

    In Spring MVC, <form:checkbox /> is used to render a HTML checkbox field, the checkbox values ...