CF-Codeforces Round #664 (Div. 2)-1395B. Boboniu Plays Chess【思维】

题目链接
题意:给你 n ∗ m n*m nm的网格和起始位置,每次可以在自己行和列内任意走动,问如何不重复的走完所有格子。
思路:直接先向上走到上边界,再向左走到左边界,此时即到了 ( 1 , 1 ) (1,1) (1,1)点,然后蛇形走位就行了,过程中维护一个 v i s vis vis数组标记是否访问过即可。
AC代码:

#include 
#define ll long long
using namespace std;
int T = 1;
bool vis[111][111];
signed main() {
    //cin>>T;
    while (T--) {
        memset(vis, false, sizeof(vis));
        int n, m, x0, y0;
        cin >> n >> m >> x0 >> y0;
        for (int i = x0; i >= 1; i--) {
            cout << i << ' ' << y0 << "\n";
            vis[i][y0] = true;
        }
        for (int i = y0; i >= 1; i--) {
            if (!vis[1][i]) cout << 1 << ' ' << i << "\n";
            vis[1][i] = true;
        }
        for (int i = 1; i <= n; i++) {
            if (i & 1)
                for (int j = 1; j <= m; j++) {
                    if (!vis[i][j])cout << i << ' ' << j << "\n";
                    vis[i][j] = true;
                }
            else
                for (int j = m; j >= 1; j--) {
                    if (!vis[i][j])cout << i << ' ' << j << "\n";
                    vis[i][j] = true;
                }
        }
    }
    return 0;
}

你可能感兴趣的:(codeforces)