#include <stdlib.h> #include <stdio.h> #include <string.h> #include <stdbool.h> /** * Auto-generated code below aims at helping you parse * the standard input according to the problem statement. **/ ////////////////////////////////////// TO DO ///////////////////////////////////// LOOPtester: int tile[L][C+1][4]; // tile[a][b] = {breaker?, inverted?, direction, number of X}; //////////////////////////////////// run to checkloop count turns then print 1 by 1 for a lighter usage of memory // void _checkloop(tilerec, grid) bool _isnextvalid(char next, bool breaker){ if((next == 'X' && breaker == false) || next == '#') return false; else return true; } bool _loopscan(int tile[4], bool breaker, bool inverted, int direction, int Xctr){ if(tile[0] == (int)(breaker) && tile[1] == (int)(inverted) && tile[2] == direction && tile[3] == Xctr){ return true; }else return false; } int main(int argc, char * argv[]){ int L; // h int C; // w scanf("%d%d", &L, &C); fgetc(stdin); int turns = 0; int xy[2], nextxy[2]; // holders, xy: current position. xy[0] = abs, xy[1] = ord. char grid[L][C+1]; // recording gamegrid int Xctr = 0; int Tloc[2][2], tctr = 0; // teleporters locations, numbers of teleporters int trail[L][C+1][3]; // keeping track of each visited tile for(int i = 0; i < L; i++){ for(int j = 0; j < C; j++){ for(int jj = 0; jj < 3; jj++){ trail[i][j][jj] = -1; } } } //////////////////////////// Recording Inputs/Grid for (int i = 0; i < L; i++) { scanf("%[^\n]", grid[i]); fgetc(stdin); //tester fprintf(stderr, "%s\n", grid[i]); //OK for(int ii = 0; ii < C; ii++){ if(grid[i][ii] == '@'){ xy[0] = ii; xy[1] = i; // scanning starting point //tester //fprintf(stderr, "\n@ (%d , %d)\n", ii, i); //OK }else if(grid[i][ii] == 'T'){ //scanning teleporter Tloc[tctr][0] = ii; Tloc[tctr][1] = i; tctr++; //tester //fprintf(stderr, "\nt_%d (%d , %d)\n", tctr, ii, i); //OK }else if(grid[i][ii] == 'X'){ // counting Obstacles Xctr++; } } } char spec[] = "SENW"; char moveset[4][6] = {"SOUTH", "EAST", "NORTH", "WEST"}; int move[4][3] = {{0, 1, 0}, {1, 0, 1}, {0, -1, 2}, {-1, 0, 3}}; // vectorial vect[k][0] = abs, vect[k][1] = ord, vect[k][2] = rank in moveset. bool inverted = false, breaker = false, blocked = false; int direction = 0; trail[xy[1]][xy[0]][0] = breaker; trail[xy[1]][xy[0]][0] = inverted; trail[xy[1]][xy[0]][0] = direction; trail[xy[1]][xy[0]][0] = Xctr; //getmov while(grid[xy[1]][xy[0]] != '$'){ if(_loopscan(trail[xy[1]][xy[0]], breaker, inverted, direction, Xctr)){ printf("LOOP\n"); return 0; } //fprintf(stderr, "\nxy0: %d, xy1: %d\n", xy[0], xy[1]); if(grid[xy[1]][xy[0]] == 'I'){ if(inverted == true) inverted = false; else inverted = true; } if(grid[xy[1]][xy[0]] == 'B'){ if(breaker == true) breaker = false; else breaker = true; //fprintf(stderr, "\nturn: %d, break: %d\n", turns, breaker); } for(int i = 0; i < 4; i++){ if(grid[xy[1]][xy[0]] == spec[i]){ direction = i; break; } } //get nextxy nextxy[0] = xy[0] + move[direction][0]; nextxy[1] = xy[1] + move[direction][1]; if(grid[nextxy[1]][nextxy[0]] == 'T'){ if(nextxy[0] == Tloc[0][0] && nextxy[1] == Tloc[0][1]){ nextxy[0] = Tloc[1][0]; nextxy[1] = Tloc[1][1]; }else{ nextxy[0] = Tloc[0][0]; nextxy[1] = Tloc[0][1]; } }else if(grid[nextxy[1]][nextxy[0]] == 'X' && breaker == true){ grid[nextxy[1]][nextxy[0]] = ' '; Xctr--; } //if blocked, unblocked. if impossible return loop; if(!_isnextvalid(grid[nextxy[1]][nextxy[0]], breaker)){ blocked = true; while(blocked == true){ if(inverted == true){ for(int i = 3; i > -1; i--){ direction = i; nextxy[0] = xy[0] + move[i][0]; nextxy[1] = xy[1] + move[i][1]; if(_isnextvalid(grid[nextxy[1]][nextxy[0]], breaker)){ blocked = false; break; } } if(blocked != false){ printf("LOOP\n"); return 0; } }else{ for(int i = 0; i < 4; i++){ direction = i; nextxy[0] = xy[0] + move[i][0]; nextxy[1] = xy[1] + move[i][1]; if(_isnextvalid(grid[nextxy[1]][nextxy[0]], breaker)){ blocked = false; break; } } if(blocked != false){ printf("LOOP\n"); return 0; } } } } //update data xy[0] = nextxy[0]; xy[1] = nextxy[1]; turns++; // trail[a][b] = {breaker?, inverted?, direction, number of X}; trail[xy[1]][xy[0]][0] = breaker; trail[xy[1]][xy[0]][0] = inverted; trail[xy[1]][xy[0]][0] = direction; trail[xy[1]][xy[0]][0] = Xctr; printf("%s\n", moveset[direction]); } /* if(grid[xy[1]][xy[0]] == '$'){ for(int i = 0; i < turns; i++){ ///////////////// replace 1 with 0 when loop problem solved OK BRO? //fprintf(stderr, "%s\n", moveset[HSHdirection[i]]); printf("%s\n", ; } } */ return 0; } /* //tester printer /////////////////////////////////// switch(direction){ case 0: pgrid[xy[1]][xy[0]] = 'v'; break; case 1: pgrid[xy[1]][xy[0]] = '>'; break; case 2: pgrid[xy[1]][xy[0]] = '^'; break; case 3: pgrid[xy[1]][xy[0]] = '<'; break; } fprintf(stderr, "\n\n"); for (int i = 0; i < L; i++) { //tester fprintf(stderr, "%s\n", pgrid[i]); strcpy(pgrid[i], grid[i]); //OK } fprintf(stderr, "\n%s\t turns: %d\thsh: %d\n", moveset[direction], turns, HSHdirection[turns]); ////////////////////////////////////////// */