The Tamworth Two(技巧模拟)

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

 

       题意:

       给出一个 10 X 10 的地图,给出每个点的状态,F 代表 famer 的位置,C 代表 cow 的位置,* 代表障碍物,. 代表通道。F 和C 同时运动,输出他们相遇时候的步数。每次他们只会朝着一个方向走,一开始朝南,后碰到障碍物或者边界则换方向,方向沿顺时针转 90°。若无法相遇则输出 0 。转方向过程也算一个步数。

 

       思路:

       技巧模拟。用 dir = (dir + 1 ) % 4 来模拟旋转的情况,旋转的时候也算一个时间,故不用更新该点的位置,只是更新方向。每个点都有4个方向的状态,那么一共有4 * 10 * 10 = 400种状态,两个点那么最多也只会走 400 * 400 = 160000 次。故循环完160000还未找到的话,永远也就不会再找到了。

 

      AC:

/*
TASK:ttwo
LANG:C++
ID:sum-g1
*/
#include <cstdio>
#define MAX 160000
using namespace std;

char Map[15][15];
int fx,fy,cx,cy,df,dc,ans;
int dir[4][2] = {-1,0,0,1,1,0,0,-1};

void next() {
    fx += dir[df][0];
    fy += dir[df][1];
    if(fx < 1 || fx > 10 || fy < 1 || fy > 10 || Map[fx][fy] == '*') {
            fx -= dir[df][0];
            fy -= dir[df][1];
            df = (df + 1) % 4;
    }

    cx += dir[dc][0];
    cy += dir[dc][1];
    if(cx < 1 || cx > 10 || cy < 1 || cy > 10 || Map[cx][cy] == '*') {
            cx -= dir[dc][0];
            cy -= dir[dc][1];
            dc = (dc + 1) % 4;
    }
}

void solve() {
    df = dc = 0;
    while(ans <= MAX) {
        if(fx == cx && fy == cy) return;
        next();
        ans++;
    }
    if(ans == MAX + 1) ans = 0;
}

int main() {
    freopen("ttwo.in","r",stdin);
    freopen("ttwo.out","w",stdout);

    for (int i = 1; i <= 10; ++i)
            for (int j = 1; j <= 10; ++j) {
                    scanf(" %c",&Map[i][j]);
                    if (Map[i][j] == 'F') {
                            fx = i;
                            fy = j;
                    }
                    if (Map[i][j] == 'C') {
                            cx = i;
                            cy = j;
                    }
            }

    solve();

    printf("%d\n",ans);
    return 0;
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值