题目蛮简单,不过对string的操作不熟悉,读int搞了一阵子,一开始想split,后来发现没有,只能一个个自己读了。
size_t find ( const string& str, size_t pos = 0 ) const;
size_t find ( const char* s, size_t pos, size_t n ) const;
size_t find ( const char* s, size_t pos = 0 ) const;
size_t find ( char c, size_t pos = 0 ) const;
Find content in string
Searches the string for the content specified in either str, s or c, and returns the position of the first occurrence in the string.When pos is specified the search only includes characters on or after position pos, ignoring any possible occurrences in previous locations.
Notice that unlike member find_first_of, whenever more than one character is being searched for, it is not enough that only one of these characters match, but the entire sequence of characters to find must be matched.
#include <iostream>
#include <vector>
using namespace std;
class ThreeTeleports
{
int ABS(int m)
{
if(m>0) return m;
else return -m;
}
int Time(int x, int y, int x2, int y2)
{
return ABS(x-x2)+ABS(y-y2);
}
int T[3][4];
/*void Initial(vector <string> teleports)
{
for(int i=0;i<3;i++)
{
sscanf(teleports[i].c_str(),"%d%d%d%d",&T[i][0],&T[i][1],&T[i][2],&T[i][3]);
}
} */
void Initial(vector <string> teleports)
{
for(int i=0;i<3;i++)
{
int pos=0;
int tmp=teleports[i].find(" ",pos);
T[i][0]=atoi(teleports[i].substr(pos,tmp).c_str());
pos=tmp+1;
tmp=teleports[i].find(" ",pos);
T[i][1]=atoi(teleports[i].substr(pos,tmp).c_str());
pos=tmp+1;
tmp=teleports[i].find(" ",pos);
T[i][2]=atoi(teleports[i].substr(pos,tmp).c_str());
pos=tmp+1;
tmp=teleports[i].find(" ",pos);
T[i][3]=atoi(teleports[i].substr(pos,tmp).c_str());
pos=tmp+1;
}
}
public:
int shortestDistance(int xMe, int yMe, int xHome, int yHome, vector <string> teleports)
{
Initial(teleports);
unsigned int best=2000000009;
int flag[3];
for(flag[0]=0;flag[0]<3;flag[0]++)
{
for(flag[1]=0;flag[1]<3;flag[1]++)
{
for(flag[2]=0;flag[2]<3;flag[2]++)
{
unsigned int time=0;
int x=xMe;
int y=yMe;
for(int i=0;i<3;i++)
{
if(flag[i]==1)
{
time+=Time(x,y,T[i][0],T[i][1]);
time+=10;
x=T[i][2];
y=T[i][3];
}
else if(flag[i]==2)
{
time+=Time(x,y,T[i][2],T[i][3]);
time+=10;
x=T[i][0];
y=T[i][1];
}
}
time+=Time(x,y,xHome,yHome);
if(time<best) best=time;
}
}
}
return best;
}
};