[USACO2.4] The Tamworth Two of two Tamworth cattle

[USACO2.4] The Tamworth Two of two Tamworth cattle

In fact, this problem is not very difficult. Just pay attention to the direction.

After writing, I looked at the solution and found some similarities and differences:

1. Some variables

Like many big guys, I use 1, 2, 3 and 4 in direction to represent north, East, South and west respectively. And use variables to record the location of FarmerJohn and Cows, and use maps array to record the map.

2. On borders

Many bigwigs judged the boundary during the movement, but Ben thought it was too troublesome to judge the boundary, so he began to set the whole map from 0 to 11 to "*" (i.e. obstacles), read the data from 1 to 10, and then judge the obstacles during the movement.
The processed examples are as follows:

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

3. Mobile

The big guys' code seems a little redundant in moving. In fact, they only need to judge the direction of Farmer John and Cows and whether there are obstacles in the previous grid in this direction. If there are no obstacles, move one grid in that direction; If there is an obstacle, change the direction (direction + 1, if greater than 4, update to 1). However, pay special attention to the moving direction. I adjusted it here for 10 minutes!

So we can write FarmerJohn's mobile code:

void fjmove() {
	if(fjd==1&&maps[fjx-1][fjy]!='*') fjx=fjx-1;
	else if(fjd==2&&maps[fjx][fjy+1]!='*') fjy=fjy+1;
	else if(fjd==3&&maps[fjx+1][fjy]!='*') fjx=fjx+1;
	else if(fjd==4&&maps[fjx][fjy-1]!='*') fjy=fjy-1;   
	else if(fjd!=4) fjd=fjd+1;
	else fjd=1;
}

Similarly, we can also write cow's mobile code:

void comove() {
	if(cod==1&&maps[cox-1][coy]!='*') cox=cox-1;
	else if(cod==2&&maps[cox][coy+1]!='*') coy=coy+1;
	else if(cod==3&&maps[cox+1][coy]!='*') cox=cox+1;
	else if(cod==4&&maps[cox][coy-1]!='*') coy=coy-1;   
	else if(cod!=4) cod=cod+1;
	else cod=1;
}

Note: the variables fjx and FJY respectively refer to the xy coordinates of FarmerJohn, and the variable fjd represents the direction of FarmerJohn. The variables Cox and coy respectively refer to the xy coordinates of Cows, and the variable cod refers to the direction of Cows

4. Judge the meeting

It's actually very simple to judge whether there will be an encounter. Just set a relatively large value (I use 10000 here). When the time is greater than it, jump out of the loop and output 0.
Like this:

while(((fjx!=cox)||(fjy!=coy))&&timee<10000) {
	......
}	
if(timee==10000) cout<<"0";
else cout<<timee;

Note: the variable timee refers to the time when FarmerJohn caught Cows.

There is nothing else to pay attention to. The above code:

#include<bits/stdc++.h>
using namespace std;
char maps[1005][1005];
int fjx,fjy,fjd=1,cox,coy,cod=1,timee=0;
//direction:
//North:1
//East:2
//South:3
//West:4
void fjmove() {
	if(fjd==1&&maps[fjx-1][fjy]!='*') fjx=fjx-1;
	else if(fjd==2&&maps[fjx][fjy+1]!='*') fjy=fjy+1;
	else if(fjd==3&&maps[fjx+1][fjy]!='*') fjx=fjx+1;
	else if(fjd==4&&maps[fjx][fjy-1]!='*') fjy=fjy-1;   
	else if(fjd!=4) fjd=fjd+1;
	else fjd=1;
}
void comove() {
	if(cod==1&&maps[cox-1][coy]!='*') cox=cox-1;
	else if(cod==2&&maps[cox][coy+1]!='*') coy=coy+1;
	else if(cod==3&&maps[cox+1][coy]!='*') cox=cox+1;
	else if(cod==4&&maps[cox][coy-1]!='*') coy=coy-1;   
	else if(cod!=4) cod=cod+1;
	else cod=1;
}
int main() {
	for(int i=0;i<=11;i++)
		for(int j=0;j<=11;j++)
			maps[i][j]='*';
	for(int i=1;i<=10;i++)
		for(int j=1;j<=10;j++)
			cin>>maps[i][j];
	for(int i=1;i<=10;i++)
		for(int j=1;j<=10;j++)
			if(maps[i][j]=='F')
				fjx=i,fjy=j;
	for(int i=1;i<=10;i++)
		for(int j=1;j<=10;j++)
			if(maps[i][j]=='C')
				cox=i,coy=j;
	while(((fjx!=cox)||(fjy!=coy))&&timee<10000) {
		fjmove();
		comove();
		timee+=1;
	}	
	if(timee==10000) cout<<"0";
	else cout<<timee;
	return 0;
}

My AC evaluation record

Sample operation diagram:

Thank you!

Added by KingPhilip on Sun, 06 Mar 2022 13:58:35 +0200