#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;
}
转载于:https://blog.51cto.com/gzmie/1851661