P1518 [USACO2.4] 两只塔姆沃斯牛 The Tamworth Two

模拟 + dfs

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> PII;
char mp[10][10];
PII Mydistance[4] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};

int t = 0;
int x, y, a, b;
void dfs(int x, int y, int x1, int y1, int i, int j)
{
    int flag = 0;
    if(x == x1 && y1 == y) return;
    if(t > 400) return;
    int tempx = x + Mydistance[i % 4].first;
    int tempy = y + Mydistance[i % 4].second;
    int tempx1 = x1 + Mydistance[j % 4].first;
    int tempy1 = y1 + Mydistance[j % 4].second;

    if(tempx < 0 || tempx >= 10 || tempy < 0 || tempy >= 10 || mp[tempx][tempy] == '*') i ++, flag += 2;
    if(tempx1 < 0 || tempx1 >= 10 || tempy1 < 0 || tempy1 >= 10 || mp[tempx1][tempy1] == '*') j ++, flag -= 3;
    t ++;
    if(flag == 2)
    {
        dfs(x, y, tempx1, tempy1, i, j);
    }
    else if(flag == -3)
    {
        dfs(tempx, tempy, x1, y1, i, j);
    }
    else if(flag == -1)
    {
        dfs(x, y, x1, y1, i, j);
    }
    else if(flag == 0)
    {
        dfs(tempx, tempy, tempx1, tempy1, i, j);
    }
}

int main()
{
    for(int i = 0; i < 10; i ++)
        for(int j = 0; j < 10; j ++) 
        {
            cin >> mp[i][j];
            if(mp[i][j] == 'F') a = i, b = j;
            if(mp[i][j] == 'C') x = i, y = j;
        }
    dfs(x, y, a, b, 0, 0);
    if(t <= 400) cout << t;
    else cout << 0;
    return 0;
}

PII存4个方向, 地图大小是100 如果走超过400步算找不到。flag判断障碍情况

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值