USACO2.4.1 The Tamworth Two 两只塔姆沃斯牛 解题报告(模拟)

本文提供USACO 2.4.1 The Tamworth Two问题的解题报告,描述了两只牛和农民John在10x10网格中的移动规则。农民和牛每分钟同步移动,目标是求出农民抓住牛所需的时间,或者判断是否无法抓住。示例输入和输出展示了问题的具体场景和解决方案的思路,建议采用模拟的方法求解。

2.4.1 The Tamworth Two 两只塔姆沃斯牛

Time Limit: 1 Sec  Memory Limit: 64 MB
Submit: 18  Solved: 14
[Submit][Status][Discuss]

Description

两只牛在森林里故意走丢了。农民John开始用他的专家技术追捕这两头牛。你的任务是模拟他们的行为(牛和John)。 追击在10x10的平面网格内进行。一个格子可以是: 一个障碍物, 两头牛(它们总在一起), 或者 农民John. 两头牛和农民John可以在同一个格子内(当他们相遇时),但是他们都不能进入有障碍的格子。 一个格子可以是: . 空地 * 障碍物 C 两头牛 F 农民John 这里有一个地图的例子::

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

牛在地图里以固定的方式游荡。每分钟,它们可以向前移动或是转弯。如果前方无障碍且不会离开地图,它们会按照原来的方向前进一步。否则它们会用这一分钟顺时针转90度。 农民John, 深知牛的移动方法,他也这么移动。 每次(每分钟)农民John和两头牛的移动是同时的。如果他们在移动的时候穿过对方,但是没有在同一格相遇,我们不认为他们相遇了。当他们在某分钟末在某格子相遇,那么追捕结束。开始时,John和牛都面向北方。

 

Input

Lines 1-10: 每行10个字符,表示如上文描述的地图。

Output

输出一个数字,表示John需要多少时间才能抓住牛们。输出0,如果John无法抓住牛。

Sample Input

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

Sample Output

49

 

从F和C点向上走,如果不能走则顺时针转向90°,问需要多久两点重合

直接模拟就好,设一个大数值让其到达此数值跳出,否则相遇时跳出

 

代码如下

#include <map>
#include <cstdlib>
#include <cmath>
#include <cstdio>
#include <string>
#include <cstring>
#include <fstream>
#include <iostream>
#include <sstream>
#include <algorithm>
#define lowbit(a) (a&(-a))
#define _mid(a,b) ((a+b)/2)
#define debu(a) cout << a <<" ";
#define debug(a) cout << a << endl;
#define _mem(a,b) memset(a,0,(b+3)<<2)
#define fori(a) for(int i=0;i<a;i++)
#define forj(a) for(int j=0;j<a;j++)
#define ifor(a) for(int i=1;i<=a;i++)
#define jfor(a) for(int j=1;j<=a;j++)
#define mem(a,b) memset(a,b,sizeof(a))
#define IN freopen("in.txt","r",stdin)
#define OUT freopen("out.txt","w",stdout)
#define IO do{\
    ios::sync_with_stdio(false);\
    cin.tie(0);\
    cout.tie(0);}while(0)
#define mp(a,b) make_pair(a,b);
using namespace std;
typedef long long ll;
const int maxn =  15;
const int INF = 0x3f3f3f3f;
const int inf = 0x3f;
const double EPS = 1e-7;
const double Pi = acos(-1);
const int MOD = 1e9+7;
char g[maxn][maxn];
int dir[4][2] = {1,0,0,1,-1,0,0,-1};
struct node{
    int x,y,m;
}a,b;
bool juge(int x,int y){return x&&x<=10&&y&&y<=10&&g[x][y]!='*';}
 
void move(node &a){
    if(!juge(a.x+dir[a.m][0],a.y+dir[a.m][1]))
        a.m = (a.m+1)%4;
    else
        a.x+=dir[a.m][0],a.y+=dir[a.m][1];
}
 
void getres(){
    int cnt = 0;
    while(cnt < 5000&&++cnt){
        move(a);
        move(b);
        if(a.x==b.x&&a.y==b.y)
            break;
    }
    cout << (cnt==5000?0:cnt)<<endl;
}
 
int main() {
    int n = 10;
    a.m = b.m = 0;
 
    for(int i=10;i>0;i--)
        jfor(10){
            cin >> g[i][j];
            if(g[i][j]=='F')
                a.x = i,a.y = j;
            else if(g[i][j] == 'C')
                b.x = i,b.y = j;
        }
    getres();
    return 0;
}
 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值