Wannafly挑战赛17 - 走格子(模拟)
生活随笔
收集整理的這篇文章主要介紹了
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; } 與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的Wannafly挑战赛17 - 走格子(模拟)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Wannafly挑战赛17 - 求值2
- 下一篇: 网络流 (EK Dinic)