|
Grid:
int grid[500][300]; // More than enough...
Representation:
grid[x][y] = 0 --> no tunnel at (x,y)
grid[x][y] = 1 --> (x,y) is part of tunnel
Current position:
int currX = 0, currY = 0;
|
void digDown(int n)
{
bool danger = false;
for ( int i = 1; i <= n; i++ )
{
if ( grid[x(currX)][y(currY-1)] == 1 )
danger = true;
grid[x(currX)][y(currY-1)] = 1; // Mark position as part of tunnel
currY--; // Go further
}
if ( !danger )
cout << (currX) << " " << (currY) << " safe" << endl;
else
{
cout << (currX) << " " << (currY) << " DANGER" << endl;
exit(0);
}
}
|
digUp( ), digLeft( ) and digRight( ) are similar...
void makeInitGrid(int grid[500][300])
{
digDown(3);
digRight(3);
digDown(2);
digRight(2);
digUp(2);
digRight(2);
digDown(4);
digLeft(8);
digUp(2);
}
|
while ( true )
{
cin >> dir;
cin >> steps;
if ( dir == 'q' )
break;
switch ( dir )
{
case 'l': digLeft(steps);
break;
case 'r': digRight(steps);
break;
case 'u': digUp(steps);
break;
case 'd': digDown(steps);
break;
}
}
|