1. 程式人生 > 資訊 >【視訊】蘋果最強晶片橫空出世,原來庫克也玩原神!

【視訊】蘋果最強晶片橫空出世,原來庫克也玩原神!

題目連結

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;
}