Logu p1518[USACO2.4] Two Tamworth Two

Posted by Derokorian on Sat, 22 Jan 2022 23:46:31 +0100

Title Description

Two cattle escaped into the forest. Farmer John began chasing the two cattle with his expertise. Your task is to simulate their behavior (Bull and John).

Follow at 10\times 1010 × 10. In a flat grid. A grid can be an obstacle, two cows (they're all together), or Farmer John. Both cows and Farmer John can be in the same grid (when they meet), but neither can enter the barrier grid.

A grid can be:

  • . Open space;
  • * Obstacles;
  • C. Two cows;
  • F Farmer John.

Here's an example of a map:

*...*.....
......*...
...*...*..
..........
...*.F....
*.....*...
...*......
..C......*
...*.*....
.*.*......

Cattle roam the map in a fixed way. They can move forward or turn every minute. If there are no barriers ahead (the edges of the map are also barriers), they will move in the same direction. Otherwise they will turn 90 degrees clockwise in this minute. At the same time, they don't leave the map.

Farmer John knows how cattle move, and so does he.

Each (minute) movement of Farmer John and the two cattle is the same. If they move across each other but don't meet in the same grid, we don't think they meet. When they meet in a grid at the end of a minute, the chase ends.

Read in ten lines to represent the map. Each line contains only 10 characters, which means the same thing as above. Make sure there is only one F and one C in the map. F and C will not be in the same grid at first.

Calculate how many minutes it will take Farmer John to grab his cattle, assuming that both the cattle and Farmer John are moving north (that is, up) at first. If John and the cow never meet, output 0.

Input Format

Enter 10 lines of 10 characters each to represent the map as described above.

Output Format

Output a number indicating how long it will take John to catch the cows. If John can't catch the cattle, output 0.

Input and Output Samples

Enter * Output *

*...*.....                            49
......*...
...*...*..
..........
...*.F....
*.....*...
...*......
..C......*
...*.*....
.*.*......

Code first

#include<iostream>
using namespace std;
const int N=100;
char a[N][N];
int t;
void Move(int a1,int b1,int a2,int b2)
{

	int dx[4]={-1,0,1,0},dy[4]={0,1,0,-1}; // Clockwise
	
	int k=0,l=0;

	for(t=1;t<1000;t++)	
	{
		
		if(a[a1+dx[k]][b1+dy[k]]=='.'|| a[a1+dx[k]][b1+dy[k]]=='F')
		{
			a[a1][b1]='.';
			a1=a1+dx[k];
			b1=b1+dy[k];
			a[a1][b1]='C';
		}
		else if(a[a1+dx[k]][b1+dy[k]]=='*'|| a1+dx[k]<0 || a1+dx[k]>9 || b1+dy[k]<0 || b1+dy[k]>9)
		{
			k++;
			if(k==4) k=0;
		}		
					
		if(a[a2+dx[l]][b2+dy[l]]=='.'|| a[a2+dx[l]][b2+dy[l]]=='C')
		{
			a[a2][b2]='.';
			a2=a2+dx[l];
			b2=b2+dy[l];
			a[a2][b2]='F';
		}			
		else if(a[a2+dx[l]][b2+dy[l]]=='*'|| a2+dx[l]<0 || a2+dx[l]>9 || b2+dy[l]<0 || b2+dy[l]>9 )
		{
			l++;
			if(l==4) l=0;
		}			
			
		if(a2==a1&&b2==b1)
		{
			cout<<t;
			return;
		}	
							
	} 
	cout<<0;
	return;			
}

int main()
{
	int a1,b1,a2,b2;
	
	for(int i=0;i<10;i++)
	{
		for(int j=0;j<10;j++)
		{
			cin>>a[i][j];
		}
	}
	
	for(int i=0;i<10;i++)
	{
		for(int j=0;j<10;j++)
		{
			if(a[i][j]=='C')
			{
				a1=i;
				b1=j;
			}
			
			if(a[i][j]=='F')
			{
				a2=i;
				b2=j;
			}			
		}
	}

	Move(a1,b1,a2,b2);
		
	return 0;
}

This question is not very thoughtful. Follow it step by step, but still need to pay attention to some details.

First read the map in a double loop without saying much about it. Then traverse the map and record the locations of cattle and farmers with variables A and B. For the function part:

We use

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

Simulate the four directions. Since it is clear that the questions rotate clockwise, the order of top, left, bottom and right cannot be disordered.

Since both the cow and the peasant are moving at the same time, we choose to simulate the action of both in the cycle. Otherwise, we need to add the condition of equal time to make additional judgment. Because the data on this topic is small, we think the two will never meet when the time is greater than 1000. Then we translate the topic to see if we can go straight or if we can't stop and turn clockwise. By the way, plus boundary judgment, the output time will be sufficient when the final encounter occurs.

PS: Although this topic is not difficult, as an embryonic, independent completion is still full of achievements.

Topics: C Algorithm