-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathttwo.cpp
102 lines (96 loc) · 2.66 KB
/
ttwo.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;
template <typename T1, typename T2>
ostream &operator <<(ostream &os, pair<T1, T2> p){os << p.first << " " << p.second; return os;}
template <typename T>
ostream &operator <<(ostream &os, vector<T> &v){for(T i : v)os << i << ", "; return os;}
template <typename T>
ostream &operator <<(ostream &os, set<T> s){for(T i : s) os << i << ", "; return os;}
template <typename T1, typename T2>
ostream &operator <<(ostream &os, map<T1, T2> m){for(pair<T1, T2> i : m) os << i << endl; return os;}
struct state{
pii loc;
int ort; // 0=north, 1=east, 2=south, 3=west
};
int hashState(state &s1, state &s2){
return s1.loc.first + 10*s1.loc.second + 100*s1.ort + 400*s2.loc.first + 4000*s2.loc.second + 40000*s2.ort;
}
void move(state &s, string (&b)[10]){
if(s.ort == 0){
if(s.loc.second == 0 || b[s.loc.first][s.loc.second-1] == '*'){
s.ort++;
s.ort %= 4;
}else{
s.loc.second--;
}
}else if(s.ort == 1){
if(s.loc.first == 9 || b[s.loc.first+1][s.loc.second] == '*'){
s.ort++;
s.ort %= 4;
}else{
s.loc.first++;
}
}else if(s.ort == 2){
if(s.loc.second == 9 || b[s.loc.first][s.loc.second+1] == '*'){
s.ort++;
s.ort %= 4;
}else{
s.loc.second++;
}
}else{
if(s.loc.first == 0 || b[s.loc.first-1][s.loc.second] == '*'){
s.ort++;
s.ort %= 4;
}else{
s.loc.first--;
}
}
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
freopen("ttwo.in", "r", stdin);
freopen("ttwo.out", "w", stdout);
string btmp[10], b[10];
state cow;
state farmer;
for(int i = 0; i < 10; i++){
cin >> btmp[i];
for(int j = 0; j < 10; j++){
if(btmp[i][j] == 'F'){
farmer.loc = pii(j, i);
btmp[i][j] = '.';
}
if(btmp[i][j] == 'C'){
cow.loc = pii(j, i);
btmp[i][j] = '.';
}
b[j][i] = btmp[i][j];
}
}
bool been[200000];
fill(been, been+200000, false);
int i = 0;
bool good = true;
for(; true; i++){
if(farmer.loc == cow.loc){
break;
}
if(been[hashState(farmer, cow)]){
good = false;
break;
}
been[hashState(farmer, cow)] = true;
move(farmer, b);
move(cow, b);
}
if(good){
cout << i << endl;
}else{
cout << 0 << endl;
}
return 0;
}