TLE求助!
查看原帖
TLE求助!
1242208
wyp20130701_是一个蒟蒻楼主2025/8/5 09:15
#include<iostream>
#include<climits>
using namespace std;
char map[15][15];
int main(){
	int fx, fy, cx, cy;
	for (int i = 1; i <= 10; i++)
		for (int j = 1; j <= 10; j++){
			cin >> map[i][j];
			if (map[i][j] == 'F'){
				fx = i;
				fy = j;
			}
			if (map[i][j] == 'C'){
				cx = i;
				cy = j;
			}
		}
	int cnt = 0;
	int fd = 0, cd = 0;
	while ((fx != cx || fy != cy) && cnt < INT_MAX){
		if (fd == 0){
			if (fx == 1 || map[fx - 1][fy] == '*'){
				fd = (fd + 1) % 4;
			}
			else{
				fx--;
			}
		}
		else if (fd == 1){
			if (fy == 10 || map[fx][fy + 1] == '*'){
				fd = (fd + 1) % 4;
			}
			else{
				fy++;
			}
		}
		else if (fd == 2){
			if (fx == 10 || map[fx + 1][fy] == '*'){
				fd = (fd + 1) % 4;
			}
			else{
				fx++;
			}
		}
		else if (fd == 3){
			if (fy == 1 || map[fx][fy - 1] == '*'){
				fd = (fd + 1) % 4;
			}
			else{
				fy--;
			}
		}
		if (cd == 0){
			if (cx == 1 || map[cx - 1][cy] == '*'){
				cd = (cd + 1) % 4;
			}
			else{
				cx--;
			}
		}
		else if (cd == 1){
			if (cy == 10 || map[cx][cy + 1] == '*'){
				cd = (cd + 1) % 4;
			}
			else{
				cy++;
			}
		}
		else if (cd == 2){
			if (cx == 10 || map[cx + 1][cy] == '*'){
				cd = (cd + 1) % 4;
			}
			else{
				cx++;
			}
		}
		else if (cd == 3){
			if (cy == 1 || map[cx][cy - 1] == '*'){
				cd = (cd + 1) % 4;
			}
			else{
				cy--;
			}
		}
		cnt++;
	}
	if (fx != cx || fy != cy) cout << 0;
	else cout << cnt;
	return 0;
}
2025/8/5 09:15
加载中...