题目大意
给你一个n×m 的矩阵,给你起点(x,y)让你输出一个轨迹,保证不重不漏的走过每一个点。 x和y保证分别严格小于n和m。
Examples
input
3 3 2 2
output
2 2
1 2
1 3
2 3
3 3
3 2
3 1
2 1
1 1
input
3 4 2 2
output
2 2
2 1
2 3
2 4
1 4
3 4
3 3
3 2
3 1
1 1
1 2
1 3
解题思路
我的思路是,先走起点的这一行,然后再走下面的所有格子,最后走上面的。
具体走法是按蛇形去,走如下图:
最后看一下代码吧
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n, m, x, y;
cin >> n >> m >> x >> y;
// 第一行
cout << x << ' ' << y << '\n';
for (int i = 1; i < y; i ++)
cout << x << ' ' << i << '\n';
for (int i = y + 1; i <= m; i ++)
cout << x << ' ' << i << '\n';
//下面的
int t = m + 1;
for (int i = x + 1; i <= n; i ++)
{
if (t == m + 1)
{
t --;
while (t > 0)
{
cout << i << ' ' << t << '\n';
t --;
}
}
else if (t == 0)
{
t ++;
while (t <= m)
{
cout << i << ' ' << t << '\n';
t ++;
}
}
}
// 上面的
for (int i = 1; i < x; i ++)
{
if (t == m + 1)
{
t --;
while (t > 0)
{
cout << i << ' ' << t << '\n';
t --;
}
}
else if (t == 0)
{
t ++;
while (t <= m)
{
cout << i << ' ' << t << '\n';
t ++;
}
}
}
return 0;
}
版权声明:本文为qq_62802647原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。