Kolstad and Schrijvers
Farmer John went crazy and created a huge maze of fences out in a field. Happily, he left out two fence segments on the edges, and thus created two "exits" for the maze. Even more happily, the maze he created by this overfencing experience is a `perfect' maze: you can find a way out of the maze from any point inside it.
Given W (1 <= W <= 38), the width of the maze; H (1 <= H <= 100), the height of the maze; 2*H+1 lines with width 2*W+1 characters that represent the maze in a format like that shown later - then calculate the number of steps required to exit the maze from the `worst' point in the maze (the point that is `farther' from either exit even when walking optimally to the closest exit). Of course, cows walk only parallel or perpendicular to the x-y axes; they do not walk on a diagonal. Each move to a new square counts as a single unit of distance (including the move "out" of the maze.
Here's what one particular W=5, H=3 maze looks like:
+-+-+-+-+-+ | | +-+ +-+ + + | | | | + +-+-+ + + | | | +-+ +-+-+-+
Fenceposts appear only in odd numbered rows and and odd numbered columns (as in the example). The format should be obvious and self explanatory. Each maze has exactly two blank walls on the outside for exiting.
PROGRAM NAME: maze1
INPUT FORMAT
Line 1: | W and H, space separated |
Lines 2 through 2*H+2: | 2*W+1 characters that represent the maze |
SAMPLE INPUT (file maze1.in)
5 3 +-+-+-+-+-+ | | +-+ +-+ + + | | | | + +-+-+ + + | | | +-+ +-+-+-+
OUTPUT FORMAT
A single integer on a single output line. The integer specifies the minimal number of steps that guarantee a cow can exit the maze from any possible point inside the maze.SAMPLE OUTPUT (file maze1.out)
9The lower left-hand corner is *nine* steps from the closest exit.
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define INIFINITY 4000
char Maze[203][80];
int W,H;
int Queue[3800][2],front,rear;
int MaxDistance=0,temp;
int tx,ty;
int Exit[2][2],Ec=0; //记录出口坐标
int Dist1[102][40];
int Dist2[102][40];
int Min(int a,int b)
{
return a>b?b:a;
}
void Enqueue()
{
Queue[rear][0]=tx;
Queue[rear][1]=ty;
rear++;
}
void Dequeue()
{
tx=Queue[front][0];
ty=Queue[front][1];
front++;
}
int Search(int x,int y,int (*Dist)[40])
{
tx=x,ty=y;
Dist[tx][ty]=1;
front=0;
rear=0;
Enqueue();
while(front!=rear){
Dequeue();
if(Maze[2*tx+1][2*ty]!='-'&&Dist[tx+1][ty]==INIFINITY){
Dist[tx+1][ty]=Dist[tx][ty]+1;
tx++;
Enqueue();
tx--;
}
if(Maze[2*tx-1][2*ty]!='-'&&Dist[tx-1][ty]==INIFINITY){
Dist[tx-1][ty]=Dist[tx][ty]+1;
tx--;
Enqueue();
tx++;
}
if(Maze[2*tx][2*ty+1]!='|'&&Dist[tx][ty+1]==INIFINITY){
Dist[tx][ty+1]=Dist[tx][ty]+1;
ty++;
Enqueue();
ty--;
}
if(Maze[2*tx][2*ty-1]!='|'&&Dist[tx][ty-1]==INIFINITY){
Dist[tx][ty-1]=Dist[tx][ty]+1;
ty--;
Enqueue();
ty++;
}
}
}
void InitialDist(int (*Dist)[40])
{
int i,j;
for(i=1;i<=H;i++)
for(j=1;j<=W;j++)
Dist[i][j]=INIFINITY;
}
int main()
{
FILE *fin,*fout;
int Dist[2][101][39];
int i,j;
fin=fopen("maze1.in","r");
fout=fopen("maze1.out","w");
fscanf(fin,"%d %d",&W,&H);
fscanf(fin,"\n");
for(i=1;i<=2*H+1;i++){
for(j=1;j<=2*W+2;j++){
Maze[i][j]=fgetc(fin);
if((j==1||j==2*W+1||i==1||i==2*H+1)&&Maze[i][j]==' '){
if(i==1){
Exit[Ec][0]=i;
Exit[Ec][1]=j/2;
Ec++;
}
if(i==2*H+1){
Exit[Ec][0]=(i-1)/2;
Exit[Ec][1]=j/2;
Ec++;
}
if(j==1){
Exit[Ec][0]=i/2;
Exit[Ec][1]=j;
Ec++;
}
if(j==2*W+1){
Exit[Ec][0]=i/2;
Exit[Ec][1]=(j-1)/2;
Ec++;
}
}
}
}
InitialDist(Dist1);
InitialDist(Dist2);
//读取完毕
Search(Exit[0][0],Exit[0][1],Dist1);
Search(Exit[1][0],Exit[1][1],Dist2);
for(i=1;i<=H;i++){
for(j=1;j<=W;j++){
if(MaxDistance<Min(Dist1[i][j],Dist2[i][j]))
MaxDistance=Min(Dist1[i][j],Dist2[i][j]);
}
}
fprintf(fout,"%d\n",MaxDistance);
return 0;
}
这是做的第一道最短路的题目啊。。。好纠结