死循环求助
查看原帖
死循环求助
857349
BinSky楼主2024/10/2 21:26
#include <bits/stdc++.h>
#define ll long long
using namespace std;
struct node {
	int x, y;
	int dx, dy;
}f, c;
char ch[15][15];
int t;
void change(int &x, int &y) {
	if (x == 0 && y == -1) {
		x = 1, y = 0;
	} else if (x == 1 && y == 0) {
		x = 0, y = 1;
	} else if (x == 0 && y == 1) {
		x = -1, y = 0;
	} else {
		x = 0, y = -1;
	}
}
int main() {
//	ios::sync_with_stdio(0);
//	cin.tie(0), cout.tie(0);
	for (int i = 1; i <= 10; i++) {
		for (int j = 1; j <= 10; j++) {
			cin >> ch[i][j];
			if (ch[i][j] == 'F') {
				f.x = i, f.y = j;
				f.dx = 0, f.dy = -1;
			}
			if (ch[i][j] == 'C') {
				c.x = i, c.y = j;
				c.dx = 0, c.dy = -1;
			}
		}
	}
	while (true) {
		int xx = f.x + f.dx;
		int yy = f.y + f.dy;
		int x_ = c.x + c.dx;
		int y_ = c.y + c.dy;
		if (xx <= 10 && yy <= 10 && xx >= 1 && yy >= 1 && ch[xx][yy] == '.') {
			f.x = xx;
			f.y = yy;
		} else {
			change(f.dx, f.dy);
		} 
		if (x_ <= 10 && y_ <= 10 && x_ >= 1 && y_ >= 1 && ch[x_][y_] == '.') {
			c.x = x_;
			c.y = y_;
		} else {
			change(c.dx, c.dy);
		}
		t ++;
		if (f.x == c.x && f.y == c.y) {
			break;
		}
	}
	cout << t << endl;
	return 0;
}
2024/10/2 21:26
加载中...