说明
生词
-
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
}