(from luckycat)
解法:
直接依照題意模擬。
注意:
程式碼:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Tittle: 10377 - Maze Traversal | |
* Author: Cheng-Shih, Wong | |
* Date: 2015/07/20 | |
*/ | |
// include files | |
#include <bits/stdc++.h> | |
using namespace std; | |
// definitions | |
#define FOR(i,a,b) for( int i=(a),_n=(b); i<=_n; ++i ) | |
#define clr(x,v) memset( x, v, sizeof(x) ) | |
#define N 105 | |
// declarations | |
int t; | |
int row, col; | |
int curx, cury; | |
int curd; | |
char maze[N][N]; | |
const int dir[4][2] = { | |
{ -1, 0 }, | |
{ 0, 1 }, | |
{ 1, 0 }, | |
{ 0, -1 } | |
}; | |
const char d2s[4] = { 'N', 'E', 'S', 'W' }; | |
// functions | |
const bool valid( int x, int y ) | |
{ | |
return (1<=x && x<=row && 1<=y && y<=col && maze[x][y]==' '); | |
} | |
void init() | |
{ | |
char tmp[N]; | |
scanf( "%d%d", &row, &col ); | |
gets(tmp); | |
FOR( i, 1, row ) gets( maze[i]+1 ); | |
scanf( "%d%d", &curx, &cury ); | |
curd = 0; | |
} | |
// main function | |
int main( void ) | |
{ | |
char buf; | |
int nxtx, nxty; | |
scanf( "%d", &t ); | |
while( t-- ) { | |
init(); | |
while( (buf=getchar())!='Q' ) { | |
if( buf == 'F' ) { | |
nxtx = curx+dir[curd][0]; | |
nxty = cury+dir[curd][1]; | |
if( valid( nxtx, nxty ) ) { | |
curx = nxtx; | |
cury = nxty; | |
} | |
} else if( buf == 'R' ) { | |
++curd; | |
curd = (curd+4)%4; | |
} else if( buf == 'L' ) { | |
--curd; | |
curd = (curd+4)%4; | |
} | |
} | |
printf( "%d %d %c\n", curx, cury, d2s[curd] ); | |
if( t ) putchar('\n'); | |
} | |
return 0; | |
} |
沒有留言:
張貼留言
任何意見都樂意傾聽