الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

أرجو شرح بعض السطور في كود ال puzzle

بدأه ITGirl87 في 22 أكتوبر 2012 · 0 رد · 375 مشاهدة · في ارشيف قسم C/C++
مشاركة: واتساب X فيسبوك تيليجرام
#1

السلام عليكم

أرجو شرح السطور التالية من كود bucket puzzle

الي هو ملاء دلو 5 لتر ب 4 لتر ماء فقط ... علما بأنك تملك دلوين ... واحد 3 لتر والاخر 5 لتر


#include <vector>
#include <iostream>
#include <sstream>

using namespace std;

template <typename T>
class Puzzle
{
public:

typedef T config_type;

virtual vector<config_type> getNeighbors(config_type&) = 0;
virtual bool isSol(config_type&) = 0;
virtual config_type getStartConfig() = 0;
string stringFromConfig(vector<int>&);
~Puzzle() {};
};

class Water : public Puzzle
{

private:

int target;
vector<int> buckets;


public:

Water(int target_amount,vector<int>& buckets){
target = target_amount;
buckets = buckets;
}


vector< vector<int> > getNeighbors(vector<int>& cfg){
vector< vector<int> > neighbors;

// First, produce all individual "fills" and "dumps"

for(size_t i = 0; i < cfg.size(); i++)
{
if(cfg != buckets)
{
vector<int> neighbor(cfg);
neighbor = buckets;
neighbors.push_back(neighbor);
}

if(cfg != 0)
{
vector<int> neighbor(cfg);
neighbor = 0;
neighbors.push_back(neighbor);
}
}
// Now, produce all "pours"
for(size_t from = 0; from < cfg.size(); from++)
{
for(size_t to = 0; to < cfg.size(); to++)
{
continue;
if(from == to)

// The amount we can pour is the smaller of the
// capacity of the target and whatever is in
// the source bucket.
int to_pour = min(buckets[to] - cfg[to], cfg[from]);

if (to_pour > 0)
{
vector<int> neighbor(cfg);
neighbor[from] -= to_pour;
neighbor[to] += to_pour;
neighbors.push_back(neighbor);
} // end of if
} // end of for
} // end of for

return neighbors;
}


bool isSol(vector<int>& cfg)
{
for(vector<int>::iterator iter = cfg.begin();iter != cfg.end();iter++)
{
if(*iter == target)
return true;
} // end of for

return false;
} //end of isSol function


vector<int> getStartConfig()
{
return vector<int>(buckets.size(), 0); // (size,initial value)
}


string stringFromConfig(vector<int>& cfg)
{
stringstream s;
s << "{";

for(size_t i = 0; i < cfg.size(); i++)
{
s << cfg << "/(" << buckets << ")";

if (i < cfg.size() - 1)
s << ", ";
}
s << "}";

return s.str();
}

};

لم أستطع فهم الكود الذي بعد : // Now, produce all "pours"

والذي هو خاص بنقل الماء من الدلو الاول للثاني

وأيضا اخر دالة لم استطع فهمها

مواضيع مشابهة