有請大家幫忙看一下可能是哪裡有問題囉,感謝^^
#include <iostream>
using namespace std;
int main()
{
int n; //積木數目
int a, b; //積木編號
int ap[2], bp[2]; //ab積木的位置
int now; //現在要移動的積木編號
string s1, s2; //動作指令
while ( cin >> n ) {
int block[n][n];//積木狀態
int top[n]; //積木stack的頂端
/* 初始化狀態 */
for ( int i = 0; i < n; i++ ) {
top[i] = 0;
block[i][0] = i;
for ( int j = 1; j < n; j++ )
block[i][j] = 25; //25就是沒有東西的意思XD
}
/* 讀入指令 */
while ( cin >> s1 ) {
if ( s1 == "quit" )
break;
cin >> a >> s2 >> b;
if ( a == b )
continue;
/* 尋找ab積木所在位置 */
for ( int i = 0; i < n; i++ )
for ( int j = 0; j < n; j++ ) {
if ( block[i][j] == a ) {
ap[0] = i;
ap[1] = j;
}
if ( block[i][j] == b ) {
bp[0] = i;
bp[1] = j;
}
}
if ( ap[0] == bp[0] ) //ab在同一堆積木
continue;
if ( s1 == "move" ) {
/* 把a積木上面的先搬回去 */
for ( int i = top[ ap[0] ]; i > ap[1]; i-- ) {
now = block[ ap[0] ][i];
block[ now ][0] = now;
top[now] = 0;
top[ ap[0] ]--;
}
if ( s2 == "onto" ) {
/* 把b積木上面的先搬回去 */
for ( int i = top [ bp[0] ]; i > bp[1]; i-- ) {
now = block[ bp[0] ][i];
block[ now ][0] = now;
top[now] = 0;
top[ bp[0] ]--;
}
}
/* 把a搬到b所在的那堆積木之上 */
now = block[ ap[0] ][ ap[1] ];
block[ ap[0] ][ ap[1] ] = 25;
top[ ap[0] ]--;
block[ bp[0] ][ ++top[ bp[0] ] ] = now;
} else if ( s1 == "pile" ) {
if ( s2 == "onto" ) {
/* 把b積木上面的先搬回去 */
for ( int i = top [ bp[0] ]; i > bp[1]; i-- ) {
now = block[ bp[0] ][i];
block[ now ][0] = now;
top[now] = 0;
top[ bp[0] ]--;
}
}
/* 把a本身和其上的積木搬到b所在的那堆積木之上 */
for ( int i = ap[1]; i <= top[ ap[0] ]; i++ ) {
block[ bp[0] ][ ++top[ bp[0] ] ] = block[ ap[0] ][i];
block[ ap[0] ][i] = 25;
}
top[ ap[0] ] = ap[1] - 1;
}
/* debug - show now
//cout << ap[0] << "," << ap[1] << "," << bp[0] << "," << bp[1] << endl;
for ( int i = 0; i < n; i++ ) {
cout << i << ":";
for ( int j = 0; j <= top[i]; j++ )
cout << " " << block[i][j];
cout << endl;
}
*/
}
for ( int i = 0; i < n; i++ ) {
cout << i << ":";
for ( int j = 0; j <= top[i]; j++ )
cout << " " << block[i][j];
cout << endl;
}
}
return 0;
}
如果未來您發現問題所在,期望您可以把這個沒有考慮到的測資回報給我們 :)
這樣可以讓這裡的測資更完整,判定的結果也會更接近原站。 感謝~~