-
Notifications
You must be signed in to change notification settings - Fork 1
/
LaserMaze.cpp
145 lines (144 loc) · 3 KB
/
LaserMaze.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include <bits/stdc++.h>
#define MAX 105
using namespace std;
typedef pair < int , int > pii;
typedef pair < pii, int > piii;
typedef vector < int > vi;
typedef vector < pii > vii;
int n , m;
vii data;//[0]= begin,[1]=end, laser pos.
vector < string > grip;
bool vis[MAX][MAX][4];
char graph[MAX][MAX][4];
int d[MAX][MAX][4];
int dc[4]={'<','^','>','v'};
int dy[4]={0, -1, 0, 1};// (i) row
int dx[4]={-1, 0, 1, 0};// (j) column
char fun( char lazer)
{
int pos=-1;
for(int i=0;i<4;i++)
if(lazer==dc[i])
{
pos=i;
break;
}
return dc[(pos+1)%4];
}
int getDir( char lazer)
{
for(int i=0;i<4;i++)
if(lazer==dc[i])
return i;
return -1;
}
bool validDisp(int i, int j, int k)
{
return i>=0 && j>=0 && i<n && j<m && (graph[i][j][k]=='.' || graph[i][j][k]=='x') ;
}
bool validMov(int i, int j, int k)
{
return i>=0 && j>=0 && i<n && j<m && graph[i][j][k]=='.';
}
void getGraph()
{
data.assign(2, pii(-1,-1));
for(int i=0;i < n;i++)
for(int j=0;j<m;j++)
{
if(grip[i][j]=='S')
{
data[0]=pii(i,j);
graph[i][j][0]=graph[i][j][1]=graph[i][j][2]=graph[i][j][3] ='.';
}
else if(grip[i][j]=='G')
{
data[1]=pii(i,j);
graph[i][j][0]=graph[i][j][1]=graph[i][j][2]=graph[i][j][3] ='.';
}
else
{
graph[i][j][0]=graph[i][j][1]=graph[i][j][2]=graph[i][j][3] =grip[i][j];
if(getDir(graph[i][j][0])>=0)
{
data.push_back(pii(i,j));
graph[i][j][1] = fun(graph[i][j][0]);
graph[i][j][2] = fun(graph[i][j][1]);
graph[i][j][3] = fun(graph[i][j][2]);
}
}
}
for(int i=2;i<data.size();i++)
{
int dire=getDir(graph[data[i].first][data[i].second][0]);
for(int k=0;k<4;k++)
{
int dir=(dire+k)%4;
int ni=data[i].first+dy[dir];
int nj=data[i].second+dx[dir];
while(validDisp(ni,nj,k) )
{
graph[ni][nj][k]='x';
ni+=dy[dir];
nj+=dx[dir];
}
}
}
}
void sol(piii ini)
{
memset(d, -1, sizeof d);
memset(vis, 0, sizeof vis);
d[ini.first.first][ini.first.second][ini.second] = 0 ;
queue < piii > q;
q.push(ini);
while(!q.empty())
{
piii u=q.front();
q.pop();
vis[u.first.first][u.first.second][u.second]=1;
if(u.first.first==data[1].first && u.first.second==data[1].second) break;
for(int k=0;k<4;k++)
{
int ni=u.first.first + dy[k];
int nj=u.first.second + dx[k];
int nl=(u.second + 1 ) %4;
if(validMov(ni, nj, nl) && !vis[ni][nj][nl])
{
vis[ni][nj][nl]=1;
q.push(piii(pii(ni, nj), nl));
d[ni][nj][nl]=d[u.first.first][u.first.second][u.second] + 1;
}
}
}
}
int main()
{
int test;
scanf("%d", &test);
for(int te=1;te<=test;te++)
{
grip.clear();
data.clear();
scanf("%d %d", &n, &m);
for(int i=0;i<n;i++)
{
string x;
cin>>x;
grip.push_back(x);
}
getGraph();
sol(piii(pii(data[0].first, data[0].second),0));
int pos=-1;
for(int i=0;i<4;i++)
if(d[data[1].first][data[1].second][i] !=-1)
{
pos=i;
break;
}
if(pos>=0)
printf("Case #%d: %d\n", te,d[data[1].first][data[1].second][pos]);
else
printf("Case #%d: impossible\n", te);
}
}