[HDU-2612] Find a Way

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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值