从前不信命,从这道题开始,我信了。
我就是没有拿牌子的命。这道题或者说这个memset,击碎了我所有对ACM的美好记忆。
#include<bits/stdc++.h>
using namespace std;
const int N = 1e3 + 10;
int r, c, w, h;
char s[N*6][N*6];
bool vis[N*6][N*6];
int dir[2][6] = {{0, 0, 3, -3, 3, -3}, {2, -2, 1, 1, -1, -1}};
struct node{
int x, y, t;
}st;
bool check(int x, int y){
if(x > 0 && x < w && y > 0 && y <h) return true;
return false;
}
int BFS(){
queue<node> Q;
node u, v;
u.x = st.x;
u.y = st.y;
u.t = st.t;
vis[u.x][u.y] = true;
Q.push(u);
while(!Q.empty()){
v = Q.front();
Q.pop();
if(s[v.x][v.y] == 'T') return v.t;
for(int i = 0; i < 6; i++){
int x = v.x + dir[1][i];
int y = v.y + dir[0][i];
int dx = x + dir[1][i];
int dy = y + dir[0][i];
if(s[x][y] == ' ' && !vis[dx][dy] && check(dx, dy)){
vis[dx][dy] = true;
u.x = dx; u.y = dy; u.t = v.t + 1;
Q.push(u);
}
}
}
return -1;
}
int main(){
//freopen("in.txt", "r", stdin);
int T;
scanf("%d", &T);
while(T--){
scanf("%d%d", &r, &c);
getchar();
w = 4 * r + 3;
h = 6 * c + 3;
for(int i = 0; i < w; i++)
gets(s[i]);
for(int i = 0; i < w; i++){
int len = strlen(s[i]);
for(int j = 0; j < len; j++){
if(s[i][j] == 'S'){
st.x = i; st.y = j; st.t = 1;
break;
}
}
if(st.t) break;
}
int ans = BFS();
printf("%d\n", ans);
for(int i = 0; i < w; i++){
int len = strlen(s[i]);
for(int j = 0; j < len; j++){
s[i][j] = '\0';
vis[i][j] = false;
}
}
st.x = 0; st.y = 0; st.t = 0;
r = c = w = h = 0;
}
return 0;
}