usaco The Tamworth Two

 john和牛在二维的图里面跑,john要抓住牛。

牛和john在遇到墙或者图的边界时都会花一单位的时间顺时针转动90度。否则花一单位时间往前走一格,当牛和john走到同一个格子中时,john抓住了牛。

求john抓住牛要多久。抓不住时输出0.

一开始想了很久如何判断抓不住的情况,相通了之后还是很简单的。

john和牛都走到了以前走到位置,且保持着以前相同的方向。则说明陷入了循环(再也抓不住了)

/*
ID: modengd1
PROG: ttwo
LANG: C++
*/
#include <iostream>
#include <stdio.h>
#include <memory.h>
int dx[4]={-1,0,1,0};
int dy[4]={0,1,0,-1};
using namespace std;
bool vis[10][10][4][10][10][4];
char input[10][10];
struct position
{
    pair<int,int> P;
    int dir;
};
void Move(position& x)
{
    //装墙或者出地图
    int nextX=x.P.first+dx[x.dir],nextY=x.P.second+dy[x.dir];
    if(input[nextX][nextY]=='*'||nextX<0||nextX>9||nextY<0||nextY>9)
    {
        x.dir=(x.dir+1)%4;
        return ;
    }
    //往前走
    x.P.first=nextX;
    x.P.second=nextY;
}
int main()
{
    freopen("ttwo.in","r",stdin);
    freopen("ttwo.out","w",stdout);
    position john,cow;
    memset(vis,false,sizeof(vis));
    for(int i=0;i<10;i++)
    {
        for(int j=0;j<10;j++)
        {
            scanf("%c",&input[i][j]);
            if(input[i][j]=='F')
            {
                john.P.first=i;
                john.P.second=j;
                john.dir=0;
            }
            else if(input[i][j]=='C')
            {
                cow.P.first=i;
                cow.P.second=j;
                cow.dir=0;
            }
        }
        getchar();
    }
    int ans=0;
    position& J=john;
    position& C=cow;
    while(true)
    {
        if(john.P.first==cow.P.first&&john.P.second==cow.P.second)
            break;
        if(vis[john.P.first][john.P.second][john.dir][cow.P.first][cow.P.second][cow.dir])
        {
            ans=0;
            break;
        }
        vis[john.P.first][john.P.second][john.dir][cow.P.first][cow.P.second][cow.dir]=true;
        Move(J);
        Move(C);
        ans++;
    }
    cout<<ans<<endl;
    return 0;
}

  

转载于:https://www.cnblogs.com/modengdubai/p/4784077.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值