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

题目链接

https://www.luogu.com.cn/problem/P1518

题目思路

模拟农夫和奶牛行动路线,为了避免死循环,当ans到一定值时跳出

题目代码

#include <iostream>
#include <algorithm>

using namespace std;
char g[12][12];
int main()
{
    int cfx, cfy, ffx, ffy;
    for(int i = 0; i < 10; i ++ ) 
        for(int j = 0; j < 10; j ++ )
            {
                cin >> g[i][j];
                if(g[i][j] == 'F') ffx = i, ffy = j;
                if(g[i][j] == 'C') cfx = i, cfy = j;
            }
    int cf = 0, ff = 0, ans = 0;
    int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
    while(ffx != cfx || ffy != cfy)
    {
        if(ans >= 100000)
        {
            cout << "0" << endl;
            return 0;
        }
        ffx += dx[ff % 4], ffy += dy[ff % 4];
        cfx += dx[cf % 4], cfy += dy[cf % 4];
        if(ffx < 0 || ffx >= 10 || ffy < 0 || ffy >= 10 || g[ffx][ffy] == '*')
        {
            ffx -= dx[ff % 4], ffy -= dy[ff % 4];
            ff ++ ;
        }
        if(cfx < 0 || cfx >= 10 || cfy < 0 || cfy >= 10 || g[cfx][cfy] == '*')
        {
            cfx -= dx[cf % 4], cfy -= dy[cf % 4];
            cf ++ ;
        }
        ans ++ ;
    }
    cout << ans << endl;
    return 0;
}
posted @   vacilie  阅读(87)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统
· 【译】Visual Studio 中新的强大生产力特性
· 2025年我用 Compose 写了一个 Todo App
点击右上角即可分享
微信分享提示
主题色彩