动物园查找最短路径题

题目从别的帖子看来的,自己写了个算法,就记在自己的博客里,欢迎大家交流




#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <algorithm>
using namespace std;


void FindMin(string testStr, vector<vector<int>> distanceMatrix, int query, int &min)
{
<span style="white-space:pre">	</span>int last=0;
<span style="white-space:pre">	</span>int cur=0;
<span style="white-space:pre">	</span>int dis=0;
<span style="white-space:pre">	</span>int j=0;
<span style="white-space:pre">	</span>for (;j<testStr.size();j++)
<span style="white-space:pre">	</span>{
<span style="white-space:pre">		</span>cur=(int)testStr[j]-48;;
<span style="white-space:pre">		</span>dis+=distanceMatrix[last][cur];
<span style="white-space:pre">		</span>if (cur==query)<span style="white-space:pre">	</span>
<span style="white-space:pre">			</span>break;
<span style="white-space:pre">		</span>last=cur;
<span style="white-space:pre">	</span>}
<span style="white-space:pre">	</span>if (dis<min)
<span style="white-space:pre">	</span>{
<span style="white-space:pre">		</span>min=dis;
<span style="white-space:pre">	</span>}
}


void ReadInputFromConsole( int &totalNum, int &query, vector<vector<int>> &distanceMatrix )
{
<span style="white-space:pre">	</span>cin>>totalNum>>query;<span style="white-space:pre">	</span>
<span style="white-space:pre">	</span>for (int i=0;i<totalNum;i++)
<span style="white-space:pre">	</span>{
<span style="white-space:pre">		</span>vector<int> oneLineInDistanceMatrix;
<span style="white-space:pre">		</span>int tempInput;
<span style="white-space:pre">		</span>for (int j=0;j<totalNum;j++)
<span style="white-space:pre">		</span>{
<span style="white-space:pre">			</span>cin>>tempInput;
<span style="white-space:pre">			</span>oneLineInDistanceMatrix.push_back(tempInput);
<span style="white-space:pre">		</span>}
<span style="white-space:pre">		</span>distanceMatrix.push_back(oneLineInDistanceMatrix);
<span style="white-space:pre">	</span>}
}


int _tmain(int argc, _TCHAR* argv[])
{
<span style="white-space:pre">	</span>int totalNum=0,query=0;
<span style="white-space:pre">	</span>vector<vector<int>> distanceMatrix;
<span style="white-space:pre">	</span>ReadInputFromConsole(totalNum, query, distanceMatrix);
<span style="white-space:pre">	</span>
<span style="white-space:pre">	</span>stringstream aa;
<span style="white-space:pre">	</span>for (int i=1;i<totalNum;i++)
<span style="white-space:pre">	</span>{
<span style="white-space:pre">		</span>aa<<i;
<span style="white-space:pre">	</span>}
<span style="white-space:pre">	</span>string testStr=aa.str();
<span style="white-space:pre">	</span>int min=100000;
<span style="white-space:pre">	</span>do 
<span style="white-space:pre">	</span>{
<span style="white-space:pre">		</span>FindMin(testStr, distanceMatrix, query, min);
<span style="white-space:pre">	</span>} 
<span style="white-space:pre">	</span>while (next_permutation(testStr.begin(),testStr.end()));
 
<span style="white-space:pre">	</span>cout<<min<<endl;
<span style="white-space:pre">	</span>return 0;
}
 

 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值