提交时间:2024-01-21 14:04:42

运行 ID: 120951

#include <bits/stdc++.h> using namespace std; int n, a[20][20], cx, cy, mx, my, dir_c, dir_m; int dx[4] = {-1, 0, 1, 0}; int dy[4] = {0, 1, 0, -1}; int main() { scanf("%d", &n); while (n--) { memset(a, 0, sizeof a); for (int i=1; i<=10; i++) { for (int j=1; j<=10; j++) { char c; scanf("%c", &c); if (c != '*' && c != 'C' && c!= '.' && c != 'M') { j--; continue; } if (c == '*') a[i][j] = 1; if (c == 'C') { cx = i, cy = j; } if (c == 'M') { mx = i, my= j; } } } int ans=0; dir_c = dir_m = 0; for (int i=1; i<=100; i++) { if (cx == mx && cy == my) { break; } int tcx=cx, tcy=cy, tmx=mx, tmy=my; tcx += dx[dir_c], tcy += dy[dir_c]; tmx += dx[dir_m], tmy += dy[dir_m]; if (a[tcx][tcy] || (tcx < 1 || tcx > 10 || tcy < 1 || tcy > 10)) { dir_c++; dir_c %= 4; } else { cx = tcx, cy = tcy; } if (a[tmx][tmy] || (tmx < 1 || tmx > 10 || tmy < 1 || tmy > 10)) { dir_m++; dir_m %= 4; } else { mx = tmx, my = tmy; } ans = i; } if (ans == 100) printf("%d\n", -1); else printf("%d\n", ans); } return 0; }