2018 CodeM初赛A轮 .编程1 (A题) (第一道题)

     这道题,题目叙述的很啰嗦,不过做法很简单,就是建一个表,其实最短距离就是求曼哈顿距离。写表的时候繁琐,不过也没办法,谁叫我们是程序员呢。

代码如下:

//

//  main.cpp

//  codeM初赛1

//

//  Created by Mr Gao on 2018/6/9.

//  Copyright © 2018年 Mr Gao. All rights reserved.

//


#include <iostream>

#include <vector>

using namespace std;

struct alpthbet{

    char ch;

    int x, y;

    alpthbet(){};

    alpthbet(char a, int a1, int a2): ch(a),x(a1),y(a2){}

};

int main(int argc, const char * argv[]) {

    // insert code here...

    int T;

    cin>>T;

    if(T == 0)

        cout<<0<<endl;

    vector<string> data(T);

    for(int i = 0; i < T; i++){

        string str;

        cin>>str;

        data[i] = str;

    }

    vector<alpthbet> steet(26);

    steet[0] = alpthbet ('A', 0, 1);

    steet[1] = alpthbet('B', 0, 1);

    steet[2] =  alpthbet('C', 0, 1);

    steet[3] =  alpthbet('D', 0, 2);

    steet[4] =  alpthbet('E', 0, 2);

    steet[5] =  alpthbet('F', 0, 2);

    steet[6] =  alpthbet('G', 1, 0);

    steet[7] =  alpthbet('H', 1, 0);

    steet[8] =  alpthbet('I', 1, 0);

    steet[9] = alpthbet('J', 1, 1);

    steet[10] = alpthbet('K', 1, 1);

    steet[11] = alpthbet('L', 1, 1);

    steet[12] =  alpthbet ('M', 1, 2);

    steet[13] =  alpthbet ('N', 1, 2);

    steet[14] =  alpthbet ('O', 1, 2);

    steet[15] =  alpthbet ('P', 2, 0);

    steet[16] =  alpthbet ('Q', 2, 0);

    steet[17] =  alpthbet ('R', 2, 0);

    steet[18] =  alpthbet ('S', 2, 0);

    steet[19] =  alpthbet ('T', 2, 1);

    steet[20] =  alpthbet ('U', 2, 1);

    steet[21] =  alpthbet ('V', 2, 1);

    steet[22] =  alpthbet ('W', 2, 2);

    steet[23] =  alpthbet ('X', 2, 2);

    steet[24] =  alpthbet ('Y', 2, 2);

    steet[25] =  alpthbet ('Z', 2, 2);

    for(int i = 0; i < T; i++){

        string tmp = data[i];

        int last_x = 0, last_y = 0, ans = 0;

        for(int j = 0; j < tmp.size(); j++){

            int tx = steet[tmp[j] - 'A'].x, ty = steet[tmp[j] - 'A'].y;

            ans += abs(tx - last_x) + abs(ty - last_y);

            last_x = tx; last_y = ty;

        }

        cout<<ans<<endl;

    }

    return 0;

}



猜你喜欢

转载自blog.csdn.net/torch_man/article/details/80637764
今日推荐