【USACO】The Tamworth Two

模拟过程。。。

/*
ID :
LANG: C++11
TASK: ttwo
 */

#include 
#include 
#include 
#include 
#include 

using namespace std;



int direction[4][2] = {-1, 0, 0, 1, 1, 0, 0, -1};
int pos_f[2], pos_c[2];
int meet_time = 0;

int main()
{
    freopen("ttwo.in", "r", stdin);
    freopen("ttwo.out", "w", stdout);
    int map[11][11] = {};
    for (int i = 0; i < 10; i ++){
        for (int j = 0; j < 10; j ++) {
            char c;
            cin >> c;
            switch (c) {
                case '*': {
                    map[i][j] = 3;
                    break;
                }
                case 'F':{
                    pos_f[0] = i; pos_f[1] = j;
                    break;
                }
                case 'C':{
                    pos_c[0] = i; pos_c[1] = j;
                    break;
                }
                default:{
                    break;
                }
            }
        }
    }
    int dir_f = 0, dir_c = 0;
    //memcpy(dir_f, direction[0], sizeof(int) * 2);
    //memcpy(dir_c, direction[0], sizeof(int) * 2);

    for (int k = 0; k < 16000; k ++) {
        int npos_c[2], npos_f[2];
        npos_c[0] = pos_c[0] + direction[dir_c][0];
        npos_c[1] = pos_c[1] + direction[dir_c][1];
        npos_f[0] = pos_f[0] + direction[dir_f][0];
        npos_f[1] = pos_f[1] + direction[dir_f][1];
        if (npos_c[0] >= 0 && npos_c[0] <= 9 && npos_c[1] >= 0 && npos_c[1] <= 9 && map[npos_c[0]][npos_c[1]] != 3) {
            pos_c[0] += direction[dir_c][0];
            pos_c[1] += direction[dir_c][1];
        } else {
            dir_c += 1;
            dir_c %= 4;
        }
        if (npos_f[0] >= 0 && npos_f[0] <= 9 && npos_f[1] >= 0 && npos_f[1] <= 9 && map[npos_f[0]][npos_f[1]] != 3){
            pos_f[0] += direction[dir_f][0];
            pos_f[1] += direction[dir_f][1];
        }
        else{
            dir_f += 1;
            dir_f %= 4;
        }
        meet_time ++;
        if (pos_f[0] == pos_c[0] && pos_f[1] == pos_c[1]){
            cout << meet_time << endl;
            exit(0);
        }
    }
    cout << 0 << endl;
    return 0;
}

 

你可能感兴趣的:(USACO)