B2100 同行列对角线的格
思路
观察下图:
红色箭头代表枚举顺序。
我们4次枚举 $x,y$,依次判断 $x,y$ 是否满足图中的四个条件。
注意左下-右上的箭头对于下标是逆序的,枚举时要注意。
代码
#include <iostream>
#include <algorithm>
using namespace std;
int n, i, j;
int main()
{
cin >> n >> i >> j;
for(int x = 1;x <= n;++x)
for(int y = 1;y <= n;++y)
if(x == i)
cout << "(" << x << "," << y << ") ";cout << endl;//同行
for(int x = 1;x <= n;++x)
for(int y = 1;y <= n;++y)
if(y == j)
cout << "(" << x << "," << y << ") ";cout << endl;//同列
for(int x = 1;x <= n;++x)
for(int y = 1;y <= n;++y)
if(x - y == i - j)
cout << "(" << x << "," << y << ") ";cout << endl;//左上-右下
for(int x = n;x >= 1;--x) //左下-右上,注意逆序枚举
for(int y = n;y >= 1;--y)
if(x + y == i + j)
cout << "(" << x << "," << y << ") ";cout << endl;
return 0;
}