TLE代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<ctype.h>
#include<algorithm>
#include<queue>
#include<vector>
using namespace std;
int Next[4][2] = { {0,1}, {0,-1}, {1,0}, {-1,0} };
struct Pos{
int x, y;
Pos(int xx, int yy):x(xx), y(yy) { }
};
struct Node{
int x, y;
int t;
Node(int xx, int yy, int tt):x(xx), y(yy), t(tt) { }
};
const int maxn = 200+10;
int n, m;
char board[maxn][maxn];
bool vis[maxn][maxn];
int main() {
while( scanf("%d%d", &n, &m)!=-1 ) {
int ans = 0x3f3f3f;
int cnt=0;
int xa, ya, xb, yb;
vector<Pos> KFC;
for(int i=0; i<n; i++) {
for(int j=0; j<m; j++) {
cin >> board[i][j];
if( board[i][j] == '@' ) {
KFC.push_back( Pos(i,j) );
++cnt;
}
if( board[i][j] == 'Y' ) {
xa = i; ya = j;
}
if( board[i][j] == 'M' ) {
xb = i; yb = j;
}
}
}
for(int i=0; i<cnt; i++) {
int counter = 0;
int sum = 0;
memset( vis, 0, sizeof(vis) );
queue<Node>Q;
Q.push( Node(KFC[i].x, KFC[i].y, 0) );
vis[ KFC[i].x ][ KFC[i].y ] = 1;
bool isfindA = 0, isfindB = 0;
while( !Q.empty() ) {
Node q = Q.front();
if( q.x==xa && q.y==ya && !isfindA ) {
isfindA = 1;
sum+=q.t;
Q.pop();
continue;
}
if( q.x==xb && q.y==yb ) {
isfindB = 1;
sum+=q.t;
Q.pop();
continue;
}
if( isfindA && isfindB ) { break; }
for(int i=0; i<4; i++) {
int tx = q.x+Next[i][0];
int ty = q.y+Next[i][1];
if( tx<0 || tx>n || ty<0 || ty>m ) { continue; }
if( vis[tx][ty] ) { continue; }
if( board[tx][ty]=='#' ) { continue; }
Q.push( Node(tx,ty,q.t+1) );
vis[tx][ty] = 1;
}
Q.pop();
}
if( isfindA && isfindB ) {
ans = min(ans, sum);
}
}
ans *= 11;
cout << ans << endl;
}
return 0;
}
AC代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<ctype.h>
#include<algorithm>
#include<queue>
#include<vector>
using namespace std;
struct Pos {
int x, y;
Pos(int xx, int yy):x(xx), y(yy) { }
};
struct Node {
int x, y;
int t;
Node(int xx, int yy, int tt):x(xx), y(yy), t(tt) { }
};
const int maxn = 200+10;
int Next[4][2] = { {0,1}, {0,-1}, {1,0}, {-1,0} };
char board[maxn][maxn];
int sum[maxn][maxn];
int isfind[maxn][maxn];
bool vis[maxn][maxn];
int n, m;
void DFS(int x, int y) {
memset( vis, 0, sizeof(vis) );
queue<Node> Q;
Q.push( Node(x,y,0) );
vis[x][y] = 1;
while( !Q.empty() ) {
Node q = Q.front();
if( board[q.x][q.y] == '@' ) {
isfind[q.x][q.y]++;
sum[q.x][q.y] += q.t;
}
for(int i=0; i<4; i++) {
int tx = q.x+Next[i][0];
int ty = q.y+Next[i][1];
if( tx<0 || tx>n || ty<0 || ty>m ) { continue; }
if( vis[tx][ty] ) { continue; }
if( board[tx][ty] == '#' ) { continue; }
if( board[tx][ty] == 'Y' || board[tx][ty] == 'M' ) { continue; }
Q.push( Node(tx,ty,q.t+1) );
vis[tx][ty] = 1;
}
Q.pop();
}
}
int main() {
while( scanf("%d %d", &n, &m)!=EOF ) {
int cnt = 0;
int xa, ya, xb, yb;
vector<Pos> KFC;
for(int i=0; i<n; i++) {
for(int j=0; j<m; j++) {
cin >> board[i][j];
if( board[i][j] == '@' ) {
KFC.push_back( Pos(i,j) );
++cnt;
}
if( board[i][j] == 'Y' ) {
xa = i; ya = j;
}
if( board[i][j] == 'M' ) {
xb = i; yb = j;
}
}
}
memset(sum, 0, sizeof(sum));
memset(isfind, 0, sizeof(isfind));
DFS(xa, ya);
DFS(xb, yb);
int ans = 0x3f3f3f;
for(int i=0; i<cnt; i++) {
if( isfind[KFC[i].x][KFC[i].y] == 2 ) {
ans = min( ans, sum[KFC[i].x][KFC[i].y] );
}
}
ans *= 11;
cout << ans << endl;
}
return 0;
}