C++/JAVA版无脑模拟shipbattle

最近帮别人水了到题。。于是我写了一大坨。。
论除C++外掌握好第二语言的重要性。。。学python好了。。。T-T

  • C++(我写的)
#include <iostream>
#include <string>
#include <boost/algorithm/string.hpp>
#include <boost/lexical_cast.hpp>
#include <vector>
#include <algorithm>
#include <sstream>
using namespace std;

struct ShipLocation {
    int l_x;
    int l_y;
    int r_x;
    int r_y;
};

struct HitLocation {
    int x;
    int y;
};

bool parser_s(string S, vector<ShipLocation>& ship_locations) {
    vector<string> ships;
    boost::split(ships, S, boost::is_any_of(","));
    uint32_t ships_num = ships.size();
    // if (ships_num > 4) 
    vector<string> loc;
    for (int i = 0; i < ships_num; ++i) {
        cout << "ship" << i << " :" << ships[i] << endl;
        loc.clear();
        boost::split(loc, ships[i], boost::is_any_of(" "));
        ShipLocation l;
        cout << loc[0][0] << " " << loc[0][1] << endl;
        try {
            l.l_x = boost::lexical_cast<int>(loc[0][0]) - 1;
            l.l_y = (int)loc[0][1] - 65;
            l.r_x = boost::lexical_cast<int>(loc[1][0]) - 1;
            l.r_y = (int)(loc[1][1]) - 65;
        } catch (boost::bad_lexical_cast &e) {
            cerr << e.what() << endl;
        }
        cout << "l_x: " << l.l_x << "; l_y: " << l.l_y << endl;
        cout << "r_x: " << l.r_x << "; r_y: " << l.r_y << endl;
        ship_locations.push_back(l);
    }
    return true;
}

void test_parser_s() {
    string S = "1B 2C,2D 4D";
    vector<ShipLocation> ship_locations;
    parser_s(S, ship_locations);
}

bool parser_t(string T, vector<HitLocation>& hit_locations) {
    vector<string> hits;
    boost::split(hits, T, boost::is_any_of(" "));
    uint32_t hits_num = hits.size();
    for (int i = 0; i < hits_num; ++i) {
        cout << "hit" << i << " :" << hits[i] << endl;
        HitLocation l;
        try {
            l.x = boost::lexical_cast<int>(hits[i][0]) - 1;
            l.y = (int)hits[i][1] - 65;
        } catch (boost::bad_lexical_cast &e) {
            cerr << e.what() << endl;
        }
        cout << "x: " << l.x << "; y: " << l.y << endl;
        hit_locations.push_back(l);
    }
    return true;
}

void test_parser_t() {
    string T = "2B 2D 3D 4D 4A";
    vector<HitLocation> hit_locations;
    parser_t(T, hit_locations);
}

string solution(int N, string S, string T) {
    vector<int> res(2, 0);

    vector<int> arr(N, 0);
    vector<vector<int> > map(N, arr);

    vector<HitLocation> hit_locations;
    parser_t(T, hit_locations);

    uint32_t hits_num = hit_locations.size();
    for (int i = 0; i < hits_num; ++i) {
        map[hit_locations[i].x][hit_locations[i].y] = 1;
    }

    for (int i = 0; i < N; ++i) {
        for (int j = 0; j < N; ++j) {
            cout << map[i][j] << " ";
        }
        cout << endl;
    }

    vector<ShipLocation> ships;
    parser_s(S, ships);

    uint32_t ships_num = ships.size();
    for (int i = 0; i < ships_num; ++i) {
        int x_begin = min(ships[i].l_x, ships[i].r_x);
        int x_end = max(ships[i].l_x, ships[i].r_x);
        int y_begin = min(ships[i].l_y, ships[i].r_y);
        int y_end = max(ships[i].l_y, ships[i].r_y);
        int total_blocks = (x_end - x_begin + 1) * (y_end - y_begin + 1);
        cout << "x_begin:" << x_begin << " x_end:" << x_end << " y_begin: " << y_begin << " y_end:" << y_end << " total_blocks:" << total_blocks << endl;
        int blocks_num = 0;
        for (int ii = x_begin; ii <= x_end; ++ii) {
            for(int jj = y_begin; jj <= y_end; ++jj) {
                if (map[ii][jj] == 1) {
                    ++blocks_num;
                }
            }
        }

        cout << "blocks_num: " << blocks_num << endl;
        if (blocks_num == 0) {
            continue;
        } else if (blocks_num >= total_blocks) {
            ++res[0]; // 沉船数
        } else {
            ++res[1]; // 撞了数
        }
    }

    stringstream ss;
    ss << res[0];
    string result;
    result.append(ss.str());
    result.append(",");
    ss.str("");
    ss.clear();
    ss << res[1];
    result.append(ss.str());
    cout << result << endl;
    return result;
}

void test_solution() {
    string S = "1B 2C,2D 4D";
    string T = "2B 2D 3D 4D 4A";
    int N = 4;

    solution(N, S, T);
}

void test_solution1() {
    string S = "1A 1B,2C 2C";
    string T = "1B";
    int N = 3;

    solution(N, S, T);
}


int main() {
    //test_parser_s();
    //cout << "========" << endl;
    //test_parser_t();
    test_solution();
    test_solution1();

    return 0;
}
  • JAVA(别人写的)
private int hitStatus(String[] ship, String T){
    //解析船的边界
    int upper_bound = ship[0].charAt(ship[0].length()-1)-'A';
    int lower_bound = ship[1].charAt(ship[1].length()-1)-'A';
    int left_bound = Integer.parseInt(ship[0].substring(0, ship[0].length()-1));
    int right_bound = Integer.parseInt(ship[1].substring(0, ship[1].length()-1));
    int cell_cnt = (right_bound-left_bound+1)*(lower_bound-upper_bound+1);
    //解析射击位置
    int hit_cnt = 0;
    String[] hits = T.split(" ");
    for(int i=0; i<hits.length; ++i){
        int y = hits[i].charAt(hits[i].length()-1)-'A';
        int x = Integer.parseInt(hits[i].substring(0, hits[i].length()-1));
        if(x>=left_bound && x<=right_bound && y>=upper_bound && y<=lower_bound)
            hit_cnt++;
    }
    //Out put
    if(hit_cnt>=cell_cnt)
        return 0; 
    else if(hit_cnt>0)
        return 1; //Hit
    else 
        return 2; //Not hit
}

public String solution(int N, String S, String T) {
    int hit_cnt = 0;
    int sunk_cnt = 0;

    String[] ships = S.split(",");
    int num_ship = ships.length;
    for(int i=1; i<=num_ship; ++i){
        String[] ship = ships[i-1].split(" ");
        int tmp = hitStatus(ship, T);
        if(tmp==0)
            sunk_cnt++;
        else if(tmp==1)
            hit_cnt++;
    }
    return Integer.toString(sunk_cnt)+","+Integer.toString(hit_cnt);
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值