放弃这个吧:因为打印的时候 0,0是在左上角!
-------------------------------
#include <iostream>
#include <string>
#include <queue>
#include <cstdio>
using namespace std;
//0-9 * 0-9 : 100个元素
// 1代表自己 2代表墙 不能通过
int a[10][10] = {
{1, 0, 2, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 2, 0, 0, 0, 0, 0, 0, 0},
{2, 0, 2, 0, 0, 0, 0, 0, 0, 0},
{2, 0, 2, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 2, 0, 0, 2, 2, 0, 0, 0},
{0, 0, 2, 0, 0, 2, 0, 0, 0, 0},
{0, 0, 2, 2, 2, 2, 0, 2, 2, 0},
{0, 0, 0, 0, 0, 0, 0, 2, 0, 0},
{0, 0, 0, 0, 2, 2, 2, 2, 0, 0},
{0, 0, 0, 0, 2, 0, 0, 0, 0, 0},
} ;
int x, y; //当前坐标
//打印出来
void show(int a[10][10] = a, int minIndex = 0, int maxIndex = 9) {
for (int i = minIndex; i <= maxIndex; i++) {
for (int j = minIndex; j <= maxIndex; j++) {
printf("%2d", a[i][j]);
}
cout << endl;
}
cout << endl;
}
//W A S D
void go(char ch) {
switch (ch) {
case 'S':// 0 0 -> 1 0
if (y + 1 <= 9 && a[y + 1][x] != 2) {
// cout << "now pos is " << y << "," << x << endl;
swap(a[y][x], a[y + 1][x]);
y++;
cout << "下" << endl;
// cout << "now pos is " << y << "," << x << endl;
} else {
cout << "越界||墙!" << endl;
}
break;
case 'W':
if (y - 1 >= 0 && a[y - 1][x] != 2) {
swap(a[y][x], a[y - 1][x]);
y--;
cout << "上" << endl;
} else {
cout << "墙!" << endl;
}
break;
case 'A'://左, 1,1 到 1,0
if (x - 1 >= 0 && a[y][x - 1] != 2) {
// cout << "now POS is " << y << "," << x << endl;
swap(a[y][x], a[y][x - 1]);
x--;
cout << "左" << endl;
// cout << "now pos is " << y << "," << x << endl;
} else {
cout << "墙!" << endl;
}
break;
case 'D':
if (x + 1 <= 9 && a[y][x + 1] != 2) {
// cout << "now x,y is " << x << "," << y << endl;
swap(a[y][x], a[y][x + 1]);
x++;
cout << "右" << endl;
// cout << "now x,y is " << x << "," << y << endl;
} else {
cout << "墙!" << endl;
}
break;
}
show();
}
int main() {
char ch;
while (cin >> ch && ch != 'Q') {
go(ch);
}
return 0;
}