SYSU暑假热身赛D

D - Rope in the Labyrinth

Time Limit:500MS     Memory Limit:65536KB     64bit IO Format:%I64d
& %I64u

Submit Status Practice URAL
1145

Description

A labyrinth with rectangular form and size m × n is divided into square cells with sides‘ length 1 by lines that are parallel with the labyrinth‘s sides. Each cell of the grid is either occupied or free. It
is possible to move from one free cell to another free cells that share a common side with the cell. One cannot move beyond the labyrinth‘s borders. The labyrinth is designed pretty specially: for any two cells there is only one way to move from one cell to
the other. There is a hook at each cell‘s center. In the labyrinth there are two special free cells, such that if you can connect the hooks of those two cells with a rope, the labyrinth‘s secret door will be automatically opened. The problem is to prepare
a shortest rope that can guarantee, you always can connect the hooks of those two cells with the prepared rope regardless their position in the labyrinth.

Input

The first line contains integers n and m (3 ≤ nm ≤ 820). The next lines describe the labyrinth. Each of the next m lines contains ncharacters. Each character is either "#"
or ".", with "#" indicating an occupied cell, and "." indicating a free cell.

Output

Print out in the single line the length (measured in the number of cells) of the required rope.

Sample Input

input output
7 6
#######
#.#.###
#.#.###
#.#.#.#
#.....#
#######
8

解题思路:两遍BFS求树的直径

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>
#define Inf 0x7fffffff
using namespace std;
char maze[850][850];
int mov[4][2]={0,1,0,-1,1,0,-1,0};
struct node
{
	int x,y;
	int step;
};
bool vis[850][850];
node bfs(int a,int b)
{
	int i,x,y;
    memset(vis,false,sizeof(vis));
    node ans;
    ans.x=a,ans.y=b,ans.step=0;
	node ini;
	ini.x=a,ini.y=b,ini.step=0;
	queue<node> q;
	q.push(ini);
	vis[a][b]=true;
	while(!q.empty())
	{
		ini=q.front();
		q.pop();
		if(ans.step<ini.step)
			ans=ini;
		for(int i=0;i<4;i++)
		{
			int x=ini.x+mov[i][0];
			int y=ini.y+mov[i][1];
			int s=ini.step+1;
			node tmp;tmp.x=x;tmp.y=y;tmp.step=s;
			if(!vis[x][y]&&maze[x][y]!='#')
			{
				vis[x][y]=true;
				q.push(tmp);
			}
		}
	}
	return ans;
}
int main()
{
	int m,n,a,b;
	//freopen("in.txt","r",stdin);
	//freopen("out.txt","w",stdout);
	while(~scanf("%d %d\n",&m,&n))
	{
		memset(maze,'#',sizeof(maze));
		for(int i=1;i<=n;i++)
		{
			for(int j=1;j<=m;j++)
			{
				scanf("%c",&maze[i][j]);
				if(maze[i][j]=='.')
					a=i,b=j;
			}
			getchar();
		}
		node p1=bfs(a,b);
		node p2=bfs(p1.x,p1.y);
		//cout<<p2.x<<" "<<p2.y<<endl;
		printf("%d\n",p2.step);
	}
	return 0;
}

SYSU暑假热身赛D

时间: 2024-08-10 18:28:49

SYSU暑假热身赛D的相关文章

暑假反思

成功的,又一门功课成功地成为了全班倒数,还是最能体现一个人编程水平的c++实训.又得好好反思一下自己的学习状态了. 刚来到华工时的那种不甘,让我近乎维持着高中的学习状态,课内成绩荣登榜首,ACM一直保持着努力状态,这时的我的代码能力可以说是超过了除了几个原本有底子的大多数人.自大一上打下良好的基础之后,一直维持“学霸”状态的我开始反思:我想成为什么样的人?我所热爱的到底是什么?并且开始打算大一下甚至花一年的时间来明确这个方向. 直到现在我都觉得那个时候的决策是正确的. 但是,从现在来看,我的寻找

一些项目——今年暑假不AC

Problem Description "今年暑假不AC?" "是的." "那你干什么呢?" "看世界杯呀,笨蛋!" "@#$%^&*%..." 确实如此,世界杯来了,球迷的节日也来了,估计很多ACMer也会抛开电脑,奔向电视了. 作为球迷,一定想看尽量多的完整的比赛,当然,作为新时代的好青年,你一定还会看一些其它的节目,比如新闻联播(永远不要忘记关心国家大事).非常6+7.超级女生,以及王小丫的&

写在开学后:暑假没有什么卵用的总结以及NOIP复赛前的计划

emmmm暑假干了点什么呢? 7月在机房培训,听的基本是各种没有学过的东西...所以基本都是懂了个大概,没有具体的代码实现...23号之后休息,除了出去玩之外也没有干啥... 8月去雅礼集训.emmm讲的基本都是以前听过的东西和已经学过的东西...所以现在还是懂了个大概...至于收获嘛,就是有了十几天的考试经验(虽然总结被我忘到脑袋后面了) 现在开学了,在新知识的学习上可以说我这两个月基本什么都没有学? 所以说更是要赶进度了= = 11月就是NOIP了,大概是没有什么压力的?不过今年的NOIP,

暑假学习总结

暑假中完成了老师布置得大任务,虽然说小组成员的分工不同,但是每一个人都非常努力.我负责的是程序测试.主要是发现完成的程序中出现的各种问题.发现问题后通知该项目的负责人然后一起修改. 在此过程中学到了很多,首先,最重要的是收获了友情,懂得了团队合作的重要性.最重要的是明白了在开发一个项目中应该注意到的问题,细心必不可少,细心细心再细心是减少程序出现问题的关键.但是再细心也会有错误发生,这就需要良好的编程基础,所以需要在平时不断地练习编程,提高自己的编程能力.更需要小组成员的通力合作.!!!!!!

今年暑假不AC hdu2037

题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=2037 今年暑假不AC Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 47163    Accepted Submission(s): 25332 Problem Description “今年暑假不AC?”“是的.”“那你干什么呢?”“看世

暑假计划(7月23日-8月21日)

暑假计划 6:45     起床 6:45-7:30         洗漱.吃早饭.到驾校 7:30-12:00       几乎一直在练车,排队时间记单词,记单词记累了看知乎! 12:00-13:00     吃完饭了,学习数学建模习题对应的论文 13:00-13:25     睡午觉 13:25-14:00     数学建模论文学习+matlab学习 14:00-15:20     休息(采用远眺.听音乐的方式) 15:20-16:00     数学建模论文学习+matlab学习 16:00

暑假要看的东西

暑假要看的东西 大魔王推荐 大魔王主学 c++,c++ 作为一门包罗万象的语言,学习难度自然是大了点.下面是我要安利的东西,都是我用过的~ 读物 以下是推荐读物. <C++ Primer>这边是在学习 c++ 后可以读一读的书,适合入门后再进阶. <C++ Primer Plus>这本反而是入门级的读物,适合 c++ 新手. <Effective C++>,这本书介绍了 c++ 中的特性与应用场景,编写出高效的代码. <Qt on Android 核心编程>

2014年暑假学习的日子

2014年的暑假接近尾声,我开始试着去回顾这个暑假的收获.虽然时间比较短只有40天,但收获还是蛮多的,在这个拿出来和大家晒晒. 思想上: 1.见到一种新事物,知道如何和以前的知识建立联系,从心态上不再有畏惧感: 2.开始试着看API文档(英文版),再次验证了学英语的重要性: 3.有了自己的时间管理,虽然每天都有组长催项目进度,但还是能坚持每天看Android视频: 4.严格控制项目期限,必须在规定时间内完成规定的事,比如我们的机房收费搞了将近半年: 5.开始试着结对学习,比如我.宏杰.一清作为一

Hdu 2037 今年暑假不AC

今年暑假不AC Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 27586    Accepted Submission(s): 14578 Problem Description “今年暑假不AC?”“是的.”“那你干什么呢?”“看世界杯呀,笨蛋!”“@#$%^&*%...” 确实如此,世界杯来了,球迷的节日也来了,估计很多ACMer也