USACO section 2.4 The Tamworth Two(算模拟吧)

博客分析了USACO中关于农夫John追捕两只逃窜奶牛的问题。在10x10网格中,根据双方的固定移动模式,通过模拟他们的行进路径来确定相遇所需的时间。如果在400000步内未相遇,则说明他们永远不会相遇,因为会陷入无限循环。解决方案是跟踪每个实体的位置和方向,通过数组deltax和deltay确定移动方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

The Tamworth Two
BIO '98 - Richard Forster

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:

  • an obstacle,
  • the cows (who always travel together), or
  • Farmer John.

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:
*...*.....
......*...
...*...*..
..........
...*.F....
*.....*...
...*......
..C......*
...*.*....
.*.*......

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.

PROGRAM NAME: ttwo

INPUT FORMAT

 

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

SAMPLE INPUT (file ttwo.in)

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

OUTPUT FORMAT

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)

49

就是模拟它的行进路线即可,只要能遇到必定在4*10*10*4*10*10(图为10*10,4个方向,一牛一人)的步骤内相遇否则永远不相遇,因此只要判断比这大就输0;

原先做踩雷了,用了深搜,后来发现不用。

 

/*
 ID:nealgav1
 LANG:C++
 PROG:ttwo
*/
#include<fstream>
using namespace std;
ifstream cin("ttwo.in");
ofstream cout("ttwo.out");
char map[12][12];
bool s[12][12][4];
const int oo=1000000;
const int dx[]={0,-1,0,1};
const int dy[]={-1,0,1,0};//左上右下
int step;
int xx[3],yy[3];
void dfs(int a,int b,int c,int x,int y,int z)
{  while(1)
  { int nx,ny,na,nb;
  na=a+dx[c];nb=b+dy[c];
  nx=x+dx[z];ny=y+dy[z];
  if(a==x&&b==y)break;
  else
  {
     if(step>oo)break;
    if(na<0||nb<0||na>9||nb>9||map[na][nb]=='*')
    {
      na=a;nb=b;c=(c+1)%4;
    }
    if(nx<0||ny<0||nx>9||ny>9||map[nx][ny]=='*')
    {
      nx=x;ny=y;z=(z+1)%4;
    }
    a=na;b=nb;x=nx;y=ny;
    step++;
   // if(step==32534)cout<<na<<" "<<nb<<" "<<c<<" "<<nx<<" "<<ny<<" "<<z<<endl;
    //cout<<step<<endl;
    //dfs(na,nb,c,nx,ny,z);
  }
  }
  if(step>oo)step=0;
}
int main()
{
  for(int i=0;i<10;i++)
  cin>>map[i];
  for(int i=0;i<10;i++)
  for(int j=0;j<10;j++)
  if(map[i][j]=='C')xx[0]=i,yy[0]=j;
  else if(map[i][j]=='F')xx[1]=i,yy[1]=j;
  step=0;
  dfs(xx[0],yy[0],1,xx[1],yy[1],1);
  cout<<step<<"\n";
}


 

 

 

USER: Neal Gavin Gavin [nealgav1]
TASK: ttwo
LANG: C++

Compiling...
Compile: OK

Executing...
   Test 1: TEST OK [0.000 secs, 3344 KB]
   Test 2: TEST OK [0.000 secs, 3344 KB]
   Test 3: TEST OK [0.011 secs, 3344 KB]
   Test 4: TEST OK [0.000 secs, 3344 KB]
   Test 5: TEST OK [0.000 secs, 3344 KB]
   Test 6: TEST OK [0.011 secs, 3344 KB]
   Test 7: TEST OK [0.000 secs, 3344 KB]
   Test 8: TEST OK [0.011 secs, 3344 KB]
   Test 9: TEST OK [0.000 secs, 3344 KB]

All tests OK.

YOUR PROGRAM ('ttwo') WORKED FIRST TIME! That's fantastic -- and a rare thing. Please accept these special automated congratulations.

Here are the test data inputs:

------- test 1 ----
.****...*.
..*......*
*.........
..........
*........*
*.**.*..**
F..*......
***....*.*
.C.......*
.......*.*
------- test 2 ----
..........
..........
..........
....*.....
....F.....
....C.....
..........
..........
..........
..........
------- test 3 ----
..........
.********.
.********.
.********.
.********.
.********.
.********.
C********.
.********.
F.........
------- test 4 ----
..........
.*........
........*.
...*......
......*...
....*.*...
F....*....
..*.......
C......*..
*.........
------- test 5 ----
...*.....*
**.......*
.....*..*.
*.*.......
.........*
F.....*.*.
**.*C....*
.......**.
.....*....
....*.*...
------- test 6 ----
..........
.......C..
..........
****..****
...*..*...
...*..*...
****..****
..........
..F.......
..........
------- test 7 ----
*...*....*
.......*..
.*...*.F..
.*........
....*..**.
*.......*.
......**..
*..C......
..*...*..*
*.....*.*.
------- test 8 ----
*..C.**...
..**...*.*
**......*.
.....**.*.
......*.**
.......**.
**..**...*
.**.......
*...*....*
*...F.*...
------- test 9 ----
*.*.**..*.
*.*.*..*.*
*.*.**.***
*.*.C*.*.*
***.**.*.*
..........
F***...**.
*.....*..*
*.....*..*
.***...**.
Keep up the good work!
The Tamworth Two
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 };

void
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;
	}
}

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


	fin = fopen("ttwo.in", "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);
	else
		fprintf(fout, "0\n");
	exit(0);
}
USACO Gateway

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值