1656: [Usaco2006 Jan] The Grove 树木
Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 246 Solved: 158
[ Submit][ Status][ Discuss]
Description
The pasture contains a small, contiguous grove of trees that has no 'holes' in the middle of the it. Bessie wonders: how far is it to walk around that grove and get back to my starting position? She's just sure there is a way to do it by going from her start location to successive locations by walking horizontally, vertically, or diagonally and counting each move as a single step. Just looking at it, she doesn't think you could pass 'through' the grove on a tricky diagonal. Your job is to calculate the minimum number of steps she must take. Happily, Bessie lives on a simple world where the pasture is represented by a grid with R rows and C columns (1 <= R <= 50, 1 <= C <= 50). Here's a typical example where '.' is pasture (which Bessie may traverse), 'X' is the grove of trees, '*' represents Bessie's start and end position, and '+' marks one shortest path she can walk to circumnavigate the grove (i.e., the answer): ...+... ..+X+.. .+XXX+. ..+XXX+ ..+X..+ ...+++* The path shown is not the only possible shortest path; Bessie might have taken a diagonal step from her start position and achieved a similar length solution. Bessie is happy that she's starting 'outside' the grove instead of in a sort of 'harbor' that could complicate finding the best path.
Input
* Line 1: Two space-separated integers: R and C
* Lines 2..R+1: Line i+1 describes row i with C characters (with no spaces between them).
Output
* Line 1: The single line contains a single integer which is the smallest number of steps required to circumnavigate the grove.
Sample Input
Sample Output
题目保证所有的树木一定在一个联通块中,并且起始点一定不会被树包围
(也就是说至少有两个方向上没有树木)
那么步骤:
①输入时的第一个'X',如果它上面没有'*',那么将这个'X'上面所有的'.'全部置为'T',否则就找下一个'X'
②从'*'处开始BFS,将'T'和'X'全部视为墙
③暴力所有的'T',答案就是每个'T'两测点最短路和的最小值+2(注意斜着的也算)
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<queue>
using namespace std;
typedef struct
{
int x;
int y;
}Point;
Point now, temp;
queue<Point> q;
char str[55][55];
int bet[55][55], dir[8][2] = {1,0,0,1,-1,0,0,-1,1,1,1,-1,-1,-1,-1,1};
int main(void)
{
int n, m, i, j, x, y, k, fx, fy, l, r, ans;
scanf("%d%d", &n, &m);
memset(str, 'X', sizeof(str));
x = y = fx = fy = -1;
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
scanf(" %c", &str[i][j]);
if(str[i][j]=='X' && fx==-1 && (y!=j || x==-1 || x>=i))
{
for(k=1;k<=i-1;k++)
str[k][j] = 'T';
fx = i, fy = j;
}
if(str[i][j]=='*')
x = i, y = j;
}
}
now.x = x, now.y = y;
memset(bet, 62, sizeof(bet));
q.push(now);
bet[now.x][now.y] = 0;
while(q.empty()==0)
{
now = q.front();
q.pop();
for(k=0;k<=7;k++)
{
temp.x = now.x+dir[k][0];
temp.y = now.y+dir[k][1];
if(str[temp.x][temp.y]!='X' && str[temp.x][temp.y]!='T' && bet[now.x][now.y]+1<bet[temp.x][temp.y])
{
bet[temp.x][temp.y] = bet[now.x][now.y]+1;
q.push(temp);
}
}
}
ans = 100000;
for(i=1;i<=fx-1;i++)
{
j = fy;
l = min(bet[i][j-1], min(bet[i-1][j-1], bet[i+1][j-1]));
r = min(bet[i][j+1], min(bet[i-1][j+1], bet[i+1][j+1]));
ans = min(ans, l+r+2);
}
printf("%d\n", ans);
return 0;
}