题目描述
在游戏中,方块被排列成一个二维网格,玩家点击一个方块时,该方块及其周围的方块会高亮。为了实现这个功能提出了“方块组”的概念:一个方块的方块组包括它自己以及上下左右紧挨着的方块。垂直方向上的方块是首尾相连的,但水平方向上的方块是首尾不连的。
例如,在一个 (8 * 3) 的方块网格中,方块 0 的方块组是 “16 8 1 0”,方块 10 的方块组是 “2 9 18 11 10”,方块 23 的方块组是 “15 22 7 23”。
现在需要你设计一个程序,当玩家点击某个方块时,能够实时得到该方块的方块组。
输入描述
- 第一行包含三个正整数 ( H )、( V ) 和 ( M ),分别表示方块盘的水平个数、垂直个数和查询次数,满足 ( 1 <= H, V <= 256 ) 和 ( 1 < M < H * V )。
- 接下来 ( M ) 行,每行包含一个方块的 ID,方块 ID 从 0 开始,不超过 ( H * V - 1 )。
输出描述
对于每个查询的方块 ID,输出对应的方块组,方块组内的 ID 以空格分隔,从上方起始,逆时针排列,输入的方块 ID 放在最后。
用例输入
12 4 1
21
9 20 33 22 21
16 4 2
0
21
48 16 1 0
5 20 37 22 21
解题思路
对于每个查询的方块 ID:
- 上方:计算上方方块的 ID,考虑垂直方向的首尾相连。
- 左方:计算左方方块的 ID,注意水平方向的边界。
- 下方:计算下方方块的 ID,考虑垂直方向的首尾相连。
- 右方:计算右方方块的 ID,注意水平方向的边界。
- 自身:将查询的方块 ID 放在最后。
代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include<algorithm>
#include<string>
#include<vector>
#include<unordered_map>
#include<unordered_set>
#include<queue>
#include<set>
#include<list>
#include <sstream>
#include <bitset>
#include <stack>
using namespace std;
int h, v; // 水平个数和垂直个数
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int num = 0;
cin >> h >> v;
int m, q;
cin >> m;
for (int i = 0; i < m; i++) {
cin >> q;
cout << (q - h + h * v) % (h * v) << " ";//上
if (q % h != 0) cout << q - 1<<" "; //左
cout << (q + h + h * v) % (h * v) << " ";//下
if(q % h !=h-1) cout << q + 1<<" "; //右
cout << q<<"\n";
}
}