poj1383链接
The northern part of the Pyramid contains a very large and complicated labyrinth. The labyrinth is divided into square blocks, each of them either filled by rock, or free. There is also a little hook on the floor in the center of every free block. The ACM have found that two of the hooks must be connected by a rope that runs through the hooks in every block on the path between the connected ones. When the rope is fastened, a secret door opens. The problem is that we do not know which hooks to connect. That means also that the neccessary length of the rope is unknown. Your task is to determine the maximum length of the rope we could need for a given labyrinth.
The input consists of T test cases. The number of them (T) is given on the first line of the input file. Each test case begins with a line containing two integers C and R (3 <= C,R <= 1000) indicating the number of columns and rows. Then exactly R lines follow, each containing C characters. These characters specify the labyrinth. Each of them is either a hash mark (#) or a period (.). Hash marks represent rocks, periods are free blocks. It is possible to walk between neighbouring blocks only, where neighbouring blocks are blocks sharing a common side. We cannot walk diagonally and we cannot step out of the
The labyrinth is designed in such a way that there is exactly one path between any two free blocks. Consequently, if we find the proper hooks to connect, it is easy to find the right path connecting them.
Your program must print exactly one line of output for each test case. The line must contain the sentence “Maximum rope length is X.” where Xis the length of the longest path between any two free blocks, measured in blocks.
Sample Input
2
3 3
#.#
7 6
#######
#.#.###
#.#.###
#.#.#.#
#…# //此处少一个,请看原题
#######
Maximum rope length is 0.
Maximum rope length is 8.
Huge input, scanf is recommended.
If you use recursion, maybe stack overflow. and now C++/c 's stack size is larger than G++/gcc
Central Europe 1999
应该是比较经典的求最长路径
我其实也有点搞不懂bfs和dfs的用法和区别,可能也是刚入门
#include
#include
#include
using namespace std;
int N,M,u,v,ans; //M是列N是行 u,v记录第一次搜索的最后点 ans记录路径长度
int dir[4][2]={1,0,0,1,-1,0,0,-1};
char mad[1200][1200]; //输入字符迷宫时需要
bool vis[1200][1200]; //作为标记数组,标记数组内每个点是否搜索过。因为只与0,1有关,bool相对于int要好一
void dfs(int x,int y,int res){
for(int i=0;i<4;i++){
int nx=dir[i][0]+x;
int ny=dir[i][1]+y; //向四周搜索,为什么1
if(nx>=0&&nx=0&&ny=ans){
u=x;v=y;ans=res; //更新数据,ans会变两次,第一次为第一次搜索的长度,第二次为输出结果
}
}
int main(){
int T;
scanf("%d",&T);
while(T--){
scanf("%d%d",&M,&N);
for(int i=0;i
1. dir[4][2]=
|1 |0 |
|-1|0 |
| 0|-1|
|0 | 1|
所以会有for(int i=0;i<4;i++){
int nx=dir[i][0]+x;
int ny=dir[i][1]+y;
2.关于二维(字符)数组的输入问题
3.dfs
#include
#include
#include
#include
#include
1.为何程序员喜欢将INF设置为0x3f3f3f3f?
2.结构体(声明、初始化、内存对齐、如何传参)
3.bfs
有很多借鉴的,也有原创的,归于原创吧,不然不知咋办
就到这吧.嘿嘿?
原码 ,这样简洁
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
int N,M,u,v,ans;
int dir[4][2]={1,0,0,1,-1,0,0,-1};
char mad[1200][1200];
bool vis[1200][1200];
void dfs(int x,int y,int res){
for(int i=0;i<4;i++){
int nx=dir[i][0]+x;
int ny=dir[i][1]+y;
if(nx>=0&&nx=0&&ny=ans){
u=x;v=y;ans=res;
}
}
int main(){
int T;
scanf("%d",&T);
while(T--){
scanf("%d%d",&M,&N);
for(int i=0;i
怎末说呢,两个差不多,差的就是些写法,bfs中看着长,其实有一些多余了,可以再简洁些,但是也可以多了解些写法。