【题目链接】:http://hihocoder.com/problemset/problem/1519?sid=1098756
【题意】
Chinese
【题解】
bfs题;
根据bfs的性质;
第一次到达的点肯定是转弯次数最少的;
每次往一个方向走到头就好了;
搞个数组判判重.
这里在往一个方向走的时候;
如果途中遇到了终点;
也算能到达终点;
其他的就没什么坑点了;
【Number Of WA】
3
【完整代码】
#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ms(x,y) memset(x,y,sizeof x)
typedef pair<int,int> pii;
typedef pair<LL,LL> pll;
const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
const int N = 510;
struct node
{
int x,y,fx;
};
int f[N][N][5],n,m,sx,sy,tx,ty;
bool bo[N][N];
char s[N];
queue <node> dl;
int bfs(int x,int y)
{
rep1(i,1,500)
rep1(j,1,500)
rep1(k,1,4)
f[i][j][k] = -2;
rep1(i,1,4)
{
f[x][y][i] = -1;
int tq = x,tw = y;
if (bo[tq+dx[i]][tw+dy[i]])
{
while (bo[tq+dx[i]][tw+dy[i]])
{
tq+=dx[i],tw+=dy[i];
if (tq==tx && tw==ty) return 0;
}
f[tq][tw][i] = 0;
dl.push(node{tq,tw,i});
}
}
while (!dl.empty())
{
node temp = dl.front();
int q = temp.x,w = temp.y,pre = temp.fx;
dl.pop();
rep1(i,1,4)
{
int tq = q,tw = w;
if (bo[tq+dx[i]][tw+dy[i]])
{
while (bo[tq+dx[i]][tw+dy[i]])
{
tq+=dx[i],tw+=dy[i];
if (tq==tx && tw==ty) return f[q][w][pre]+1;
}
if (f[tq][tw][i]==-2)
{
f[tq][tw][i] = f[q][w][pre]+1;
dl.push(node{tq,tw,i});
}
}
}
}
return -1;
}
int main()
{
//freopen("F:\\rush.txt","r",stdin);
ios::sync_with_stdio(false),cin.tie(0);//scanf,puts,printf not use
//init??????
cin >> n >> m;
rep1(i,1,n)
{
cin >>(s+1);
rep1(j,1,m)
{
if (s[j]=='.')
bo[i][j]=true;
if (s[j]=='#')
bo[i][j]=false;
if (s[j]=='S')
{
bo[i][j] = true;
sx = i,sy = j;
}
if (s[j]=='T')
{
bo[i][j] = true;
tx = i,ty = j;
}
}
}
cout << bfs(sx,sy)<<endl;
return 0;
}