# |BZOJ 1656|BFS|[Usaco2006 Jan] The Grove 树木

bzoj 1656
luogu 2864
from: USACO 2006 Jan Sliver(USACO刷题第1题)

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#define ms(i, j) memset(i, j, sizeof i)
using namespace std;

const int MAXN = 50 + 5;
const int dx[8] = {1,0,-1,0,1,-1,1,-1};
const int dy[8] = {0,1,0,-1,1,-1,-1,1};

struct node{int step, x, y;};
int r, c, s, t, woodx, woody, vi[MAXN][MAXN];
char m[MAXN][MAXN];

void bfs() {
queue<node> q;
vi[s][t] = 0; q.push((node){0,s,t});
while (!q.empty()) {
node p = q.front(); q.pop();
for (int i=0;i<8;i++) {
int tx = p.x + dx[i], ty = p.y + dy[i];
if (tx>0&&ty>0&&tx<=r&&ty<=c) {
if (m[tx][ty]=='.'&&vi[tx][ty]==-1) {
vi[tx][ty] = p.step + 1;
q.push((node){p.step + 1,tx,ty});
}
}
}
}
}
void clear() {
for (int i=1;i<=r;i++)
for (int j=1;j<=c;j++) vi[i][j] = -1;
woodx = woody = 0;
}
void init() {
clear();
for (int i=1;i<=r;i++) {
for (int j=1;j<=c;j++) {
char ch;
while (ch!='.'&&ch!='X'&&ch!='*') ch = getchar();
m[i][j] = ch;
if (ch=='*') s = i, t = j;
if (!woodx&&ch=='X') woodx = i, woody = j;
ch = getchar();
}
}
for (int j=1;j<woody;j++) m[woodx][j] = '^';
}
void solve() {
bfs();
int ans = 1000000000;
for (int j=1;j<woody;j++) {
if (j-1>0&&vi[woodx-1][j-1]!=-1&&vi[woodx+1][j+1]!=-1) ans = min(ans, vi[woodx-1][j-1]+vi[woodx+1][j+1]);//1 6
if (vi[woodx-1][j]!=-1  &&vi[woodx+1][j]!=-1)   ans = min(ans, vi[woodx-1][j]+  vi[woodx+1][j]);//2 5
if (j-1>0&&vi[woodx-1][j+1]!=-1&&vi[woodx+1][j-1]!=-1) ans = min(ans, vi[woodx-1][j+1]+vi[woodx+1][j-1]);//3 4
if (vi[woodx-1][j]!=-1  &&vi[woodx+1][j+1]!=-1) ans = min(ans, vi[woodx-1][j]+  vi[woodx+1][j+1]);//2 6
if (vi[woodx-1][j+1]!=-1&&vi[woodx+1][j]!=-1)   ans = min(ans, vi[woodx-1][j+1]+vi[woodx+1][j]);//3 5
}
printf("%d\n", ans+2);
}
int main() {
#ifndef ONLINE_JUDGE
freopen("1.in", "r", stdin);freopen("1.out", "w", stdout);
#endif
while (scanf("%d%d", &r, &c)==2&&r&&c) init(), solve();
return 0;
}
• 0
点赞
• 0
评论
• 0
收藏
• 一键三连
• 扫一扫，分享海报

07-04 390
10-31 252
08-23 2827
09-26 26
11-25 6476

NotFound1

¥2 ¥4 ¥6 ¥10 ¥20