Example #1
0
	int minDis(string & orgWord,string & desWord ,int step)
	{
		int ret;

		if(record.find(desWord) != record.end() && record[desWord].find(orgWord) != record[desWord].end())
			return record[desWord][orgWord];

		if(orgWord.size() == 0 && desWord.size() == 0)
		{
			if(minSteps > step)
				minSteps = step;
		}
		if(orgWord.size() == 0)
		{
			ret = desWord.size();
			FillRecord(orgWord,desWord,ret);
			return ret	;
		}
		if(desWord.size() == 0)
		{
			ret = orgWord.size();
			FillRecord(orgWord,desWord,ret);
			return ret;
		}

		if( step > minSteps)
			return 1;

		string orgWordSub = orgWord.size()==1?string(""): orgWord.substr(1);
		string desWordSub = desWord.size()==1?string(""): desWord.substr(1);

		if(orgWord[0] == desWord[0])
		{
			int doNoneSetps = minDis( orgWordSub,desWordSub,step); 
			ret =doNoneSetps;
			FillRecord(orgWord,desWord,ret);
			return ret;
		}
		else
		{
			int addSteps = minDis(orgWord,desWordSub,step+1) + 1;
			int delSteps = minDis(orgWordSub,desWord,step+1) + 1;
			int replaceSteps = minDis( orgWordSub,desWordSub,step+1) + 1; 
			ret = min(min(delSteps,addSteps),replaceSteps);
			FillRecord(orgWord,desWord,ret);		
			return ret;
		}
	}
Example #2
0
 int bfs(int start, int end) {
   vector<int> minDis(linkedList.size(), INT32_MAX);
   vector<bool> visited(linkedList.size(), false);
   queue<int> q;
   
   q.push(start);
   visited[start] = true;
   minDis[start] = 0;
   
   while(!q.empty()) {
     int node = q.front();
     q.pop();
     
     for (int i = 0 ; i < linkedList[node].size(); i ++) {
       int nb = linkedList[node][i];
       
       if (!visited[nb]) {
         q.push(nb);
         visited[nb] = true;
         minDis[nb] = minDis[node] + 1;
       }
       // This line is important: if minDis[nb] < minDis[node] + 1, it would be wrong to push node back
       if (minDis[node] + 1 == minDis[nb]) previous[nb].push_back(node);
     }
   }
   
   return minDis[end];
 }
void MST::fill_greedy(vector<Block> &ans,bool *used)
{
	int CC[X];
	int a,bb,a1,b1;
	minDis top;
	int ind;
	double matemp,ma;

	for(int i=0;i<X;i++)
		CC[i] = 0;

	for(int i=0;i<X;i++)
	{
		a=i/N,bb=i%N;

		for(int j=0;j<4;j++)
		{
			a1=a+xx[j],b1=bb+yy[j];
			if(a1<0||a1>=N) continue;
			if(b1<0||b1>=N) continue;
			if(ans[a1*N+b1].idx!=-1)
				CC[i]++;
		}
	}
	priority_queue<minDis> Q;
	for(int i=0;i<X;i++)
	  if(!used[ans[i].idx])
		Q.push(minDis(CC[i],i));

	while(Q.size())
	{
		top = Q.top();
		Q.pop();
		if(ans[top.id].idx!=-1) 
			continue;
		ind=-1;
		ma = 0;
		for(int i=0;i<X;i++)
		if(!used[i])
		{
			matemp=pieces->getWeight(ans,top.id,pieces->block[i]);
			if(ind==-1||ma>matemp) ind=i,ma=matemp;
		}

		a=top.id/N,bb=top.id%N;
		for(int j=0;j<4;j++)
		{
			a1=a+xx[j],b1=bb+yy[j];
			if(a1<0||a1>=N) continue;
			if(b1<0||b1>=N) continue;
			if(ans[a1*N+b1].idx==-1)
			{
				CC[a1*N+b1]++;
				Q.push(minDis(CC[a1*N+b1],a1*N+b1)); 
			}
		}

		ans[top.id] = pieces->block[ind]; 
		used[ind] =1;
	}
}
Example #4
0
	int minDistance(string word1, string word2) {
		// Start typing your C/C++ solution below
		// DO NOT write int main() function
		minSteps = -1;
		return minDis(word1,word2,0);
	}