2015-10-15 72 views
0

給用戶一個隨機生成的4x4二維數組,其中一個元素肯定是0.考慮到0是空位置,用戶必須重複交換剩餘的15個元素,直到他們以升序獲得數組,0作爲最後一個元素。 在這一點上,他們被允許與0交換任何元素。 但是,如何修改此代碼以確保只能交換那些與它相鄰的元素(上方,下方或旁邊) ?與將二維數組按升序排序相關的謎題

#include<iostream> 
#include<stdlib.h> 
#include<time.h> 

using namespace std; 

int check_asc(int a[][4]) 
{ 
int i, j, previous = a[0][0]; 

for (i = 0; i < 4; i++) 
{ 
    for (j = 0; j < 4; j++) 
    { 
     if(i == 3 && j == 3) 
     { 
      if (a[i][j] == 0) 
       return 1; 
     } 
     else if (a[i][j] < previous) 
     { 
      return 0; 
     } 
     previous = a[i][j]; 
    } 
} 
return 1; 
} 

void swap(int a[][4], int &xpos, int &ypos) 
{ 
int arr, temp; 

cout << "\n\nEnter number to be swapped with 0: "; 
cin >> arr; 

for (int i = 0; i < 4; i++) 
{ 
    for (int j = 0; j < 4; j++) 
    { 
     if (a[i][j] == arr) 
     { 
      temp = a[xpos][ypos]; 
      a[xpos][ypos] = a[i][j]; 
      a[i][j] = temp; 
      xpos = i; 
      ypos = j; 
      return; 
     } 
    } 
} 
} 

int check_rep(int a[][4], int assign) 
{ 
for (int i = 0; i < 4; i++) 
{ 
    for (int j = 0; j < 4; j++) 
    { 
     if (assign == a[i][j]) 
     return 0; 
    } 
} 
return 1; 
} 

void main() 
{ 
int a[4][4], assign, xpos = 0, ypos = 0, asc_result, rep_result; 

srand((unsigned)time(NULL)); 

for (int i = 0; i < 4; i++) 
    for (int j = 0; j < 4; j++) 
    { 
     if (i == 0 && j == 0) 
      a[i][j] = 0; 
     else 
     { 
      do { 
       assign = rand() % 50; 
       rep_result = check_rep(a, assign); 
      } while (rep_result == 0); 
      a[i][j] = assign; 
     } 
    } 

    cout << "\n\nArrange the 4x4 matrix into ascending order. (Consider 0 as a blank space)" << endl; 

    for (int i = 0; i < 4; i++) 
    { 
    cout << endl; 
    for (int j = 0; j < 4; j++) 
     cout << a[i][j] << '\t'; 
    } 

    do { 
     swap(a, xpos, ypos); 
     system("cls"); 

     for (int i = 0; i < 4; i++) 
     { 
     cout << endl; 
     for (int j = 0; j < 4; j++) 
      cout << a[i][j] << '\t'; 
     } 
     asc_result = check_asc(a); 

    } while (asc_result == 0); 

    cout << "\n\tYou win"<<endl; 

    system("pause"); 
} 
+0

它類似於十五謎題,只是數字不需要是1..15(和重新可能是重複的)。 – CiaPan

+0

是的,這裏的數字可以是任何東西,直到50,沒有重複。 – Abhay

+0

沒錯,沒有重複。順便說一句,你知道你的'check_rep'函數測試整個數組的重複部分,包括尚未初始化的部分? – CiaPan

回答

1

簡單,只需用一段代碼,將檢查是否元素的位置擴展您的交換功能被交換相鄰的0位置:

void swap(int a[][4], int &xpos, int &ypos) 
    { 
    ... 
     if (a[i][j] == arr && 
       ((i == xpos && (j == ypos - 1 || j == ypos + 1)) || 
       (j == ypos && (i == xpos - 1 || i == xpos + 1)))) 
     { 
      temp = a[xpos][ypos]; 
      a[xpos][ypos] = a[i][j]; 
      a[i][j] = temp; 
      xpos = i; 
      ypos = j; 
      return; 
     } 

的改進將是分開所述檢驗條件,並通知用戶的情況下,當該元件處於不相鄰0

0

粗糙算法

1)創建一個函數查找位置,它將返回其具有的x,y整數字段的結構點時,會發現任何片的基礎上,片的x,y位置值,即允許輸入0,如果它位於左上角(0,0),將返回一個點(0,0)

2)創建一個函數,其中包含2點'0'的位置以及我們希望交換的部分的位置讓它稱爲S,如果Sx = 0.x和0.y-1 = Sy或Sy - 0.y + 1,那麼你知道那件作品是直接高於或低於0,現在當然,你不必爲邊界添加一些條件,因爲我們不檢查網格外部。如果S片段位於上方/下方/旁邊,則返回int 1;否則返回0。

3)如果返回1你允許這樣做翻轉,如果返回0找到另一塊