Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <fstream>
- #include <iostream>
- using namespace std;
- struct commandOutput {
- pair<int,int> cords;
- int dir;
- int lastBomb;
- };
- void changeIncr(int dir, int& incrx, int& incry){
- switch (dir) {
- case 0:
- incrx = 1;
- incry = 0;
- break;
- case 1:
- incrx = 0;
- incry = -1;
- break;
- case 2:
- incrx = -1;
- incry = 0;
- break;
- case 3:
- incrx = 0;
- incry = 1;
- }
- }
- int main() {
- ifstream in("input.txt");
- ofstream out("output.txt");
- int N, M, i;
- in>>N>>M;
- commandOutput comms[N];
- int last = 0, x = 0, y = 0, dir = 0, incrx = 1, incry = 0;
- char command;
- for (i = 0; i < N; i++) {
- in>>command;
- if (command == 'R') {
- dir = dir == 3 ? 0 : dir+1;
- changeIncr(dir, incrx, incry);
- } else if (command == 'L') {
- dir = dir == 0 ? 3 : dir-1;
- changeIncr(dir, incrx, incry);
- } else if (command == 'F') {
- x += incrx;
- y += incry;
- } else if (command == 'B') {
- x -= incrx;
- y -= incry;
- }
- comms[i].cords = {x,y};
- comms[i].dir = dir;
- comms[i].lastBomb = last;
- if (command == 'X') {
- last = i+1;
- }
- }
- int s, e;
- bool end = false;
- x = 0; y = 0; dir = 0;
- for (i = 0; i < M && !end; i++) {
- in>>s>>e;
- while(s < comms[e].lastBomb) {
- e = comms[e].lastBomb-1;
- end = true;
- }
- if(s == 0){
- incrx = comms[e].cords.first;
- incry = comms[e].cords.second;
- last = comms[e].dir;
- }else{
- incrx = comms[e].cords.first - comms[s-1].cords.first;
- incry = comms[e].cords.second - comms[s-1].cords.second;
- last = comms[e].dir - comms[s-1].dir;
- if(comms[s-1].dir == 1){
- int temp = incrx;
- incrx = -incry;
- incry = temp;
- }else if(comms[s-1].dir == 2){
- incrx = -incrx;
- incry = -incry;
- }else if(comms[s-1].dir == 3){
- int temp = incrx;
- incrx = incry;
- incry = -temp;
- }
- }
- if(dir == 0){
- x += incrx;
- y += incry;
- }else if(dir == 1){
- x += incry;
- y -= incrx;
- }else if(dir == 2){
- x -= incrx;
- y -= incry;
- }else{
- x -= incry;
- y += incrx;
- }
- dir += last;
- if(dir > 3){
- dir-=4;
- }
- if(dir < 0){
- dir+=4;
- }
- }
- out<<x<<' '<<y<<'\n';
- in.close();
- out.close();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement