提交时间:2024-01-22 18:51:16

运行 ID: 123269

#include<bits/stdc++.h> #define int long long using namespace std; namespace Fast{ inline int fr(){ register int x = 0,f = 1; static char c = getchar(); while (c < '0' || c > '9'){ if (c == '-'){ f = -1; } c = getchar(); } while (c >= '0' && c <= '9'){ x = (x << 1) + (x << 3) + (c ^ 48); c = getchar(); } return x * f; } inline void fw(int x){ if (x == 0){ putchar('0'); return ; } if (x < 0){ putchar('-'); x = -x; } stack <char> s; while (x){ s.push(x % 10 + 48); x /= 10; } while (!s.empty()){ putchar(s.top()); s.pop(); } } inline void DEBUG(){ puts("awa"); } inline void CCF(){ ios::sync_with_stdio(false); cin.tie(0),cout.tie(0); } } using namespace Fast; char a[15][15]; bool f[15][15]; int cx,cy,cd,mx,my,md,o[5][3] = {{-1,0},{0,1},{1,0},{0,-1}}; void slove(){ int n = 10; for (int i = 1;i <= n;i++){ for (int j = 1;j <= n;j++){ cin>>a[i][j]; f[i][j] = (a[i][j] == '*'); if (a[i][j] == 'C'){ cx = i,cy = j,cd = 0; } else if (a[i][j] == 'M'){ mx = i,my = j,md = 0; } } } int cnt = 0; bool flag = false; while (cnt < 100){ cnt++; int scx = cx + o[cd][0],scy = cy + o[cd][1]; if (scx <= 0 || scx > n || scy <= 0 || scy > n || f[scx][scy]){ cd = (cd + 1) % 4; } else{ cx = scx,cy = scy; } int smx = mx + o[md][0],smy = my + o[md][1]; if (smx <= 0 || smx > n || smy <= 0 || smy > n || f[smx][smy]){ md = (md + 1) % 4; } else{ mx = smx,my = smy; } if (cx == mx && cy == my){ flag = true; break; } } fw(flag?cnt:-1),putchar('\n'); return ; } signed main(){ int t = fr(); while (t--){ slove(); } return 0; }