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
跟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; }
相关文章推荐
- NYOJ 284 坦克大战(BFS)
- nyoj--284--坦克大战(bfs模板)
- nyoj 284 坦克大战
- nyoj284-坦克大战(搜索 bfs)
- NYOJ - 284 - 坦克大战(BFS求最短路,优先队列)
- nyoj--284--坦克大战(bfs模板)
- nyoj 284 坦克大战
- 南阳(NYOJ)284 坦克大战
- nyoj284 坦克大战(dijkstra(bfs+优先队列))
- 坦克大战(nyoj284)
- NYOJ-284-坦克大战(BFS)
- nyoj 284 坦克大战(广搜+优先队列)
- nyoj 284 坦克大战
- NYOJ 284 坦克大战
- nyoj-284-坦克大战
- nyoj--284 坦克大战(bfs+优先队列)
- NYOJ 284 坦克大战
- nyoj 284 坦克大战
- nyoj 284 坦克大战
- nyoj 284 坦克大战(BFS)