关于一直输出0
查看原帖
关于一直输出0
649815
Gmor_cerr楼主2023/2/21 16:38

rt, 求调

#include <bits/stdc++.h>
using namespace std;
char ap[15][15];
int cx, cy, cv = 1, fx, fy, fv = 1;
int ans = 0;
bool judge(int fx, int fy, int cx, int cy) {
	if (fx == cx && fy == cy) return 1;
	else return 0;
}
void move(int x, int y, int v) {
	if (v == 1) {
		if (ap[x-1][y] == '*' || x - 1 < 0) v = 2;
		else x--;
	}
	if (v == 2) {
		if (ap[x][y+1] == '*' || y + 1 > 9) v = 3;
		else y++;
	}
	if (v == 3) {
		if (ap[x+1][y] == '*' || x + 1 > 9) v = 4;
		else x++;
	}
	if (v == 4) {
		if (ap[x][y-1] == '*' || y - 1 < 0) v = 1;
		else y--;
	}
	return ;
}
int main() {
	for (int i = 0; i < 10; i++) {
		for (int j = 0; j < 10; j++) {
			scanf("%c", &ap[i][j]);
			if (ap[i][j] == 'C') cx = i, cy = j;
			if (ap[i][j] == 'F') fx = i, fy = j;
		}
	}
	while (!judge(fx, fy, cx, cy)) {
		move(fx, fy, fv);
		move(cx, cy, cv);
		ans++;
		if (ans == 1e6) {
			puts("0");
			return 0;
		}
	}
	printf("%d", ans);
	return 0;
} 
2023/2/21 16:38
加载中...