【USACO2.4.1】两只塔姆沃斯牛 模拟

纯粹的按照题目意思的模拟,

小技巧:

const int dx[4] = {-1, 0, 1, 0};
const int dy[4] = {0, 1, 0, -1}; 定义方向变动数组。    改变方向的话,只要+1 再MOD 4即可。


#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;

char map[12][12];
int fx,fy,cx,cy;
int df=0, dc=0;

void init()
{
	for (int i = 0; i != 12; ++ i)
		for (int j = 0; j != 12; ++ j)	map[i][j] = '*';
	for (int i = 1; i <= 10; ++ i)	
	{
		scanf("%s", map[i] + 1) ;
		map[i][strlen(map[i] + 1) + 1] = '*';
	}
	for (int i = 1; i <= 10; ++ i)
		for (int j = 1; j <= 10; ++ j)
		{
			if (map[i][j] == 'C')
			{
				cx = i;
				cy = j;
			}	
			if (map[i][j] == 'F')
			{
				fx = i;
				fy = j;
			}
		}
}

const int dx[4] = {-1, 0, 1, 0};
const int dy[4] = {0, 1, 0, -1};

void doit()
{
	int t = 0;
	while (++t <= 1000)
	{
		int nfx = fx + dx[df];
		int nfy = fy + dy[df];
		int ncx = cx + dx[dc];
		int ncy = cy + dy[dc];
		if (map[nfx][nfy] == '*')	
		{
			df = (df + 1) % 4;
			nfx = fx;
			nfy = fy;
		}
		if (map[ncx][ncy] == '*')
		{
			dc = (dc + 1) % 4;
			ncx = cx;
			ncy = cy;
		}
		if (cx == fx && cy == fy)
		{
			cout << t - 1 << endl;
			return;	
		}
		fx = nfx, fy = nfy, cx = ncx, cy = ncy;
	}
	cout<< 0 << endl;
}

int main()
{
	init();
	doit();
	return 0;
}


你可能感兴趣的:(【USACO2.4.1】两只塔姆沃斯牛 模拟)