本题练习广搜:
题目链接:http://acm.hit.edu.cn/hoj/problem/view?id=2979
本题十分简单,典型的FloodFIll。输出格式细节地方需要注意即可。
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <queue>
using namespace std;
int map[202][202];
int dis[202][202];
struct Point
{
int x;
int y;
};
char c;
int disx[6] = {-1,0,1,1,0,-1};
int disy[6] = {1,2,1,-1,-2,-1};
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
int h,t;
int temp;
int curx,cury;
int num = 0;
int flag = 0;
int ans;
while(scanf("%d %d",&h,&t) == 2)
{
num++;
if(num!=1)
{
printf("\n");
}
flag = 0;
memset(map,0,sizeof(map));
memset(dis,0,sizeof(dis));
for(int i=0;i<h;i++)
{
for(int j=0;j<i+1;j++)
{
scanf(" %c",&c);
if(c == 'S')
{
temp = 1;
curx = i;
cury = h-i-1+j*2;
}
else if(c == '@')
{
temp = 2;
}
else if(c == '*')
{
temp = 3;
}
else if(c == 'D')
{
temp = 4;
}
map[i][h-i-1+j*2] = temp;
}
}
queue<Point> p;
Point nex,temp;
nex.x = curx;
nex.y = cury;
p.push(nex);
while(!p.empty())
{
temp = p.front();
p.pop();
for(int i=0;i<6;i++)
{
int tempx = temp.x + disx[i];
int tempy = temp.y + disy[i];
if(tempx>=0 && tempx<h && tempy>=0 && tempy<2*h)
{
if(map[tempx][tempy] == 2)
{
dis[tempx][tempy] = dis[temp.x][temp.y] + 1;
nex.x = tempx;
nex.y = tempy;
p.push(nex);
map[tempx][tempy] = 5;//标志为已访问过
}
else if(map[tempx][tempy] == 4)
{
flag = 1;
ans = dis[temp.x][temp.y] + 1;
printf("Maze #%d :\n",num);
if(ans<=t)
{
if(ans == 1)
{
printf("Hurry up, You need %d minute to escape!\n",ans);
}
else
{
printf("Hurry up, You need %d minutes to escape!\n",ans);
}
}
else
{
printf("Oh No, I'm afraid that you don't have enough time to escape!\n");
}
break;
}
}
}
if(flag == 1)
{
break;
}
}
if(flag == 0)
{
printf("Maze #%d :\n",num);
printf("Oh No, I'm afraid that you don't have enough time to escape!\n");
}
}
return 0;
}