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 这题主要是叫我们求出树的直径,在求树的直径之前要先判断一下有没有环 树的直径指的就是一棵树上面距 ...
随机推荐
- MySQL的索引单表优化案例分析
建表 建立本次优化案例中所需的数据库及数据表 CREATE DATABASE db0206; USE db0206; CREATE TABLE `db0206`.`article`( `id` INT ...
- github blog
git version 2.18.0.windows.1 node-v10.8.0-win-x64.zip 1 安装node.js,直接下载,配置环境变量即可(win10重启生效) 2 git安装,略 ...
- ASPxGridView 下拉框不让输入
DropDownStyle="DropDownList"该属性使combox控件不能手动输入数据,只能在下拉列表中选择
- 51nod1478(yy)
题目链接: http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1478&judgeId=365133 题意: 中文题诶 ...
- [转]关于TDD、BDD和DDD的一些看法
在实际的项目中,我们可能随时面对各种不同的需求,它的各个方面的要素决定了我们所采用的开发模式. 比如,它的复杂度如何?所有的需求是否足够清晰?开发人员对相关的业务是否足够了解?项目的工期是否合理?种种 ...
- Eclipse中使用Spring IOC容器的具体方法
1.通过IOC容器创建对象,并为属性赋值 在IOC容器本身对象创建时(xml文件加载时),会将配置文件中配置好的bean先创建出来,按照xml文件中配置的先后顺序创建 <bean id=&quo ...
- Drop user 报ORA-00600 [KTSSDRP1]
一客户删除一个数据库用户THH时报错: 说明在获取seg$时没有找到相应的条目,先来解释下这个600错误的参数含义: Arg [a] Tablespace number Arg [b] File nu ...
- react PropTypes 与 DefaultProps
PropTypes 与 DefaultProps import React ,{ Component } from 'react'; import PropTypes from 'prop-types ...
- SQL Server 清理日志
USE[master] GO ALTER DATABASE 要清理的数据库名称 SET RECOVERY SIMPLE WITH NO_WAIT GO ALTER DATABASE 要清理的数据库名称 ...
- java中检测网络是否相通
转载:https://www.cnblogs.com/moon-jiajun/p/3454576.html 1 package com.cjj.client; 2 3 import java.io.I ...