0% found this document useful (0 votes)
43 views2 pages

Dice Navigation Simulation Code

The document is a C++ program that simulates the movement of a dice on a grid based on input commands. It reads the grid dimensions, the initial position and direction of the dice, and a series of commands to move or rotate the dice. The program tracks whether the dice goes out of bounds and marks the position as lost if it does.

Uploaded by

a0978361492
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views2 pages

Dice Navigation Simulation Code

The document is a C++ program that simulates the movement of a dice on a grid based on input commands. It reads the grid dimensions, the initial position and direction of the dice, and a series of commands to move or rotate the dice. The program tracks whether the dice goes out of bounds and marks the position as lost if it does.

Uploaded by

a0978361492
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

#include <bits/stdc++.

h>
using namespace std;

int main() {
int a, b;
cin >> a >> b;

char di[4] = {'N', 'E', 'S', 'W'};


map <int, pair<int, int>> dice;
dice[0].first = 0;
dice[0].second = 1; // n
dice[1].first = 1;
dice[1].second = 0; // e
dice[2].first = 0;
dice[2].second = -1; // s
dice[3].first = -1;
dice[3].second = 0; // w

int lost, flag[a + 1][b + 1];

for (int i=0; i<a + 1; i++) {


for (int j=0; j<b + 1; j++) {
flag[i][j] = 0;
}
}

int x, y, d;
char c;

while (cin >> x >> y >> c) {


lost = 0;
if (c == 'N') {
d = 0;
} else if (c == 'E') {
d = 1;
} else if (c == 'S') {
d = 2;
} else if (c == 'W') {
d = 3;
}
cin.ignore();
string str;
getline(cin, str);

for (int i=0; i<str.length(); i++) {


if (lost == 0) {
if (str[i] == 'R'){
d = (d+1)%4;
} else if (str[i] == 'L') {
d = (d+3)%4;
} else if (str[i] == 'F') {
x += dice[d].first;
y += dice[d].second;

if (x > a || x < 0 || y > b || y < 0) {


x -= dice[d].first;
y -= dice[d].second;

if (flag[x][y] == 0) { //錯的就不能再錯了
lost = 1;
flag[x][y] = 1;
}
}
}
}

cout << x << " " << y << " " << di[d];
if (lost == 1) {
cout << " LOST";
}
cout << endl;

return 0;
}

You might also like