题目通道
简单的bfs,两次,注意到了 KFC不要return;
有一些KFC是到不了的,这是代码体现在len1[i][j] == 0 上
题解代码
#include<iostream>
#include<queue>
#include<cstring>
#include<string>
#include<sstream>
#include<map>
#include<vector>
#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstdlib>
#include<stack>
#include<ctime>
using namespace std;
#define rep(i,aa,bb) for(register int i=aa;i<=bb;i++)
#define rrep(i,aa,bb) for(register int i=aa;i>=bb;i--)
#define LL long long
#define eps 0.000001
#define inf 0x3f3f3f3f
#define exp 0.000001
#define pai 3.141592654
#define random(x) rand()%(x)
#define lowbit(x) x&(-x)
inline int read()
{
int x=0,y=1;char a=getchar();while ( a>'9' || a<'0'){if ( a=='-')y=-1;a=getchar();}
while ( a>='0' && a<='9' ){ x=10*x+a-'0'; a=getchar();}return x*y;
}
#define N 209
char e[N][N];int n,m;bool vis[N][N];
const int nxt[][2] = { 1,0,-1,0,0,1,0,-1 };
struct node{
int x,y,t;
};
queue<node>q;
void init(){
memset(e,0,sizeof(e));
memset(vis,0,sizeof(vis));
while ( q.empty() == 0 ) q.pop();
// tot1 = tot2 = 0;
}
bool isin(int x,int y){
if ( x > 0 && x <= n && y > 0 && y <= m ) return 1;
return 0;
}
int len1[N][N];int tot1 = 0;
int len2[N][N];int tot2 = 0;
void bfs(int z){
while ( q.empty() == 0 ){
node a = q.front();
q.pop();
if ( e[a.x][a.y] == '@' ){
if ( z == 1 ){
len1[a.x][a.y] = a.t;
// ++tot1;
// ans1[tot1].x = a.x;
// ans1[tot1].y = a.y;
// ans1[tot1].t = a.t;
}
else {
len2[a.x][a.y] = a.t;
// ++tot2;
// ans2[tot2].x = a.x;
// ans2[tot2].y = a.y;
// ans2[tot2].t = a.t;
}
}
node b;
for (int i = 0; i < 4; i++ ){
b.x = a.x + nxt[i][0];
b.y = a.y + nxt[i][1];
b.t = a.t + 1;
if ( isin(b.x,b.y) ){
if ( !vis[b.x][b.y] && (e[b.x][b.y] == '.' || e[b.x][b.y] == '@') ){
vis[b.x][b.y] = 1;
q.push(b);
}
}
}
}
}
int main()
{
// freopen("1.txt","r",stdin);
srand((int)time(0));
// std::ios::sync_with_stdio(false);
while ( scanf("%d%d",&n,&m) == 2 ){
init();
rep(i,1,n)
scanf("%s",e[i]+1);
rep(i,1,n) rep(j,1,m){
if( e[i][j] == 'Y' ){
node a ;
a.x = i; a.y = j; a.t = 0;
vis[i][j] = 1;
q.push(a);
bfs(1);
}
}
memset(vis,0,sizeof(vis));
tot2 = 0;
rep(i,1,n) rep(j,1,m){
if( e[i][j] == 'M' ){
node a ;
a.x = i; a.y = j; a.t = 0;
vis[i][j] = 1;
q.push(a);
bfs(2);
}
}
int minx = inf;
for (int i = 1; i <= n ; i++)
for (int j = 1;j <= m; j++){
if ( e[i][j] == '@' && minx > len1[i][j] + len2[i][j] && len1[i][j] && len2[i][j])
minx = len1[i][j] + len2[i][j];
}
printf("%d\n",minx*11);
}
return 0;
}