思路:
- 正解:2次BFS。
- 我的思路:
- 第一次我从每个K开始,找每个K的到达两个人的时间,TLE。
- 把 char mp[][] 换成 bool mp[][],TLE。
- 正解:从每个人开始BFS,注意KFC的存储。
代码:
#include <iostream>
#include <cstring>
#include <queue>
#include <algorithm>
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = 205;
const int mt[4][2] = {1,0 , -1,0 , 0,1 , 0,-1};
struct node{
int x,y;
int step;
node(int x,int y,int step) : x(x) , y(y) , step(step) {} ;
};
char mp[maxn][maxn] ;
bool vis[maxn][maxn];
int ans=INF;
queue<node> Q;
int M,N;
int BFS(int m,int n,char tar){
memset(vis,false,sizeof(vis));
while(Q.size())
Q.pop();
int t=0;
Q.push(node(m,n,0));vis[m][n] = true;
while(Q.size()){
node cur = Q.front();Q.pop();
int x= cur.x;
int y= cur.y;
if(mp[x][y] == tar)
return cur.step;
for(int i=0;i<4;i++){
int nx = x+mt[i][0];
int ny = y+mt[i][1];
if(vis[nx][ny] || mp[nx][ny] == '#')
continue;
if(nx < 1 || nx > M || ny < 1 || ny > N)
continue;
Q.push(node(nx,ny,cur.step+1));vis[nx][ny] = true;
}
}
return INF;
}
int main(){
while(cin>>M>>N){
for(int i=1;i<=M;i++)
for(int j=1;j<=N;j++){
cin>>mp[i][j];
}
for(int i=1;i<=M;i++)
for(int j=1;j<=N;j++)
if(mp[i][j] == '@'){
int t1 = BFS(i,j,'Y');
int t2 = BFS(i,j,'M');
ans = min(ans , t1 + t2);
}
ans*=11;
cout<<ans<<endl;
}
return 0;
}
#include <iostream>
#include <cstring>
#include <queue>
#include <algorithm>
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = 205;
const int mt[4][2] = {1,0 , -1,0 , 0,1 , 0,-1};
struct node{
int x,y;
int step;
node(int x,int y,int step) : x(x) , y(y) , step(step) {} ;
};
bool mp [maxn][maxn];
int Yx,Yy,Mx,My;
int Kx[maxn*maxn];
int Ky[maxn*maxn];
int cnt;
bool vis[maxn][maxn];
int ans;
queue<node> Q;
int M,N;
int BFS(int m,int n){
int t1=INF,t2=INF;
memset(vis,false,sizeof(vis));
while(Q.size())
Q.pop();
Q.push(node(m,n,0));vis[m][n] = true;
while(Q.size()){
node cur = Q.front();Q.pop();
int x= cur.x;
int y= cur.y;
if(x == Yx && y == Yy)
t1 = cur.step ;
if(x == Mx && y == My)
t2 = cur.step ;
for(int i=0;i<4;i++){
int nx = x+mt[i][0];
int ny = y+mt[i][1];
if(vis[nx][ny] || !mp[nx][ny])
continue;
if(nx < 1 || nx > M || ny < 1 || ny > N)
continue;
Q.push(node(nx,ny,cur.step+1));vis[nx][ny] = true;
}
}
return t1 + t2;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
while(cin>>M>>N){
ans = INF;
cnt=0;
memset(mp , false , sizeof(mp));
for(int i=1;i<=M;i++)
for(int j=1;j<=N;j++){
char t;
cin>>t;
if(t != '#'){
mp[i][j] = true;
if(t == 'Y')
Yx = i , Yy = j;
if(t == 'M')
Mx = i , My = j;
if(t == '@'){
cnt++;
Kx[cnt] = i , Ky[cnt] = j;
}
}
}
for(int i=1;i<=cnt;i++){
int t = BFS(Kx[i] , Ky[i]) ;
ans = min(ans , t) ;
}
ans*=11;
cout<<ans<<endl;
}
return 0;
}
#include <iostream>
#include <queue>
#include <cstring>
#include <algorithm>
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = 205;
const int mt[4][2] = {1,0 , -1,0 , 0,1 , 0,-1} ;
struct node{
int x,y;
int s;
node(int x,int y,int s) : x(x) , y(y) , s(s) {} ;
};
queue<node> Q;
int M,N;
bool mp [maxn][maxn];
bool K [maxn][maxn];
bool vis[maxn][maxn];
int Kt [2][maxn][maxn];
int Yx , Yy;
int Mx , My;
int ans;
void BFS(int m,int n,int id){
while(Q.size())
Q.pop();
memset(vis,false,sizeof(vis));
Q.push(node(m,n,0));
vis[m][n] = true;
while(Q.size()){
node cur = Q.front() ; Q.pop();
int x = cur.x , y = cur.y ;
if(K[x][y])
Kt[id][x][y] = cur.s;
for(int i=0;i<4;i++){
int nx = x + mt[i][0];
int ny = y + mt[i][1];
if(vis[nx][ny] || !mp[nx][ny])
continue;
if(nx < 1 || ny < 1 || nx > M || ny > N)
continue;
Q.push(node(nx , ny , cur.s + 1));vis[nx][ny] = true;
}
}
return ;
}
int main(){
while(cin>>M>>N){
memset(K , false , sizeof(K));
memset(Kt , INF , sizeof(Kt));
ans = INF;
memset(mp,false,sizeof(mp));
for(int i=1;i<=M;i++){
for(int j=1;j<=N;j++){
char t;
cin>>t;
if(t != '#')
mp[i][j] = true;
if(t == '@')
K [i][j] = true;
if(t == 'Y')
Yx = i , Yy = j;
if(t == 'M')
Mx = i , My = j;
}
}
BFS(Yx , Yy , 0);
BFS(Mx , My , 1);
for(int i=1;i<=M;i++){
for(int j=1;j<=N;j++){
if(K[i][j]){
ans = min(ans , Kt[0][i][j] + Kt[1][i][j]);
}
}
}
ans *= 11;
cout<<ans<<endl;
}
return 0;
}