请问大佬TLE了两个点,是被卡常了吗,谢谢!!!
查看原帖
请问大佬TLE了两个点,是被卡常了吗,谢谢!!!
627212
zyz21楼主2022/2/15 21:41
#include<iostream>


using namespace std;

int main() {
    pair<int, int> north{-1, 0}, east{0, 1}, south{1, 0}, west{0, -1}, step[4];
    step[0] = north;
    step[1] = east;
    step[2] = south;
    step[3] = west;
    int time = 0;
    bool field[10][10];
    pair<int, int> farmer, cow, farmer_initial, cow_initial, cow_future, farmer_future;
    for (int i = 0; i < 10; ++i) {
        for (int j = 0; j < 10; ++j) {
            char grid;
            cin >> grid;
            if (grid == '*')field[i][j] = false;
            else if (grid == '.')field[i][j] = true;
            else if (grid == 'C') {
                field[i][j] = true;
                cow.first = i, cow.second = j;
                cow_initial.first = i, cow_initial.second = j;
            } else if (grid == 'F') {
                field[i][j] = true;
                farmer.first = i, farmer.second = j;
                farmer_initial.first = i, farmer_initial.second = j;
            }
        }
    }
    int direction_f = 0, direction_c = 0;
    while (cow.first != farmer.first || cow.second != farmer.second) {
        if (cow.first == cow_initial.first && cow_initial.second == cow.second &&
            farmer.first == farmer_initial.first && farmer_initial.second == farmer.second && time > 0 &&
            direction_c == 0 && direction_f == 0) {
            time = 0;
            break;
        }
        time++;//一分钟过去了,下面让他们各自运动
        cow_future.first = cow.first + step[direction_c].first;
        cow_future.second = cow.second + step[direction_c].second;
        if (field[cow_future.first][cow_future.second] &&
            cow_future.first >= 0 && cow_future.first <= 9 &&
            cow_future.second >= 0 && cow_future.second <= 9) {
            cow.first = cow_future.first;
            cow.second = cow_future.second;
//            cout << "cow" << cow.first << " " << cow.second << endl;
        } else {
            ++direction_c %= 4;
//            cout<<"cow change direction: "<<direction_c<<endl;
        }
        farmer_future.first = farmer.first + step[direction_f].first;
        farmer_future.second = farmer.second + step[direction_f].second;
        if (field[farmer_future.first][farmer_future.second] &&
            farmer_future.first >= 0 && farmer_future.first <= 9 &&
            farmer_future.second >= 0 && farmer_future.second <= 9) {
            farmer.first = farmer_future.first;
            farmer.second = farmer_future.second;
//            cout << "farmer" << farmer.first << " " << farmer.second << endl;
        } else {
            ++direction_f %= 4;
//            cout<<"farmer change direction: "<<direction_f<<endl;
        }
    }
    cout << time << endl;
    return 0;
}
2022/2/15 21:41
加载中...