Skip to code
Skip to analysis

This is a explanation of this problem from USACO's training website. I have converted it to markdown. Please do not just copy code; you will not learn anything; at least type it out and understand so you can do it yourself in the future!

A pair of cows is loose somewhere in the forest. Farmer John is lending his expertise to their capture. Your task is to model their behavior.

The chase takes place on a 10 by 10 planar grid. Squares can be empty or they can contain:

The cows and Farmer John can occupy the same square (when they ‘meet’) but neither the cows nor Farmer John can share a square with an obstacle.

Each square is represented as follows:

. Empty square
* Obstacle
C Cows
F Farmer

Here is a sample grid:


The cows wander around the grid in a fixed way. Each minute, they either move forward or rotate. Normally, they move one square in the direction they are facing. If there is an obstacle in the way or they would leave the board by walking ‘forward’, then they spend the entire minute rotating 90 degrees clockwise.

Farmer John, wise in the ways of cows, moves in exactly the same way.

The farmer and the cows can be considered to move simultaneously during each minute. If the farmer and the cows pass each other while moving, they are not considered to have met. The chase ends when Farmer John and the cows occupy the same square at the end of a minute.

Read a ten-line grid that represents the initial state of the cows, Farmer John, and obstacles. Each of the ten lines contains exactly ten characters using the coding above. There is guaranteed to be only one farmer and one pair of cows. The cows and Farmer John will not initially be on the same square.

Calculate the number of minutes until the cows and Farmer John meet. Assume both the cows and farmer begin the simulation facing in the ‘north’ direction. Print 0 if they will never meet.



Lines 1-10: Ten lines of ten characters each, as explained above




A single line with the integer number of minutes until Farmer John and the cows meet. Print 0 if they will never meet.

SAMPLE OUTPUT (file ttwo.out)







Russ Cox

We can simply simulate the movement of Farmer John and the two cows, but we need to find a way to detect loops. There are only 100 places where the cows can be, and 4 directions they can face. The same goes for Farmer John. This multiplies out to 400*400 = 160,000 different (place, direction) configurations for Farmer John and the cows. If we ever happen upon a configuration we have been in before, we are in an infinite loop, and John and the cows will never meet.

In fact, we don’t even need to keep track of which configurations we’ve seen. If they’re going to meet, they’re going to meet in fewer than 160,000 steps. Otherwise, they have to repeat some configuration, in which case they’ll never meet.

We take care of the simulation by keeping track of the position and direction of each. Direction is a number from 0 to 3, 0 = north, 1 = east, 2 = south, 3 = west. So turning clockwise is incrementing one. We calculate how to move given the direction by looking up offsets in the deltax and deltay arrays.

PROG: ttwo
ID: rsc001

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

char grid[10][10];

/* delta x, delta y position for moving north, east, south, west */
int deltax[] = { 0, 1, 0, -1 };
int deltay[] = { -1, 0, 1, 0 };

move(int *x, int *y, int *dir)
	int nx, ny;

	nx = *x+deltax[*dir];
	ny = *y+deltay[*dir];

	if(nx < 0 || nx >= 10 || ny < 0 || ny >= 10 || grid[ny][nx] == '*')
		*dir = (*dir + 1) % 4;
	else {
		*x = nx;
		*y = ny;

	FILE *fin, *fout;
	char buf[100];
	int i, x, y;
	int cowx, cowy, johnx, johny, cowdir, johndir;

	fin = fopen("", "r");
	fout = fopen("ttwo.out", "w");
	assert(fin != NULL && fout != NULL);

	cowx = cowy = johnx = johny = -1;

	for(y=0; y<10; y++) {
		fgets(buf, sizeof buf, fin);
		for(x=0; x<10; x++) {
			grid[y][x] = buf[x];
			if(buf[x] == 'C') {
				cowx = x;
				cowy = y;
				grid[y][x] = '.';
			if(buf[x] == 'F') {
				johnx = x;
				johny = y;
				grid[y][x] = '.';

	assert(cowx >= 0 && cowy >= 0 && johnx >= 0 && johny >= 0);

	cowdir = johndir = 0;	/* north */

	for(i=0; i<160000 && (cowx != johnx || cowy != johny); i++) {
		move(&cowx, &cowy, &cowdir);
		move(&johnx, &johny, &johndir);

	if(cowx == johnx && cowy == johny)
		fprintf(fout, "%d\n", i);
		fprintf(fout, "0\n");

Back to top