#include <bits/stdc++.h>
#include <windows.h>
#include <conio.h>
using namespace std;
char wasd;
int n,a[1005][1005];
struct node{
int x,y;
}o;
void chushihua(){
o.x=o.y=n-1;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
a[i][j]=i*n+(j+1);
}
}
a[n-1][n-1]=0;
}
void shuchu(){
for(int i=0;i<n;i++){
for(int j=0;j<n;j++)
printf("%4d",a[i][j]);
cout<<"\n";
}
}
void daluan1(){
int wasd2=rand()%(5);
while(wasd2==0) wasd2=rand()%(5);
if(wasd2==1&&o.x>0){
swap(a[o.x][o.y],a[o.x--][o.y]);
}else if(wasd2==2&&o.x<n-1){
swap(a[o.x][o.y],a[o.x++][o.y]);
}else if(wasd2==3&&o.y>0){
swap(a[o.x][o.y],a[o.x][o.y--]);
}else if(wasd2==4&&o.y<n-1){
swap(a[o.x][o.y],a[o.x][o.y++]);
}
}
void daluan(int i){
for(int j=0;j<i;j++){
daluan1();
}
}
void duru(){
wasd=getch();
if((wasd=='w'||wasd=='W')&&o.x>0){
swap(a[o.x][o.y],a[o.x--][o.y]);
}else if((wasd=='s'||wasd=='S')&&o.x<n-1){
swap(a[o.x][o.y],a[o.x++][o.y]);
}else if((wasd=='a'||wasd=='A')&&o.y>0){
swap(a[o.x][o.y],a[o.x][o.y--]);
}else if((wasd=='d'||wasd=='D')&&o.y<n-1){
swap(a[o.x][o.y],a[o.x][o.y++]);
}
}
int main(){
cin>>n;
chushihua();
daluan(1000);
while(true){
system("cls");
shuchu();
duru();
}
return 0;
}
9 个赞
加个玩法说明吧
(有点看不懂)
题目以经加了说明
2 个赞