URAL 1145—— Rope in the Labyrinth——————【求树的直径】
Time Limit:500MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
Description
Input
Output
Sample Input
| input | output |
|---|---|
7 6 |
8 |
题目大意:给你一个图,"."表示你可以走,"#“表示墙不能走,每个格子都有一个钩。任意两个格子之间只有一条路,现在问你最短能让所有"."的格子中的钩能用绳子连接的绳子长度。
解题思路:其实就是让你求树的直径的。由"."构成的是一棵树,然后求树的直径,两次广搜即可。
#include<stdio.h>
#include<algorithm>
#include<queue>
#include<string.h>
using namespace std;
typedef long long LL;
const int maxn = 900;
char Map[maxn][maxn];
bool vis[maxn][maxn];
int f[4][2] = {{1,0},{0,1},{-1,0},{0,-1}};
struct Node{
int x,y,step;
};
queue<Node>Q;
bool jud(int i,int j){
Node st;
int ret = 0;
if(Map[i-1][j] == '#'){
ret++;
}
if(Map[i][j-1] == '#'){
ret++;
}
if(Map[i+1][j] == '#'){
ret++;
}
if(Map[i][j+1] == '#'){
ret++;
}
if(ret >= 3){
st.x = i, st.y = j, st.step = 0;
Q.push(st);
return true;
}
return false;
}
int n,m;
Node BFS(){
Node st,tmp,en;
en = Q.front();
vis[en.x][en.y] = 1;
while(!Q.empty()){
st = Q.front();
Q.pop();
if(st.step > en.step){
en = st;
}
int tmpx ,tmpy;
for(int i = 0; i < 4; i++){
tmp.x = st.x + f[i][0];
tmp.y = st.y + f[i][1];
if(tmp.x <= 0 ||tmp.x > m ||tmp.y <= 0 || tmp.y > n || Map[tmp.x][tmp.y] =='#' ||vis[tmp.x][tmp.y]){
continue;
}
vis[tmp.x][tmp.y] = 1;
tmp.step = st.step + 1;
Q.push(tmp);
}
}
return en;
}
int main(){
while(scanf("%d%d",&n,&m)!=EOF){
while(!Q.empty()) Q.pop();
for(int i = 1; i <= m; i++){
getchar();
for(int j = 1; j <= n; j++){
scanf("%c",&Map[i][j]);
}
}
int flag = 0;
for(int i = 1; i <= m; i++){
if(flag) break;
for(int j = 1; j <= n; j++){
if(Map[i][j] == '.' && flag == 0){
flag = jud(i,j);
}
if(flag) break;
}
}
Node st = BFS();
memset(vis,0,sizeof(vis));
// printf("%d %d %d+++\n",st.x,st.y,st.step);
st.step = 0;
Q.push(st);
Node en = BFS();
printf("%d\n",en.step);
}
return 0;
}
URAL 1145—— Rope in the Labyrinth——————【求树的直径】的更多相关文章
- ural 1145. Rope in the Labyrinth
1145. Rope in the Labyrinth Time limit: 0.5 secondMemory limit: 64 MB A labyrinth with rectangular f ...
- poj2631 求树的直径裸题
题目链接:http://poj.org/problem?id=2631 题意:给出一棵树的两边结点以及权重,就这条路上的最长路. 思路:求实求树的直径. 这里给出树的直径的证明: 主要是利用了反证法: ...
- poj1985 Cow Marathon (求树的直径)
Cow Marathon Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 3195 Accepted: 1596 Case ...
- hdu 4607 Park Visit 求树的直径
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4607 题目大意:给你n个点,n-1条边,将图连成一棵生成树,问你从任意点为起点,走k(k<=n) ...
- [USACO2004][poj1985]Cow Marathon(2次bfs求树的直径)
http://poj.org/problem?id=1985 题意:就是给你一颗树,求树的直径(即问哪两点之间的距离最长) 分析: 1.树形dp:只要考虑根节点和子节点的关系就可以了 2.两次bfs: ...
- 4612 warm up tarjan+bfs求树的直径(重边的强连通通分量)忘了写了,今天总结想起来了。
问加一条边,最少可以剩下几个桥. 先双连通分量缩点,形成一颗树,然后求树的直径,就是减少的桥. 本题要处理重边的情况. 如果本来就两条重边,不能算是桥. 还会爆栈,只能C++交,手动加栈了 别人都是用 ...
- HDU4612+Tarjan缩点+BFS求树的直径
tarjan+缩点+树的直径题意:给出n个点和m条边的图,存在重边,问加一条边以后,剩下的桥的数量最少为多少.先tarjan缩点,再在这棵树上求直径.加的边即是连接这条直径的两端. /* tarjan ...
- hdoj 4612 Warm up【双连通分量求桥&&缩点建新图求树的直径】
Warm up Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Total Su ...
- 求树的直径+并查集(bfs,dfs都可以)hdu4514
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4514 这题主要是叫我们求出树的直径,在求树的直径之前要先判断一下有没有环 树的直径指的就是一棵树上面距 ...
随机推荐
- Memcached Cache
using System; using System.Collections.Generic; using System.Linq; using System.Web; using Memcached ...
- SQLite 如何清空表数据并将递增量归零
SQLite并不支持TRUNCATE TABLE语句 方式一: DELETE FROM [Tab_User] --不能将递增数归零 方式二: DELETE FROM sqlite_sequence W ...
- 使用KubeAdm部署Kubernetes集群——如何访问google代码仓库及Yum源
一.申请国外服务器,部署ShadowSock服务 1.下载并创建配置文件 wget https://github.com/shadowsocks/shadowsocks-go/releases/dow ...
- 最优的cuda线程配置
1 每个SM上面失少要有192个激活线程,寄存器写后读的数据依赖才能被掩盖 2 将 寄存器 的bank冲突降到最低,应尽量使每个block含有的线程数是64的倍数 3 block的数量应设置得 ...
- 从100PV到1亿级PV网站架构演变(转)
http://www.linuxde.net/2013/05/13581.html 一个网站就像一个人,存在一个从小到大的过程.养一个网站和养一个人一样,不同时期需要不同的方法,不同的方法下有共同的原 ...
- 树状数组【bzoj1103】: [POI2007]大都市meg
1103: [POI2007]大都市meg 在经济全球化浪潮的影响下,习惯于漫步在清晨的乡间小路的邮递员Blue Mary也开始骑着摩托车传递邮件了. 不过,她经常回忆起以前在乡间漫步的情景.昔日,乡 ...
- CF351A Jeff and Rounding 思维
Jeff got 2n real numbers a1, a2, ..., a2n as a birthday present. The boy hates non-integer numbers, ...
- linux下虚拟主机配置
<VirtualHost *:80>ServerAdmin admin@localhostServerName www.baidu.org DocumentRoot "/d ...
- Java基础笔记(十九)——抽象类abstract
抽象类作为父类,不能实例化自己类型的对象,但可以通过向上转型实例化子类对象. public abstract class Animal{ } 比如eat(); ,每个动物子类都应有自己的方法,那An ...
- java 在web应用中获取本地目录和服务器上的目录不一致的问题
先来讲讲我所遇到的问题.最近有个新的项目添加新的功能. 修改之后部署到服务器上面发现取到classpath目录跑到别的地方去了.在本地测试却正常. 当时毛的着火了.硬是想不懂什么问题. 终于发现了这个 ...