條條道路通羅馬

#include <vector>  
#include <string>  
#include<iostream>  
#include <algorithm>  
using namespace std;  
  
//條條道路通羅馬  
  
int cityCount = 0;  
int citySrc = 0;  
int cityDst = 0;  
  
vector< vector<int> > PathVec;  
  
int GetCountOfPath(int citySrc , int cityDst , vector<int> exceptCity )  
{  
exceptCity.push_back(cityDst);  
int result = 0;  
//查找cityDst周邊的  
for (int i = 0 ; i< cityCount ; i++)  
{  
if (i == citySrc)  
{  
result += PathVec[i][cityDst];  
}  
else if(i != cityDst && 1 == PathVec[i][cityDst] && exceptCity.end() == find(exceptCity.begin() , exceptCity.end() , i))  
{  
result += GetCountOfPath(citySrc , i , exceptCity);  
}  
}  
return result;  
}  
  
  
  
//字符串轉vec  
  
vector<int> stringToVec(const string& str)  
{  
vector<int> InputVec;  
char *p;  
p = strtok(const_cast<char*>(str.c_str())," ");  
while(p)  
{  
if (' ' == *p)  
{  
p=strtok(NULL," ");  
continue;  
}  
InputVec.push_back(atoi(p));  
p=strtok(NULL," ");  
}  
return InputVec;  
}  
void main()  
{  
  
//輸入第一行 “N A B”(1 < N <= 10; 0 <= A,B <= 9; A != B),N表示有多少個城市,A標識從編號爲A的城市出發,B標識目的城市  
string strInput;  
strInput.length();  
getline(cin , strInput);  
  
vector<int> InputVec = stringToVec(strInput);  
  
  
cityCount = InputVec[0];  
citySrc = InputVec[1];  
cityDst = InputVec[2];  
  
//輸入路徑是否連接  
for (int j = 0 ; j < cityCount ; j++)  
{  
string temp;  
getline(cin , temp);  
PathVec.push_back(stringToVec(temp));  
}  
vector<int> exceptCity;  
int result = GetCountOfPath(citySrc , cityDst , exceptCity);  
cout << result <<endl;  
  
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章