Wannafly挑战赛17 - 走格子(模拟)

题目链接

题目描述

在平面上有n*n大小的正方形,定义正方形左下角坐标是(1,1),右下角坐标是(n,1)

现在A君在左下角,他的初始方向是向右,他要在正方形内走m步
当A君碰到边界或者已经走过的格子时,他便会逆时针转90°继续走,直到走完m步。
现在给你两个整数n和m,请算出走完m步后A君的坐标。

输入描述:

输入一行两个整数n和m。

输出描述:

输出一行两个数表示A君的坐标。

输入

3 3

输出

3 2

备注:

n<=1000,m

思路

模拟

AC

#include<bits/stdc++.h>
#define N 1005
#define ll long long
#define mem(a, b) memset(a, b, sizeof(a));
using namespace std;
int inf = 0x3f3f3f3f;
int vis[N][N];
int n, m;
// 判断这个点是否合法
int judge_point(int x, int y) {
    if (x >=1 && x <= n && y >= 1 && y <= n && vis[x][y] == 0)
            return 1;
    else    return 0;
}
// 判断下个点是否能走
int judge_dir(int dir, int now_x, int now_y) {
    if (dir == 1 && judge_point(now_x - 1, now_y))
        return 1;
    if (dir == 2 && judge_point(now_x, now_y - 1))
        return 1;
    if (dir == 3 && judge_point(now_x + 1, now_y))
        return 1;
    if (dir == 4 && judge_point(now_x, now_y + 1))
        return 1;
    return 0;
}
void change_dir(int dir, int &now_x, int & now_y) {
    if (dir == 1)   now_x -= 1;
    if (dir == 2)   now_y -= 1;
    if (dir == 3)   now_x += 1;
    if (dir == 4)   now_y += 1;
    vis[now_x][now_y] = 1;
}
int main () {
    ios::sync_with_stdio(false);
    // freopen("in.txt", "r", stdin);
    cin >> n >> m;
    int now_x = n;
    int now_y = 1;
    // 方向: 1 上,2 左, 3 下, 4 右
    int dir = 4;
    vis[now_x][now_y] = 1;
    while (m--) {
        if (judge_dir(dir, now_x, now_y))   change_dir(dir, now_x, now_y);
        else {
            dir = (dir + 1) % 4;
            if (dir == 0)   dir = 4;
            change_dir(dir, now_x, now_y);
        }
    }
    int ans_x = now_y, ans_y = n + 1 - now_x;
    cout << ans_x << " " << ans_y << endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/henuyh/article/details/80629178