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 <iostream> #include <vector> #define x first #define y second
using namespace std;
typedef pair<int, int> pii; const int N = 20; const int dx[]{ -1, 0, 1, 0 }; const int dy[]{ 0, 1, 0, -1 };
int ax, ay, bx, by, rows = 13, cols = 14, direction = 0; int g[N][N]; vector<pii> snake[2];
int nextMove() { for(int i = 0; i < 4; i ++) { int x = snake[0][snake[0].size() - 1].x + dx[i]; int y = snake[0][snake[0].size() - 1].y + dy[i]; if(x >= 0 && y >= 0 && x < rows && y < cols && g[x][y] == 0) { return i; } } return 0; }
bool check_tail_increasing(int steps) { if (steps <= 10) { return true; } return steps % 3 == 1; }
vector<pii> getCells(int sx, int sy, string steps) { steps = steps.substr(1, steps.length() - 2); vector<pii> res; int x = sx, y = sy; int step = 0; res.push_back({x, y}); for (int i = 0; i < steps.size(); i++) { int d = steps[i] - '0'; x += dx[d]; y += dy[d]; res.push_back({x, y}); if (!check_tail_increasing(++step)) { res.erase(res.begin()); } } return res; }
void before() { string str, split = "#"; vector<string> s; cin >> str; if (str == "") return; string strs = str + split; size_t pos = strs.find(split); while (pos != strs.npos) { string temp = strs.substr(0, pos); s.push_back(temp); strs = strs.substr(pos + 1, strs.size()); pos = strs.find(split); } for (int i = 0, k = 0; i < rows; i++) { for (int j = 0; j < cols; j++, k++) { if (s[0][k] == '1') { g[i][j] = 1; } } }
ax = stoi(s[1],0,10); ay = stoi(s[2],0,10); bx = stoi(s[4],0,10); by = stoi(s[5],0,10); snake[0] = getCells(ax, ay, s[3]); snake[1] = getCells(bx, by, s[6]);
for (pii c : snake[0]) { g[c.x][c.y] = 1; } for (pii c : snake[1]) { g[c.x][c.y] = 1; } }
int main() { before(); direction = nextMove(); cout << direction; return 0; }
|