-
Notifications
You must be signed in to change notification settings - Fork 0
/
10047.cpp
108 lines (94 loc) · 2.14 KB
/
10047.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include <cstdio>
#include <cmath>
#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
#include <vector>
#include <utility>
#include <stack>
#include <queue>
#include <map>
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define pi 2*acos(0.0)
#define eps 1e-9
#define PII pair<int,int>
#define PDD pair<double,double>
#define LL long long
#define INF 1000000000
using namespace std;
int dr[]={-1, 0, 1, 0};
int dc[]={ 0, 1, 0,-1};
typedef struct
{
int r,c,dir,color,t;
} cell;
bool visited[30][30][4][5];
char grid[30][30];
int bawah,samping,x,y,z,c,ans;
PII start,finish;
queue<cell> q;
int main()
{
c=0;
while(1)
{
scanf("%d %d",&bawah,&samping);
if(bawah+samping==0) break;
for(y=0;y<bawah;y++) scanf("%s",grid[y]);
for(y=0;y<bawah;y++)
for(x=0;x<samping;x++)
{
memset(visited[y][x],false,sizeof(visited[y][x]));
if(grid[y][x]=='S')
{
grid[y][x]='.';
start=mp(y,x);
}
if(grid[y][x]=='T')
{
grid[y][x]='.';
finish=mp(y,x);
}
}
while(!q.empty()) q.pop();
q.push((cell){start.fi,start.se,0,0,0});
visited[start.fi][start.se][0][0]=true;
ans=INF;
while(!q.empty())
{
cell now=q.front();q.pop();
if((mp(now.r,now.c)==finish)&&(!now.color))
{
ans=now.t;
break;
}
//maju
int sr=now.r+dr[now.dir],sc=now.c+dc[now.dir];
if((sr>-1)&&(sc>-1)&&(sr<bawah)&&(sc<samping)&&(grid[sr][sc]=='.'))
if(!visited[sr][sc][now.dir][(now.color+1)%5])
{
visited[sr][sc][now.dir][(now.color+1)%5]=true;
q.push((cell){sr,sc,now.dir,(now.color+1)%5,now.t+1});
}
//hadap kiri kanan
for(int i=-1;i<=1;i+=2)
{
int sdir=(now.dir+i+4)%4;
if(!visited[now.r][now.c][sdir][now.color])
{
visited[now.r][now.c][sdir][now.color]=true;
q.push((cell){now.r,now.c,sdir,now.color,now.t+1});
}
}
}
if(c) printf("\n");
printf("Case #%d\n",++c);
if(ans==INF) printf("destination not reachable\n");
else printf("minimum time = %d sec\n",ans);
}
return 0;
}