33分求大佬帮助
查看原帖
33分求大佬帮助
741732
small_Dongpo楼主2023/5/5 20:53

我的代码:

#include <iostream>
using namespace std;

char ch[15][15];
int ax[4] = {-1, 0, 1, 0};
int ay[4] = {0, 1, 0, -1};

int main()
{
    int fx, fy, ff = 0, cx, cy, cf = 0;
    for (int i = 1; i <= 10; ++i)
    {
        for (int j = 1; j <= 10; ++j)
        {
            cin >> ch[i][j];
            if (ch[i][j] == 'F')
            {
                fx = i;
                fy = j;
            }
            if (ch[i][j] == 'C')
            {
                cx = i;
                cy = j;
            }
        }
    }
    for (int i = 1; i <= 160000; ++i)
    {
        int tx = fx + ax[ff], ty = fy + ay[ff];
        while (ch[tx][ty] == '*' || tx < 1 || tx > 10 || ty < 1 || ty > 10)
        {
            ff = (ff + 1) % 4;
            tx = fx + ax[ff];
            ty = fy + ay[ff];
        }
        fx = tx;
        fy = ty;
        tx = cx + ax[cf], ty = cy + ay[cf];
        while (ch[tx][ty] == '*' || tx < 1 || tx > 10 || ty < 1 || ty > 10)
        {
            cf = (cf + 1) % 4;
            tx = cx + ax[cf];
            ty = cy + ay[cf];
        }
        cx = tx;
        cy = ty;
        if (fx == cx && fy == cy)
        {
            cout << i;
            return 0;
        }
    }
    cout << 0;
    return 0;
}
2023/5/5 20:53
加载中...