USACO 2.4.1 两只塔姆沃斯牛 The Tamworth Two

博客给出题解,指出该题模拟即可。提到判断不会相遇的方法,一种是确定一个较大时间,超过就跳出;另一种是判断是否进入环,还给出了代码部分。

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

题解

模拟即可。注意怎样判断不会相遇,我比较暴力直接确定一个大一点的时间超过了就break,
当然不会相遇肯定是进入了一个环,可以借此判断。


Code

// head files excluded
using namespace std;
int n,m;
int cot[11][11];
int Mx[4]={-1,0,1,0};
int My[4]={0,1,0,-1};
int main(void){

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

    char c;
    int fx,fy,cx,cy;
    for(int i=1;i<=10;i++)
        for(int j=1;j<=10;j++){
            cin>>c;
            if(c=='*') cot[i][j]=1;
            if(c=='F'){
                fx = i;
                fy = j;
            } 
            if(c=='C'){
                cx = i;
                cy = j;
            }
        }

    int time,fd,cd;// direction
    time=fd=cd=0;

    int tmpx,tmpy;
    while( fx!=cx || fy!=cy  ){
        tmpx = fx+Mx[fd]; 
        tmpy = fy+My[fd]; 
        if( tmpx>0 && tmpx<11 && 
            tmpy>0 && tmpy<11 && 
            cot[tmpx][tmpy]!=1 ){
            fx = tmpx;
            fy = tmpy;
        }else {
            fd++;
            fd%=4;
        }

        tmpx = cx+Mx[cd]; 
        tmpy = cy+My[cd]; 
        if( tmpx>0 && tmpx<11 && 
            tmpy>0 && tmpy<11 && 
            cot[tmpx][tmpy]!=1 ){
            cx = tmpx;
            cy = tmpy;
        }else {
            cd++;
            cd%=4;
        }

        time++;
        if(time>5000) break;
    }


    if(time>5000) cout<<0<<endl;
    else cout<<time<<endl;
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值