死循环+输出一直为0
查看原帖
死循环+输出一直为0
1232632
cclianlian楼主2024/6/11 16:01
using namespace std;
string s;
vector<string>a;
int dir[4][2] = { -1,0,0,1,1,0,0,-1 };
int ans;
int xf,yf,xc,yc;
void findf() {
	for (int i = 0; i < 10; i++) {
		for (int j = 0; j < 10; j++) {
			if (a[i][j] == 'F') {
				xf = i;
				yf = j;
				return;
			}
		}
	}
}
void findc() {
	for (int i = 0; i < 10; i++) {
		for (int j = 0; j < 10; j++) {
			if (a[i][j] == 'C') {
				xc = i;
				yc = j;
				return;
			}
		}
	}
}
void solve() {
	for (int i = 0; i < 10; i++) {
		cin >> s;
		a.push_back(s);
	}
	 findf();
	 findc();
	 int k1=0, k2=0;
	 while (true) {
		 for (k1; k1 < 4; k1 = (k1 + 1) % 4) {
			 int curx = xf + dir[k1][0];
			 int cury = yf + dir[k1][1];
			 if (curx < 0 || cury < 0 || curx >= 10 || cury >= 10||a[curx][cury]=='*') {
				 k1 = (k1 + 1) % 4;//改变方向
				 break;
			 }
				 xf = curx;
				 yf = cury;
				 break;
		 }
		 for (k2; k2 < 4; k2 = (k2 + 1) % 4) {
			 int curx = xc + dir[k1][0];
			 int cury = yc + dir[k1][1];
			 if (curx < 0 || cury < 0 || curx >= 10 || cury >= 10 || a[curx][cury] == '*') {
				 k2 = (k2 + 1) % 4;
				 break;
			 }
				 xc = curx;
				 yc = cury;
				 break;

		 }
		 ans++;
		 if (xf == xc && yf == yc) {
			 cout << ans;
			 return;
		 }
	 }
	 
	 cout << 0;
		
}

int main(){
	std::ios::sync_with_stdio(false);
	std::cin.tie(0); std::cout.tie(0);
	solve();
	return 0;
}
2024/6/11 16:01
加载中...