SRM 519DIV2 600 ThreeTeleports (如何从string读入多个int)

题目蛮简单,不过对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;  
    }  
};  


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值