78分求助
查看原帖
78分求助
217577
kemkra楼主2020/7/30 16:29
#include <bits/stdc++.h>

using namespace std;

int f[3] = {0}, c[3] = {0};
char m[10][10];

void move(int * c)
{
    switch(c[2])
    {
    case 0:
        c[0] -= 1;
        break;
    case 1:
        c[1] += 1;
        break;
    case 2:
        c[0] += 1;
        break;
    case 3:
        c[1] -= 1;
        break;
    }
}

bool check(int x, int y, int r)
{
    if (
        x == 0 && r == 0 ||
        y == 9 && r == 1 ||
        x == 9 && r == 2 ||
        y == 0 && r == 3
    ) return 0;
    if (
        m[x - 1][y] == '*' && r == 0 ||
        m[x][y + 1] == '*' && r == 1 ||
        m[x + 1][y] == '*' && r == 2 ||
        m[x][y - 1] == '*' && r == 3
    ) return 0;
    return 1;
}

int rotate(int r)
{
    r += 1;
    if (r > 3) r -= 4;
    return r;
}

int main()
{
    int time = 0, init[2][2];
    bool catched = 1;
    for (int i = 0; i < 10; i++)
        for (int j = 0; j < 10; j++)
            {
                cin >> m[i][j];
                switch (m[i][j])
                {
                case 'F':
                    f[0] = i;
                    f[1] = j;
                    break;
                case 'C':
                    c[0] = i;
                    c[1] = j;
                    break;
                }
            }
    init[0][0] = f[0];
    init[0][1] = f[1];
    init[1][0] = c[0];
    init[1][1] = c[1];
    while (1)
    {
        if (check(f[0], f[1], f[2])) move(f);
        else f[2] = rotate(f[2]);
        if (check(c[0], c[1], c[2])) move(c);
        else c[2] = rotate(c[2]);
        time++;
        if (
            f[0] == init[0][0] &&
            f[1] == init[0][1] &&
            c[0] == init[1][0] &&
            c[1] == init[1][1]
        )
        {
            catched = 0;
            break;
        }
        if (f[0] == c[0] && f[1] == c[1]) break;
    }
    if (catched) cout << time;
    else cout << 0;
    return 0;
}

#1WA #6TLE

2020/7/30 16:29
加载中...