89分求助,#8TLE
查看原帖
89分求助,#8TLE
319478
zhibuba楼主2020/5/26 23:06
#include <stdio.h>
#include <stdbool.h>

enum dir {N, E, S, W
	};
bool moved = false;

bool move(int p[],enum dir t, bool map[][10])
{
	switch (t)
	{
		case N :
			if (map[p[0] - 1][p[1]] || p[0] == 0)
				return true;
			else
				p[0]--, moved = true; return false;
		case E :
			if (map[p[0]][p[1] + 1] || p[1] == 9)
				return true;
			else
				p[1]++, moved = true; return false;
		case S :
			if (map[p[0] + 1][p[1]] || p[0] == 9)
				return true;
			else
				p[0]++, moved = true; return false;
		case W :
			if (map[p[0]][p[1] - 1] || p[1] == 0)
				return true;
			else
				p[1]--, moved = true; return false;		
	}
}

int main(void)
{
	bool map[10][10] = {{false}};
	int PF[2], PC[2], PF0[2], PC0[2];
	enum dir DF = N, DC = N;
	int time = 0;
	for (int i = 0; i < 10; i++)
	{
		char c;
		for (int j = 0; j < 10; j++)
			if ((c = getchar()) == '*')
				map[i][j] = true;
			else if (c == 'F')
				PF0[0] = PF[0] = i, PF0[1] = PF[1] = j;
			else if (c == 'C')
				PC0[0] = PC[0] = i, PC0[1] = PC[1] = j;
		while ((c = getchar()) != '\n')
			;
	}
	while (1)
	{
		DF = (DF + move(PF, DF, (bool (*)[10])map)) % 4;
		DC = (DC + move(PC, DC, (bool (*)[10])map)) % 4;
		time++;
		if (PF[0] == PC[0] && PF[1] == PC[1] )
		{
			printf("%d", time);
			return 0;
		}
		else if (time > 40000 || (moved && PF[0] == PF0[0] && PF[1] == PF0[1] && PC[0] == PC0[0] && PC[1] == PC0[1]))
		{
			printf("0");
			return 0;
		} 
	}	
}
2020/5/26 23:06
加载中...