孩子调试修改了一下午,样例都过不去,但是又找不出错误,有没有大佬帮忙看看QAQ
查看原帖
孩子调试修改了一下午,样例都过不去,但是又找不出错误,有没有大佬帮忙看看QAQ
1163793
spindrift楼主2023/11/26 19:32
#include<iostream>                         
#include<cstring>
using namespace std;
char a[20][20];
int main() {
	int x, y;
	int nx, ny;//牛坐标
	int f1=-1, f2=0;
	int nf1 = -1,nf2 = 0;//牛的下一个坐标的变化(朝向)
	int time=0;
	for (int i = 0; i < 10; i++) {
		for (int j = 0; j < 10; j++) {
			cin >> a[i][j];
			if (a[i][j] == 'F')x = i, y = j;
   			if (a[i][j] == 'C') nx = i, ny = j;
		}
	}
	while (!(nx == x && ny == y) && time < 200000) {
		if (nx==x&&ny==y) break;
		if (a[x + f1][y + f2] == '*' || x + f1 <0 || y + f2< 0 || x + f1==9 || y + f2 == 9) {
			if (f1 == -1 && f2 == 0) f1 = 0, f2 = 1;
			else if (f1 == 0 && f2 == 1) f1 = 1, f2 = 0;
			else if (f1 == 1 && f2 == 0) f1 = 0, f2 = -1;
			else if (f1 == 0 && f2 == -1) f1 = -1, f2 = 0;
		}
		else {
			 x += f1; y += f2;
		}
		if (a[nx + nf1][ny + nf2] == '*' || nx + nf1 < 0 || ny + nf2 < 0 || nx + nf1==9|| ny + nf2==9) {
			if (nf1 == -1 && nf2 == 0) nf1 = 0, nf2 = 1;
			else if (nf1 == 0 && nf2 == 1) nf1 = 1, nf2 = 0;
    		else if (nf1 == 1 && nf2 == 0) nf1 = 0, nf2 = -1;
			else if (nf1 == 0 && nf2 == -1) nf1 = -1, nf2 = 0;
		}
		else {
			nx += nf1; ny += nf2;
		}
		time++;
	}
	if (time == 200000) cout << 0;//对死循环的判定
	else cout << time;
	return 0;
}
 

2023/11/26 19:32
加载中...