【codingame】There is no Spoon - Episode 1

在这里插入图片描述

说明

  1. 题目来自:https://www.codingame.com/ide/puzzle/there-is-no-spoon-episode-1

生词

  • diagonal 美/daɪˈæɡənl/ 对角线 — 在《哈利波特》中有对角巷(Diagon Alley)

  • shuriken 手里剑 – 在《英雄联盟》中,凯南会扔闪电手里剑: lightning shuriken – shuruiken

API

std::to_string()

C++ 11中,把数字类型转换为std::string类型

+

字符串拼接

代码

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

class Node {
    public:
    int x, y;
    string getOutput() {
        return to_string(x) + " " + to_string(y) + " ";
    }
};

/**
 * Don't let the machines win. You are humanity's last hope...
 **/

int main()
{
    int width; // the number of cells on the X axis
    cin >> width; cin.ignore();
    int height; // the number of cells on the Y axis
    cin >> height; cin.ignore();

    char nodes[width][height];

    for (int i = 0; i < height; i++) {
        string line;
        getline(cin, line); // width characters, each either 0 or .
        for (int j = 0; j < line.length(); j++) {
            nodes[j][i] = line[j];
        }
    }

    // Write an action using cout. DON'T FORGET THE "<< endl"
    // To debug: cerr << "Debug messages..." << endl;
    
    for (int i = 0; i < height; i++) {
        for (int j = 0; j < width; j++) {
            if (nodes[j][i] == '0') {
                Node cur_node = Node();
                cur_node.x = j;
                cur_node.y = i;


                int tempx = j;
                while(++tempx < width && nodes[tempx][i] == '.'); // 向右查找,直到找到包含node的位置,或者直到最右

                Node r_node = Node();
                if (tempx < width) { 
                    r_node.x = tempx;
                    r_node.y = i;
                } else { 
                    r_node.x = -1;
                    r_node.y = -1;
                }
 

                int tempy = i;
                while(++tempy < height && nodes[j][tempy] == '.');// 向下查找,直到最下

                Node b_node = Node();
                if (tempy < height) {
                    b_node.x = j;
                    b_node.y = tempy;
                } else { 
                    b_node.x = -1;
                    b_node.y = -1;
                }

            cout << cur_node.getOutput() << r_node.getOutput() << b_node.getOutput() << endl;
            }
        }
    }

    // Three coordinates: a node, its right neighbor, its bottom neighbor
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值