您的位置:首页 > 其它

Nyoj 284 坦克大战

2014-04-05 14:07 218 查看
题目来源:http://acm.nyist.net/JudgeOnline/problem.php?pid=284

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

参考:/article/10999224.html

#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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: