NOJ [1257] Get Lost

这篇博客讨论了一个关于在网格上按照给定方向行走的问题。输入包括网格的大小和起点坐标,以及网格上的字符指示行走方向(N,S,W,E)。程序通过遍历网格并检查当前位置是否已访问过或是否超出边界来判断行走路径。最终输出YES表示到达边界,NO表示返回已访问过的格子。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

模拟题,按照当前所站的位置的NSWE来走。
当走到地图外面去了,YES。
当走到以前走过的格子,NO。

出题代码:

#include <set>
#include <list>
#include <stack>
#include <queue>
#include <cmath>
#include <cstdio>
#include <vector>
#include <iomanip>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;

int m, n;
int v[22][22];
char t[22][22];

int main()
{
while (~scanf("%d%d", &m, &n))
{
int x, y;
scanf("%d%d", &x, &y);
memset(v, 0, sizeof(v));
for (int i = 0; i < m; i++)
{
scanf("%s", t[i]);
}
while (1)
{
if (x < 0 || y < 0 || x == m || y == n)
{
printf("YES\n");
break;
}
if (v[x][y])
{
printf("NO\n");
break;
}
v[x][y] = 1;
if (t[x][y] == 'N') x--;
else if (t[x][y] == 'S') x++;
else if (t[x][y] == 'W') y--;
else if (t[x][y] == 'E') y++;
}
}

return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值