#define DeBUG
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <string>
#include <set>
#include <sstream>
#include <map>
#include <bitset>
using namespace std ;
#define zero {0}
#define INF 2000000000
#define EPS 1e-6
typedef long long LL;
const double PI = acos(-1.0);
inline int sgn(double x)
{
return fabs(x) < EPS ? 0 : (x < 0 ? -1 : 1);
}
struct Node
{
int x, y;
int step;
int t;
};
int dir[4][2] = {0, 1, 0, -1, 1, 0, -1, 0};
int main()
{
#ifdef DeBUGs
freopen("C:\\Users\\Sky\\Desktop\\1.in", "r", stdin);
// freopen("C:\\Users\\Sky\\Desktop\\dabiao.txt","w",stdout);
#endif
int n, m;
while (scanf("%d%d", &n, &m) + 1)
{
char mp[30][30];
int vis[30][30] = zero;
for (int i = 0; i < n; i++)
{
scanf("%s", mp[i]);
}
Node node;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
if (mp[i][j] == 'S')
{
node.x = i;
node.y = j;
node.step = 0;
node.t = 0;
break;
}
}
}
queue<Node>Q;
Q.push(node);
int T = 0;
while (!Q.empty())
{
node = Q.front();
Q.pop();
int x, y;
// printf("*****************%d\n", T);//可以用来调试哦
// for (int d = 0; d < n; d++)
// {
// for (int k = 0; k < m; k++)
// {
// if (d == node.x && k == node.y)
// printf("@");
// else
// printf("%c", mp[d][k]);
// }
// printf("\n");
// }
if (mp[node.x][node.y] == 'T')
break;
if (node.t != T)
{
T++;
}
for (int i = 0; i < 4; i++)
{
x = node.x + dir[i][0];
y = node.y + dir[i][1];
if (T & 1) //变
{
if (i == 0 || i == 1)
{
if (mp[x][y] == '|')
{
x = x + dir[i][0];
y = y + dir[i][1];
}
if (mp[x][y] == '-')
{
Node addnode;
addnode.x = node.x;
addnode.y = node.y;
addnode.step = node.step;
addnode.t = T + 1;
Q.push(addnode);
continue;
}
}
else
{
if (mp[x][y] == '|')
{
Node addnode;
addnode.x = node.x;
addnode.y = node.y;
addnode.step = node.step;
addnode.t = T + 1;
Q.push(addnode);
continue;
}
if (mp[x][y] == '-')
{
x = x + dir[i][0];
y = y + dir[i][1];
}
}
}
else
{
if (i == 2 || i == 3)
{
if (mp[x][y] == '|')
{
x = x + dir[i][0];
y = y + dir[i][1];
}
if (mp[x][y] == '-')
{
Node addnode;
addnode.x = node.x;
addnode.y = node.y;
addnode.step = node.step;
addnode.t = T + 1;
Q.push(addnode);
continue;
}
}
else
{
if (mp[x][y] == '|')
{
Node addnode;
addnode.x = node.x;
addnode.y = node.y;
addnode.step = node.step;
addnode.t = T + 1;
Q.push(addnode);
continue;
}
if (mp[x][y] == '-')
{
x = x + dir[i][0];
y = y + dir[i][1];
}
}
}
if (x < 0 || y < 0 || x >= n || y >= m || mp[x][y] == '*' || vis[x][y])
continue;
Node addnode;
addnode.x = x;
addnode.y = y;
addnode.step = node.step + 1;
addnode.t = T + 1;
vis[x][y] = 1;
Q.push(addnode);
}
}
if (Q.empty()&&mp[node.x][node.y]!='T')
printf("no way\n");
else
printf("%d\n", node.t);
}
return 0;
}
hdu1180诡异的楼梯 bfs
最新推荐文章于 2018-09-02 09:58:24 发布