Question link ~~>
Question recognition:This question is very appealing, from time-out Optimization to AC.
Solution:
If the figure is not pre-processed, the simulation will definitely time out ~ The graph needs to be processed in advance, because the orientation N and S are Y unchanged, x changes, W and e x remain unchanged, and Y is changed. in this way, we can use two vectors to store the values of Y when x remains unchanged, and the values of X when y remains unchanged. In this way, we can fix the values of X or Y when querying, and then find the changed values. The maximum complexity is 26*1000*1000.
Code:
# Include <iostream> # include <sstream> # include <map> # include <cmath> # include <fstream> # include <queue> # include <vector> # include <sstream> # include <cstring> # include <cstdio> # include <stack> # include <bitset> # include <ctime> # include <string> # include <iomanip> # include <algorithm> using namespace STD; # define int _ int64const int INF = 99999999; const double ESP = 0.0000000001; const double Pi = ACOs (-1.0); const int my = 100000 + 5; const int MX = 1000 + 5; int n, m, num, Q, gnum; char s [MX] [MX], S1 [50]; int DX [4] = {-1, 1, 0, 0}, Dy [4] = {0, 0,-1, 1}; // N, S, w, evector <int> GX [MX], GY [MX]; struct node // saved points {char ch; int X, Y;} t [50]; struct move // each instruction {int Dir, step;} Move [my]; void input () {int X, Y; num = 0; // record the number of scenic spots gnum = 0; // record # The number of for (INT I = 0; I <n; ++ I) {scanf ("% s ", s [I]); For (Int J = 0; j <m; ++ J) if (s [I] [J]> = 'A' & S [I] [J] <= 'Z ') // It is an attraction {T [num]. X = I; t [num]. y = J; t [num ++]. ch = s [I] [J];} else if (s [I] [J] = '#') // yes the wall {GX [I]. push_back (j); Gy [J]. push_back (I) ;}} scanf ("% d", & Q); char ch; For (INT I = 0; I <q; ++ I) // enter the command {CIN> CH> X; If (CH = 'n') y = 0; else if (CH = 's') y = 1; else if (CH = 'W') y = 2; else y = 3; move [I]. dir = y; // record command move [I]. Step = x ;}} bool solve (int x, int y) {int Sx, Sy, Dir, step, MT; For (INT I = 0; I <q; ++ I) // execute the Q command {dir = move [I] at a time. dir; step = move [I]. step; If (! DIR) // n y unchanged {SX = x + dx [dir] * step; MT = Gy [Y]. size (); If (SX <0 | SX> = N) return false; // For (Int J = 0; j <MT; ++ J) if (GY [y] [J]> = SX & Gy [y] [J] <X) return false; X = SX;} else if (DIR = 1) // y remains unchanged under s {SX = x + dx [dir] * step; MT = Gy [Y]. size (); If (SX <0 | SX> = N) return false; // For (Int J = 0; j <MT; ++ J) if (GY [y] [J]> X & Gy [y] [J] <= SX) return false; X = SX ;} Else if (DIR = 2) // W left X unchanged {Sy = Y + dy [dir] * step; MT = GX [X]. size (); If (SY <0 | Sy> = m) return false; For (Int J = 0; j <MT; ++ J) if (Gx [x] [J] <Y & GX [x] [J]> = Sy) return false; y = sy ;} else // e right X unchanged {Sy = Y + dy [dir] * step; MT = GX [X]. size (); If (SY <0 | Sy> = m) return false; For (Int J = 0; j <MT; ++ J) if (Gx [x] [J]> Y & GX [x] [J] <= Sy) return false; y = Sy ;}} RET Urn true;} int main () {While (~ Scanf ("% d", & N, & M) {input (); int Nx = 0; bool flag = false; For (INT I = 0; I <num; ++ I) if (solve (T [I]. x, t [I]. y) // determine whether the start point is valid {flag = true; S1 [NX ++] = T [I]. ch;} If (FLAG) {S1 [NX] = '\ 0'; stable_sort (S1, S1 + Nx); cout <S1 <Endl ;} else cout <"no solution" <Endl; For (INT I = 0; I <gnum; ++ I) {GX [I]. clear (); Gy [I]. clear () ;}} return 0 ;}
Codeforces 106 D Treasure Island