洛谷 P1518 两只塔姆沃斯牛 / 黄题 / 模拟

#include <bits/stdc++.h>
using namespace std;

string mp[12], s;
pair<int,int> C, F;
bool st[131][131][4][4];
int f, step, i, j, x, y, dx[4]={-1, 0, 1, 0}, dy[4]={0, 1, 0, -1};

int main () {
    mp[0]=mp[11]="************";
    for (int i=1; i<11; i++)  {cin >> s; mp[i]='*'+s+'*';}
    for (int i=1; i<11; i++)
        for (int j=1; j<11; j++) {
            if (mp[i][j]=='C') C={i,j};
            if (mp[i][j]=='F') F={i,j};
        }

    while (!f && !st[C.first*10+C.second][F.first*10+F.second][i][j]) {
        st[C.first*10+C.second][F.first*10+F.second][i][j]=true;

        x=dx[i]; y=dy[i];
        if (mp[C.first+x][C.second+y]!='*') {
            C.first+=x; C.second+=y;
        }
        else i=(i+1)%4;

        x=dx[j]; y=dy[j];
        if (mp[F.first+x][F.second+y]!='*') {
            F.first+=x; F.second+=y;
        }
        else j=(j+1)%4;

        step++;
        if (C.first*10+C.second==F.first*10+F.second) f=step;
    }

    cout << f;
    return 0;
}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值