过都能过,但有三个TLE,该怎么优化?
查看原帖
过都能过,但有三个TLE,该怎么优化?
613172
mianm楼主2022/1/25 17:34
#include<iostream>
using namespace std;
char map[10][10];
int main()
{
	void move(int* x, int* y,int* direction);
	long long t = 0;
	int fx, fy, cx, cy, directionf = 0, directionc = 0;
	int* fxd = &fx, * fyd = &fy, * cxd = &cx, * cyd = &cy;
	int* directionfd = &directionf, * directioncd = &directionc;
	for (int i = 0;i < 10;i++)
		for (int j = 0;j < 10;j++)
			cin >> map[i][j];
	for (int i = 0;i < 10;i++)
		for (int j = 0;j < 10;j++)
		{
			if (map[i][j] == 'F')
			{
				fx = j;
				fy = i;
			}
			if (map[i][j] == 'C')
			{
				cx = j;
				cy = i;
			}
		}
	for (long long i = 0;i < 10000000000;i++)
	{
		if (fx == cx && fy == cy)
		{
			t = i;
			break;
		}
		move(fxd, fyd,directionfd);
		move(cxd, cyd,directioncd);
	}
	cout << t << endl;
	return 0;
}
void move(int* x, int* y, int* direction)
{
	switch (*direction)
	{
	case 0:if (map[*y - 1][*x] == '*' || *y - 1 < 0)
		*direction = 1;
		   else (*y)--;break;
	case 1:if (map[*y][*x + 1] == '*' || *x + 1 > 9)
		*direction = 2;
		   else (*x)++;break;
	case 2:if (map[*y + 1][*x] == '*' || *y + 1 > 9)
		*direction = 3;
		   else (*y)++;break;
	case 3:if (map[*y][*x - 1] == '*' || *x - 1 < 0)
		*direction = 0;
		   else (*x)--;break;
	}
}
2022/1/25 17:34
加载中...