Tony041010

數字拼盤2(利用逆數對找到並輸出可解版面)

Jun 17th, 2021 (edited)
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.72 KB | None | 0 0
  1. #include <conio.h>
  2. #include <iostream>
  3. #include <vector>
  4. #include <algorithm>
  5. #include <time.h>
  6. #include <windows.h>
  7. using namespace std;
  8. vector<string> bign{"0","1","2","3","4","5","6","7","8","9"};  //用來輸出的全形數字,因為空白格 □是全形,這樣格子才不會歪掉
  9. const int length = 4; //使用者可以透過更改邊長來玩到不一樣大小的數字拼盤
  10. int map[length*length + 1]={0};  //一開始10格都設為0 ,但是捨棄第0格,只用map[1]~map[9]九宮格
  11. int p;//目前位置
  12. int k;//目前按鍵
  13. int s;//目前移動數
  14.  
  15. void draw(){
  16.     //system("cls");  //cls means clear screen
  17.     cout << "steps:" << s << endl;
  18.     for(int i = 1 ; i <= (length * length) ; i++ ){
  19.         //cout << "de";
  20.         if( map[i] == 0 ){
  21.             cout << "□" << " ";
  22.         }
  23.         else if( map[i] >=1 && map[i] <=9 ){
  24.             cout << bign[ map[i] ] << " ";
  25.         }
  26.         else{
  27.             cout << map[i] << " ";
  28.         }
  29.         if( i % length == 0 ){
  30.             cout << endl;
  31.         }
  32.     }
  33. }
  34.  
  35. void init(){
  36.     int i , j ;
  37.     for( i = 1 ; i <= ((length*length) -1) ; i++ ){
  38.         map[i] = i;
  39.     }
  40.     map[i] = 0;
  41.    
  42.     while(1){
  43.         for( i = 1 ; i <= (length*length -1) ; i ++ ){
  44.             int x = rand()%(length*length -1)+1;
  45.             int y = map[x];
  46.             map[x] = map[i];
  47.             map[i] = y;
  48.         }
  49.        
  50.         //第二題 逆數對之運算
  51.         int a = 0;
  52.         for( i = 1 ; i <= (length*length -2) ; i++ ){
  53.             for( j = i + 1 ; j <= (length*length -1) ; j ++ ){
  54.                 if( map[i] > map[j] ){
  55.                     a++;
  56.                 }
  57.             }
  58.         }
  59.         if( a % 2 == 0 ){
  60.             break; 
  61.         }
  62.         cout << "no solution , try next\n";
  63.         draw();
  64.     }
  65.     p = length*length;
  66.     s = 0;
  67. }
  68.  
  69.  
  70.  
  71. int main(){
  72.     init();
  73.     draw();
  74.     /*while(1){
  75.         if( kbhit() ){
  76.             k = _getch();
  77.             if(k == 'q'){
  78.                 break;
  79.              }
  80.             else if( k == 'e'){
  81.                 if( p < (length*length -length) ){
  82.                     int x = p + length ;
  83.                     int y = map[x];
  84.                     map[x] = map[p];
  85.                     map[p] = y;
  86.                     p = x;
  87.                 }
  88.             }
  89.             else if( k == 'd'){
  90.                 if( p > length ){
  91.                     int x = p - length ;
  92.                     int y = map[x];
  93.                     map[x] = map[p];
  94.                     map[p] = y;
  95.                     p = x;
  96.                 }
  97.             }
  98.             else if( k == 's'){
  99.                 if( p % length != 0 ){
  100.                     int x = p + 1 ;
  101.                     int y = map[x];
  102.                     map[x] = map[p];
  103.                     map[p] = y;
  104.                     p = x;
  105.                 }
  106.             }
  107.             else if( k == 'f'){
  108.                 if( p % length != 1 ){
  109.                     int x = p - 1 ;
  110.                     int y = map[x];
  111.                     map[x] = map[p];
  112.                     map[p] = y;
  113.                     p = x;
  114.                 }
  115.             }
  116.             if( k == 'e' || k == 'd' || k == 's' || k == 'f' ){
  117.                 s++;
  118.             }
  119.             bool finished = true;
  120.             for( int i = 1 ; i <= (length*length -1) ; i++ ){
  121.                 if( map[i] != i ){
  122.                     finished = false;
  123.                     break;
  124.                 }
  125.             }
  126.             if( finished ){
  127.                 cout<<"you win!\n";
  128.                 draw();
  129.                 break;
  130.             }
  131.             draw();
  132.          }
  133.      }*/
  134.     return 0;
  135. }
Add Comment
Please, Sign In to add comment