#include <iostream>
#include <string>
#include <queue>
using namespace std;
int n, m;
string maze[110];
bool vis[110][110];
int dir[4][2] = { {-1, 0}, {0, -1}, {1, 0}, {0, 1} };
bool in(int x, int y)
{
return 0 <= x && x < n && 0 <= y && y < m;
}
struct node
{
int x,y,d;
node (int xx, int yy,int dd)
{
x = xx;
y = yy;
d = dd;
}
};
int bfs(int sx, int sy)
{
queue<node> q;
q.push(node(sx,sy,0));
vis[sx][sy] = true;
while (!q.empty())
{
node now = q.front();
q.pop();
for(int i = 0; i < 4; i++)
{
int tx = now.x + dir[i][0];
int ty = now.y + dir[i][1];
if (in(tx,ty) && maze[tx][ty] != '*' && !vis[tx][ty])
{
if (maze[tx][ty] == 'T')
{
return now.d + 1;
}
else
{
vis [tx][ty] = true;
q.push(node(tx,ty,now.d + 1));
}
}
}
}
return -1;
}
int main()
{
cin >> n >> m;
for (int i = 0; i < n; i++)
{
cin >> maze[i];
}
int x, y;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
if (maze[i][j] == 'S')
{
x = i, y = j;
}
}
}
cout << bfs(x,y) << endl;
return 0;
}

最新文章

  1. JS正则表达式元字符
  2. AOP学习心得&amp;jdk动态代理与cglib比较
  3. Geometry Curve of OpenCascade BRep
  4. 使Eclipse符合Java编程规范
  5. Web Api 2 接口API文档美化
  6. 转载一篇文章 python程序员经常犯的10个错误
  7. Android 用户界面---拖放(Drag and Drop)(二)
  8. Java的导入与导出Excel
  9. 浅析PAC,教你动手修改你的PAC文件及user-rule文件实现自动代理
  10. jquery正则表达式显示文本框输入范围 只能输入数字、小数、汉字、英文字母的方法
  11. spring之注解详解
  12. 最优化方法:范数和规则化regularization
  13. .NET Core TDD 前传: 编写易于测试的代码 -- 依赖项
  14. 转 - Linux安装python3.6
  15. 20172310 实验四 Android程序设计
  16. Visual Question Answering with Memory-Augmented Networks
  17. Shell Trap信号管理
  18. bug定位
  19. python threading模块2
  20. 软件工程作业 - Week 1

热门文章

  1. python获取上周的起始日期
  2. js提取字符串开头公共部分
  3. 题解[CF1628F]A_Random_Code_Problem
  4. java学习一:java介绍及第一个helloword程序
  5. 4、kubesphere环境安装
  6. memoのPython和3D那点事
  7. Delphi模拟win+tab按键效果
  8. idea gradle 安装失败
  9. 2017GPLT
  10. D3简介