#include <iostream>
#include <windows.h>
using namespace std;
int main()
{
int n, m, x, y;
bool L_pressed = false, R_pressed = false;
bool U_pressed = false, D_pressed = false;
cout << "Please input your map size (n, m): ";
cin >> n >> m;
cout << "Please input robot place (x, y): ";
cin >> x >> y;
while (true) {
// A boolean to record there's any key be pressed.
bool changed = false;
// Left Key
int L_key = GetKeyState(VK_LEFT);
// if L_key >= 0 (More specifically, 0 or 1), means
// L_key now is not being pressed. Let L_pressed be false.
// else, let L_pressed be true, and also set changed to be true.
if (L_key >= 0)
L_pressed = false;
else if (!L_pressed) {
changed = true, L_pressed = true;
// if the robot's position can still go left, we let it go.
if (y != 0)
y--;
}
// RIGHT Key
int R_key = GetKeyState(VK_RIGHT);
if (R_key >= 0)
R_pressed = false;
else if (!R_pressed) {
changed = true, R_pressed = true;
if (y != m-1)
y++;
}
// UP Key
int U_key = GetKeyState(VK_UP);
if (U_key >= 0)
U_pressed = false;
else if (!U_pressed) {
changed = true, U_pressed = true;
if (x != 0)
x--;
}
// DOWN Key
int D_key = GetKeyState(VK_DOWN);
if (D_key >= 0)
D_pressed = false;
else if (!D_pressed) {
changed = true, D_pressed = true;
if (x != n-1) {
x++;
}
}
// If there's any key be pressed, print the map.
if (changed) {
/*
// Use "====" to seperate two printed map.
for (int j = 0; j < m; j++) {
cout << "=";
}
cout << endl;
*/
// Use cls command to clean the input.
system("cls");
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (i == x && j == y)
cout << '@';
else
cout << '.';
}
cout << endl;
}
}
}
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8d2luZG93cy5oPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwppbnQgbWFpbigpCnsKICAgIGludCBuLCBtLCB4LCB5OwogICAgYm9vbCBMX3ByZXNzZWQgPSBmYWxzZSwgUl9wcmVzc2VkID0gZmFsc2U7CiAgICBib29sIFVfcHJlc3NlZCA9IGZhbHNlLCBEX3ByZXNzZWQgPSBmYWxz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ICAgICAgICBpZiAoUl9rZXkgPj0gMCkKICAgICAgICAgICAgUl9wcmVzc2VkID0gZmFsc2U7CiAgICAgICAgZWxzZSBpZiAoIVJfcHJlc3NlZCkgewogICAgICAgICAgICBjaGFuZ2VkID0gdHJ1ZSwgUl9wcmVzc2VkID0gdHJ1ZTsKICAgICAgICAgICAgaWYgKHkgIT0gbS0xKQogICAgICAgICAgICAgICAgeSsrOwogICAgICAgIH0KICAgICAgICAvLyBVUCBLZXkKICAgICAgICBpbnQgVV9rZXkgPSBHZXRLZXlTdGF0ZShWS19VUCk7CiAgICAgICAgaWYgKFVfa2V5ID49IDApCiAgICAgICAgICAgIFVfcHJlc3NlZCA9IGZhbHNlOwogICAgICAgIGVsc2UgaWYgKCFVX3ByZXNzZWQpIHsKICAgICAgICAgICAgY2hhbmdlZCA9IHRydWUsIFVfcHJlc3NlZCA9IHRydWU7CiAgICAgICAgICAgIGlmICh4ICE9IDApCiAgICAgICAgICAgICAgICB4LS07CiAgICAgICAgfQogICAgICAgIC8vIERPV04gS2V5CiAgICAgICAgaW50IERfa2V5ID0gR2V0S2V5U3RhdGUoVktfRE9XTik7CiAgICAgICAgaWYgKERfa2V5ID49IDApCiAgICAgICAgICAgIERfcHJlc3NlZCA9IGZhbHNlOwogICAgICAgIGVsc2UgaWYgKCFEX3ByZXNzZWQpIHsKICAgICAgICAgICAgY2hhbmdlZCA9IHRydWUsIERfcHJlc3NlZCA9IHRydWU7CiAgICAgICAgICAgIGlmICh4ICE9IG4tMSkgewogICAgICAgICAgICAgICAgeCsrOwogICAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIC8vIElmIHRoZXJlJ3MgYW55IGtleSBiZSBwcmVzc2VkLCBwcmludCB0aGUgbWFwLiAKICAgICAgICBpZiAoY2hhbmdlZCkgewogICAgICAgICAgICAvKgogICAgICAgICAgICAvLyBVc2UgIj09PT0iIHRvIHNlcGVyYXRlIHR3byBwcmludGVkIG1hcC4KICAgICAgICAgICAgZm9yIChpbnQgaiA9IDA7IGogPCBtOyBqKyspIHsKICAgICAgICAgICAgICAgIGNvdXQgPDwgIj0iOwogICAgICAgICAgICB9CiAgICAgICAgICAgIGNvdXQgPDwgZW5kbDsKICAgICAgICAgICAgKi8KICAgICAgICAgICAgLy8gVXNlIGNscyBjb21tYW5kIHRvIGNsZWFuIHRoZSBpbnB1dC4KICAgICAgICAgICAgc3lzdGVtKCJjbHMiKTsKICAgICAgICAgICAgZm9yIChpbnQgaSA9IDA7IGkgPCBuOyBpKyspIHsKICAgICAgICAgICAgICAgIGZvciAoaW50IGogPSAwOyBqIDwgbTsgaisrKSB7CiAgICAgICAgICAgICAgICAgICAgaWYgKGkgPT0geCAmJiBqID09IHkpCiAgICAgICAgICAgICAgICAgICAgICAgIGNvdXQgPDwgJ0AnOwogICAgICAgICAgICAgICAgICAgIGVsc2UKICAgICAgICAgICAgICAgICAgICAgICAgY291dCA8PCAnLic7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBjb3V0IDw8IGVuZGw7CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICB9CiAgICByZXR1cm4gMDsKfQ==