2017-06-17 91 views
-1

有數字牆建造。 0意味着有一個洞,塊不能坐在孔上。有人擁有一把特殊的槍,可以一次射出所有的數字。
所以我有一個名爲牆的矩陣,必須寫一把槍。我寫了這個程序,但是我有一個問題,我不明白爲什麼會發生。在我的代碼牆壁破壞

#include <iostream> 
#include <cstdio> 

using namespace std; 

int createWall(int &height, int &length, int wall[][ 100 ], int shots) 
{ 
    int i; 
    int j; 
    cin >> height; 
    cin >> length; 
    cin >> shots; 
    for (i = 0; i < height; i++) 
    { 
     for (j = 0; j < length; j++) 
     { 
      cin >> wall[ i ][ j ]; 
     } 
    } 
    return shots; 
} 


void wallNow(int height, int length, int wall[][ 100 ]) 
{ 
    int i; 
    int j; 
    for (i = 0; i < height; i++) 
    { 
     for (j = 0; j < length; j++) 
     { 
      cout << wall[ i ][ j ] << " "; 
     } 
     cout << "\n"; 
    } 
} 

void destroyWall(int height, int length, int wall[][100], int shots) 
{ 
    int i; 
    int j; 
    int k; 
    int x; 
    int aimedBlocks;//number to be "destroyed" 
    //set all aimedBlocks to 0 
    for (x = 0; x < shots; x++) 
    { 
     cin >> aimedBlocks; 
     for (i = 0; i < height; i++) 
     { 
      for (k = 0; k < length; k++) 
      { 
       if (wall[ i ][ k ] == aimedBlocks) 
       { 
        wall[ i ][ k ] = 0; 
       } 
      } 
     } 
    } 

    int counter;//I use this variable because at some point I have a 0 followed only by 0's 
    for (i = 0; i < length; i++) 
    { 
     j = height - 1; 
     counter = 0; 
     //if I find a 0 then I move all elements higher that it one step down 
     while (counter < height) 
     { 
      if (wall[ j ][ i ] == 0) 
      { 
       for (k = j; k > 0; k--) 
       { 
        wall[ k ][ i ] = wall[ k - 1 ][ i ]; 
       } 
       wall[ height - j - 1 ][ i ] = 0; 
      } 
      else 
       j--;//I don't always go up ene step because the "block" droped in place of 0 may be 0 
      counter++; 
     } 
    } 
} 

int main() 
{ 
    int height; 
    int length; 
    int wall[ 100 ][ 100 ]; 
    int shots = 0; 
    shots = createWall(height, length, wall, shots); 
    destroyWall(height, length, wall, shots); 
    wallNow(height, length, wall); 
} 

我真的不明白,爲什麼線wall[ height - j - 1 ][ i ] = 0;在下面的例子正在爲前4列,它不爲最後一個工作。

格式輸入:

height length shots 
wall_0_0 ... wall_0_length 
... ... ... 
wall_height ... wall_height_length 
shot_0 ... shot_shots 

輸入:

4 5 3 
3 5 4 5 1 
2 1 1 5 3 
1 1 5 5 1 
5 5 1 4 3 
1 5 1 

刪除與151匹配所有值。牆仍然必須落入底部。

輸出:

0 0 0 0 0 
0 0 0 0 0 
3 0 0 0 0 
2 0 4 4 3 

預計:

0 0 0 0 0 
0 0 0 0 0 
3 0 0 0 3 
2 0 4 4 3 

請幫我解決這個問題。我找不到它調試代碼。

+1

這聽起來像你需要使用調試器。 –

+0

@JamesRoot我用調試器 –

回答

2

你的算法很奇怪,我不明白你想做什麼。

實現您的目的的一種簡單方法是從牆的左側到右側迭代,然後從底部到頂部迭代。每次獲得0時,您都會在頂部搜索非零值,並在發現它們時交換它們。

例(非常基本可以改善):

for (size_t i = 0; i < length; i++) { // i is for iterate from left(0) to right(length - 1) 
    size_t j = height; // j is for iterate from bot(height - 1) to top(0) 
    while (j-- > 0) { 
    if (wall[j][i] == 0) { 
     size_t k = j; // k is for found a non zero value from j - 1 to the top(0) 
     while (k-- > 0) { 
     if (wall[k][i] != 0) { 
      wall[j][i] = wall[k][i]; 
      wall[k][i] = 0; 
      break; 
     } 
     } 
    } 
    } 
} 

注:

  1. 我用size_t,因爲這是指數的類型。
  2. 我建議您切換爲std::vector並在C++中使用迭代器。
+0

我正在學習這就是爲什麼我寫了奇怪的算法。謝謝!爲什麼'size_t'是索引的類型? –

+0

@Timʘteihttp://en.cppreference.com/w/cpp/types/size_t。請記住,這種類型是無符號的。初學者操作可能很奇怪。但是你應該學會如何使用它。 https://stackoverflow.com/a/22587575/7076153 – Stargateur

+0

我從第二個鏈接瞭解的是不使用無符號循環。 –