Skip to content

Instantly share code, notes, and snippets.

@Leko
Last active December 18, 2015 05:49
Show Gist options
  • Save Leko/5735545 to your computer and use it in GitHub Desktop.
Save Leko/5735545 to your computer and use it in GitHub Desktop.
AOJ_0104 Magical Tiles Time Limit : 1 sec, Memory Limit : 65536 KB
// clear time: 00:30
#include <iostream>
#include <string>
#include <vector>
#define REP(i, n) for ( int i = 0; i < n; i++ )
using namespace std;
void dump(vector<string> f, int x = -1, int y = -1) {
REP(i, f.size()) {
REP(j, f[i].size()) {
if ( i == y && j == x )
cout << "#";
else
cout << f[i][j];
}
cout << endl;
}
}
/*
0:上
1:右
2:下
3:左
*/
int mx[] = {0, 1, 0, -1},
my[] = {-1, 0, 1, 0};
vector< vector<int> > closed;
void go(vector<string> &f, int x, int y) {
// dump(f, x, y);
// cout << endl;
while( f[y][x] != '.' ) {
if ( closed[y][x] ) {
cout << "LOOP" << endl;
return;
}
closed[y][x] = 1;
int mv = -1;
switch(f[y][x]) {
case '^':
mv = 0;
break;
case '>':
mv = 1;
break;
case 'v':
mv = 2;
break;
case '<':
mv = 3;
break;
}
if ( mv != -1 ) {
x += mx[mv];
y += my[mv];
}
// dump(f, x, y); cout << endl;
}
cout << x << " " << y << endl;
}
int main() {
int w, h;
while( cin >> h >> w, w || h ) {
vector<string> f(h);
closed = vector< vector<int> >(h, vector<int>(w, 0));
REP(i, h) {
REP(j, w) {
closed[i][j] = 0;
}
}
REP(i, h) {
cin >> f[i];
}
go(f, 0, 0);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment