Nyoj 284 坦克大战

来源:互联网 发布:淘宝男装原创品牌 编辑:程序博客网 时间:2024/04/30 01:19

题目来源:http://acm.nyist.net/JudgeOnline/problem.php?pid=284

跟zoj1649 Rescue 相当的类似!http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1649

参考:http://blog.csdn.net/hearthougan/article/details/12377477

#include <iostream>#include <cstring>#include <cstdio>#include <queue>#include <cmath>using namespace std;const int MAXN  = 310;const int INF = 0xffffff;struct Node{    int x, y;    int step;    Node()    {        x = y = 0;        step = 0;    }};char Graph[MAXN][MAXN];int MinTime[MAXN][MAXN];int dir[4][2] = {{0, -1}, {1, 0}, {0, 1}, {-1, 0}};Node TargetPos;int row, col;bool Is_CanGo(Node CurPos){    if(CurPos.x < 0 || CurPos.x >= row || CurPos.y < 0 || CurPos.y >= col || Graph[CurPos.x][CurPos.y] == 'S' || Graph[CurPos.x][CurPos.y] == 'R')        return false;    return true;}void BFS(Node s){    queue <Node> Que;    Que.push(s);    while(!Que.empty())    {        Node CurPos = Que.front();        Que.pop();        for(int i = 0; i < 4; ++i)        {            Node t;            t.x = CurPos.x + dir[i][0];            t.y = CurPos.y + dir[i][1];            if(Is_CanGo(t))            {                t.step = CurPos.step + 1;                if(Graph[t.x][t.y] == 'B')                    t.step++;                if(MinTime[t.x][t.y] > t.step)                {                    MinTime[t.x][t.y] = t.step;                    Que.push(t);                }            }        }    }    return ;}int main(){    Node StartPos;    while(~scanf("%d %d", &row, &col) && (row + col))    {        for(int i = 0; i < row; ++i)        {            scanf("%s", Graph[i]);            for(int j = 0; j < col; ++j)            {                MinTime[i][j] = INF;                if(Graph[i][j] == 'Y')                    StartPos.x = i, StartPos.y = j;                else if(Graph[i][j] == 'T')                    TargetPos.x = i, TargetPos.y = j;            }        }        MinTime[StartPos.x][StartPos.y] = 0;        StartPos.step = 0;        BFS( StartPos );        int t = MinTime[TargetPos.x][TargetPos.y];        if(t != INF)            printf("%d\n", t);        else            printf("-1\n");    }    return 0;}


0 0
原创粉丝点击