数字华容道

xhg 2022-09-09 23:10:03 1

#include <bits/stdc++.h> #include <conio.h> #include <windows.h> using namespace std; int n_puzzle[3][3]={1,3,8,7,0,2,5,6,4}; int m_puzzle[3][3]={1,2,3,4,5,6,7,8,0}; void outarr(){ for(int i=0;i<3;++i){ for(int j=0;j<3;++j){ if(n_puzzle[i][j]!=0){ cout<<n_puzzle[i][j]<<" "; }else{ cout<<" "; } } cout<<"\n"; } } void change(int* a,int* b){ int temp=*a; *a=*b; *b=temp; } int main(){ outarr(); int x=1,y=1,count=0; char choose; while(true){ cout<<"请输入移动方向(wsad) : "<<endl; choose=getch(); count++; if(choose=='w'&&x>=0&&x<2){ change(&n_puzzle[x][y],&n_puzzle[x+1][y]); ++x; }else if(choose=='s'&&x>0&&x<=2){ change(&n_puzzle[x][y],&n_puzzle[x-1][y]); --x; }else if(choose=='a'&&y>=0&&y<2){ change(&n_puzzle[x][y],&n_puzzle[x][y+1]); ++y; }else if(choose=='d'&&y>0&&y<=2){ change(&n_puzzle[x][y],&n_puzzle[x][y-1]); --y; } system("cls"); outarr(); int flag=1; for(int i=0;i<3;++i){ for(int j=0;j<3;++j){ if(n_puzzle[i][j]!=m_puzzle[i][j]){ flag=0; break; } } } if(flag==1){ string steps="你成功了,使用步数为 :"; char sum[100]; itoa(count,sum,10); steps+=sum; MessageBox(0,steps.c_str(),"提示",MB_OK); return 0; } } return 0; }

{{ vote && vote.total.up }}