跳到正文

个人记录

猫和老鼠

jijibao约 2 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 2 分钟

版本

  1. v1当前
C++
#include <bits/stdc++.h>
using namespace std;
const int nx[] = {-1, 0, 1, 0};
const int ny[] = {0, 1, 0, -1};
int n = 10, sx, sy, xx, xy;
char maps[15][15];
bool check(int x, int y, int t1) {
	if (maps[x + nx[t1]][y + ny[t1]] == '.') return true;
	return false;
}
int moved(int x, int y, int a, int b, int step, int t1, int t2) {
	if (x == a && y == b) return step;
	if (step >= 60) return 61;
	if (check(x, y, t1)) x += nx[t1], y += ny[t1];
	else t1 = (t1 + 1) % 4;
	if (check(a, b, t2)) a += nx[t2], b += ny[t2];
	else t2 = (t2 + 1) % 4;
	return moved(x, y, a, b, step + 1, t1, t2);
}
int main() {
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= n; j++) {
			cin >> maps[i][j];
			if (maps[i][j] == 'C') {
				sx = i;
				sy = j;
				maps[i][j] = '.';
			}
			if (maps[i][j] == 'M') {
				xx = i;
				xy = j;
				maps[i][j] = '.';
			}
		}
	}
	int ans = moved(sx, sy, xx, xy, 0, 0, 0);
	if (ans < 60) cout << ans;
	else cout << -1;
	return 0;
}
原文