#include<iostream>
using namespace std;
char a[12][12];
int n = 1, m = 0;
struct Animal
{
char name;
int location_x, location_y, direction;
}farmer,cows;
void initialize(Animal& s,char c,int i,int j)
{
s.name = c;
s.location_x = i;
s.location_y = j;
}
void start(void)
{
for (int i = 1; i < 11; i++)
for (int j = 1; j < 11; j++)
{
cin >> a[i][j];
if (a[i][j] == 'F')
initialize(farmer, 'F',i,j);
else if (a[i][j] == 'C')
initialize(cows, 'C',i,j);
}
}
void go(Animal& s)
{
switch (s.direction)
{
case 0:
if (a[s.location_x - 1][s.location_y] == '\0' || a[s.location_x - 1][s.location_y] == '*')
s.direction = 1;
else
{
a[s.location_x-1][s.location_y] = s.name;
a[s.location_x][s.location_y] = '.';
s.location_x--;
}
break;
case 1:
if (a[s.location_x][s.location_y + 1] == '\0' || a[s.location_x][s.location_y + 1] == '*')
s.direction = 2;
else
{
a[s.location_x][s.location_y+1] = s.name;
a[s.location_x][s.location_y] = '.';
s.location_y++;
}
break;
case 2:
if (a[s.location_x + 1][s.location_y] == '\0' || a[s.location_x + 1][s.location_y] == '*')
s.direction = 3;
else
{
a[s.location_x+1][s.location_y ] = s.name;
a[s.location_x][s.location_y] = '.';
s.location_x++;
}
break;
case 3:
if (a[s.location_x][s.location_y - 1] == '\0' || a[s.location_x][s.location_y - 1] == '*')
s.direction = 0;
else
{
a[s.location_x][s.location_y -1] = s.name;
a[s.location_x][s.location_y] = '.';
s.location_y--;
}
}
}
bool compare(const Animal& s, const Animal& t)
{
if (t.location_x == s.location_x && t.location_y == s.location_y)
return true;
else
return false;
}
void search(void)
{
for (;;n++)
{
go(farmer);
go(cows);
if (compare(farmer, cows))
break;
if (n > 10000)
{
n = 0;
break;
}
}
}
void show(void)
{
cout << n;
}
int main()
{
start();
search();
show();
return 0;
}
洛谷P1518 [USACO2.4]两只塔姆沃斯牛 The Tamworth Two进阶解法
最新推荐文章于 2023-08-26 11:59:06 发布