[問題] 使用全域變數的權衡
問題(Question):
在寫acm題目的時候,常常遇到好像用全域變數會比較容易處理的情況?
以101 The block problem來說,我的寫法是用一個二維的vector來當作方塊,
一些動作則寫在function中。
我的問題是,如果不用全域變數,function的參數就都要加上這個vector。
但是大家又說盡量不要用全域變數,哪麼有什麼其它的寫法嗎?
謝謝
程式碼(Code):(請善用置底文網頁, 記得排版)
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <stack>
using namespace std;
void reset_desk(vector<vector<int> > &desk, int n) {
desk.clear();
desk.resize(n);
for (int i=0; i<n; i++)
desk[i].push_back(i);
}
int find_block(const vector<vector<int> > &desk, const int block ) {
for (int i = 0; i < desk.size(); ++i) {
vector<int>::const_iterator it = find(desk[i].begin(), desk[i].end(), block);
if (it != desk[i].end()) {
return i;
}
}
}
void print_desk(const vector<vector<int> > &desk) {
for (int i = 0; i < desk.size(); ++i) {
cout << i << ":";
for (vector<int>::const_iterator it=desk[i].begin();
it != desk[i].end();
++it) {
cout <<" " << *it;
}
cout << endl;
}
}
void clean_top(vector<vector<int> > &desk, const int block) {
int temp;
int pos = find_block(desk, block);
while (desk[pos].back() != block) {
temp = desk[pos].back();
desk[temp].push_back(temp);
desk[pos].pop_back();
}
}
void move(vector<vector<int> > &desk, const int from_block, const int to_block) {
stack<int> temp;
int from = find_block(desk, from_block);
while (desk[from].back() != from_block) {
temp.push( desk[from].back() );
desk[from].pop_back();
}
temp.push(desk[from].back());
desk[from].pop_back();
int to = find_block(desk, to_block);
while( !temp.empty() ) {
desk[to].push_back( temp.top() );
temp.pop();
}
//print_desk(desk);
//cout << endl;
}
void execute (vector<vector<int> > &desk,
const string cmd1, const int a, const string cmd2, const int b) {
//cout << cmd1 << " " << a << " " << cmd2 << " " << b << endl;
if (cmd1 == "move") clean_top(desk, a);
if (cmd2 == "onto") clean_top(desk, b);
move (desk, a, b);
}
bool invalid_command(const vector<vector<int> > desk, const int a, const int b) {
int pos_a, pos_b;
pos_a = find_block( desk, a );
pos_b = find_block( desk, b );
return (a == b || pos_a == pos_b);
}
int main() {
int n, a, b;
int pos_a, pos_b; //position for a and b
string cmd1, cmd2;
vector< vector<int> > desk;
//----------testing------------
freopen ("101.in", "r", stdin);
freopen ("101.out", "w", stdout);
while (cin >> n) {
reset_desk(desk, n);
while (1) {
cin >> cmd1;
if (cmd1 == "quit") break;
cin >> a >> cmd2 >> b;
if (invalid_command(desk, a, b)) continue;
execute(desk, cmd1, a, cmd2, b);
}
print_desk(desk);
}
return 0;
}
補充說明(Supplement):
我是用C++寫的~~
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 114.32.170.48
※ 編輯: howardxu 來自: 114.32.170.48 (03/25 08:42)
推
03/25 09:45, , 1F
03/25 09:45, 1F
推
03/25 12:49, , 2F
03/25 12:49, 2F
→
03/25 12:50, , 3F
03/25 12:50, 3F
推
03/25 13:15, , 4F
03/25 13:15, 4F
→
03/25 13:26, , 5F
03/25 13:26, 5F
→
03/25 13:58, , 6F
03/25 13:58, 6F
→
03/25 13:59, , 7F
03/25 13:59, 7F
→
03/25 14:07, , 8F
03/25 14:07, 8F
→
03/25 14:07, , 9F
03/25 14:07, 9F
→
03/25 14:08, , 10F
03/25 14:08, 10F
推
03/25 17:04, , 11F
03/25 17:04, 11F
→
03/25 17:09, , 12F
03/25 17:09, 12F
→
03/25 17:09, , 13F
03/25 17:09, 13F
→
03/25 17:41, , 14F
03/25 17:41, 14F
→
03/25 18:27, , 15F
03/25 18:27, 15F
→
03/25 18:28, , 16F
03/25 18:28, 16F
→
03/25 23:30, , 17F
03/25 23:30, 17F
→
03/25 23:33, , 18F
03/25 23:33, 18F
→
03/25 23:34, , 19F
03/25 23:34, 19F
→
03/25 23:35, , 20F
03/25 23:35, 20F
→
03/25 23:36, , 21F
03/25 23:36, 21F
→
03/25 23:36, , 22F
03/25 23:36, 22F
→
03/25 23:36, , 23F
03/25 23:36, 23F
→
03/25 23:44, , 24F
03/25 23:44, 24F
→
03/25 23:45, , 25F
03/25 23:45, 25F
→
03/25 23:45, , 26F
03/25 23:45, 26F
→
03/25 23:46, , 27F
03/25 23:46, 27F
→
03/25 23:55, , 28F
03/25 23:55, 28F
推
03/26 00:27, , 29F
03/26 00:27, 29F
→
03/26 00:28, , 30F
03/26 00:28, 30F
→
03/26 00:29, , 31F
03/26 00:29, 31F
→
03/26 00:31, , 32F
03/26 00:31, 32F
推
03/26 07:51, , 33F
03/26 07:51, 33F
→
03/26 19:42, , 34F
03/26 19:42, 34F