-
Notifications
You must be signed in to change notification settings - Fork 0
/
P1518.cpp
156 lines (151 loc) · 3.59 KB
/
P1518.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
//
// main.cpp
// P1518
//
// Created by 丁国真 on 2018/10/25.
// Copyright © 2018 丁国真. All rights reserved.
//
#include <bits/stdc++.h>
using namespace std;
char input[12][12];
int test ;
char dirf,dirc;
struct node{
int x ; int y ;
};
struct node F,C,Fori,Cori;
inline void readln(){
for (int i = 1 ; i < 11 ; i++){
for ( int j = 1 ; j < 11 ; j++){
cin >> input[i][j];
switch (input[i][j]) {
case 'F':
F.x=i;
F.y=j;
input[i][j]='.';
break;
case 'C':
C.x=i;
C.y=j;
input[i][j]='.';
default:
break;
}
}
}
}
inline void setsides(){
for (int i = 0 ; i < 12 ;i++){
input[0][i]='*';
input[i][0]='*';
input[11][i]='*';
input[i][11]='*';
}
}
inline void checkmap(){
for (register int i = 0 ; i < 12 ; i ++){
for (register int j = 0 ; j < 12 ; j++){
if (F.x==i and F.y ==j )cout << "F ";
else if (C.x==i and C.y ==j )cout << "C ";
else cout << input[i][j]<<' ';
}
cout << endl;
}
}
inline int check(){
if (F.x==C.x and F.y==C.y) return 1;
else if (F.x==Fori.x and F.y==Fori.y and C.y==Cori.y and C.x==Cori.x and dirf=='N' and dirc =='N') return 2;
return 0;
}
inline void turning(char a){
switch (a) {
case 'F':
if(dirf=='N') dirf='E';
else if(dirf=='E') dirf='S';
else if(dirf=='S') dirf='W';
else if(dirf=='W') dirf='N';
break;
case 'C':
if(dirc=='N') dirc='E';
else if(dirc=='E') dirc='S';
else if(dirc=='S') dirc='W';
else if(dirc=='W') dirc='N';
default:
break;
}
}
inline void walking(){
switch (dirf) {
case 'W':
if(input[F.x][F.y-1]=='*') turning('F');
else F.y-=1;
break;
case 'S':
if(input[F.x+1][F.y]=='*') turning('F');
else F.x += 1;
break;
case 'E':
if(input[F.x][F.y+1]=='*') turning('F');
else F.y += 1;
break;
case 'N':
if(input[F.x-1][F.y]=='*') turning('F');
else F.x -= 1;
break;
default:
break;
}
switch (dirc) {
case 'W':
if(input[C.x][C.y-1]=='*') turning('C');
else C.y-=1;
break;
case 'S':
if(input[C.x+1][C.y]=='*') turning('C');
else C.x += 1;
break;
case 'E':
if(input[C.x][C.y+1]=='*') turning('C');
else C.y += 1;
break;
case 'N':
if(input[C.x-1][C.y]=='*') turning('C');
else C.x -= 1;
break;
default:
break;
}
}
int main(int argc, const char * argv[]) {
test = 0 ;
readln();
setsides();
Fori=F;
Cori=C;
// checkmap();
register int cnt= 0 ;
dirf='N';
dirc='N';
while(true){
// checkmap();
// cout << endl;
if (cnt > 9*1e7) { // cnt max = 2*1e5 is enough
cout << 0;
exit(0);
}
cnt++;
walking();
switch (check()) {
case 1:
cout << cnt ;
exit(0);
break;
case 2:
cout << 0;
exit(0);
default:
break;
}
}
return 0;
}