模拟 POJ 1573 Robot Motion
2024-10-12 15:36:52
题目地址:http://poj.org/problem?id=1573
/*
题意:给定地图和起始位置,robot(上下左右)一步一步去走,问走出地图的步数
如果是死循环,输出走进死循环之前的步数和死循环的步数
模拟题:used记录走过的点,因为路线定死了,所以不是死循环的路只会走一次,可以区分出两个步数 注意:比较坑的是,如果不是死循环,临界(走进去就出来)步数是1;而死循环却是0. 这里WA几次。。。
*/
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <string>
#include <map>
#include <queue>
#include <vector>
using namespace std; const int MAXN = ;
const int INF = 0x3f3f3f3f;
int a[MAXN][MAXN];
int used[MAXN][MAXN]; void work(int n, int m, int k)
{
int ans = ; int cnt = ; int i = , j = k;
while (i >= && i <= n && j >= && j <= m)
{
if (used[i][j] <= )
{
switch (a[i][j])
{
case 'N': i -= ; break;
case 'S': i += ; break;
case 'W': j -= ; break;
case 'E': j += ; break;
}
used[i][j]++;
if (used[i][j] == ) cnt++;
else ans++;
}
else
{
int res = ans - cnt;
if (res == ) res = ;
printf ("%d step(s) before a loop of %d step(s)\n", res, cnt);
break;
}
}
if (i < || i > n || j < || j > m)
printf ("%d step(s) to exit\n", ans);
} int main(void) //POJ 1573 Robot Motion
{
//freopen ("H.in", "r", stdin); int n, m, k;
while (~scanf ("%d%d%d", &n, &m, &k) && n && m && k)
{
getchar ();
for (int i=; i<=n; ++i)
{
for (int j=; j<=m; ++j)
{
a[i][j] = getchar ();
}
getchar ();
} memset (used, , sizeof (used));
work (n, m, k);
} return ;
} /*
10 step(s) to exit
3 step(s) before a loop of 8 step(s)
*/
最新文章
- List集合去重的一种方法
- [codevs1105][COJ0183][NOIP2005]过河
- Art-template模板
- Python统计百分比及排序
- Kendo UI for ASP.NET MVC 的一些使用经验(转)
- 作业一:创建个人技术博客、自我介绍、简单的C程序
- Displaying SharePoint Lists or Libraries in other sites 显示其他站点的List
- Ibatis中传List参数
- 【转】iOS开发--一步步教你彻底学会『iOS应用间相互跳转』
- Python zxing 库解析(条形码二维码识别)
- 4位或者5位led数码显示,485通信modbus,支持任意小数点写入,工业标准设置,可和plc,dcs,组态完美对接,支持定制修改
- HTML+CSS - 搜索 And 高级搜索
- nginx反向代理二级域名注意事项
- web前端异步数据交互(长连接)
- utf-8并不";兼容"; gb2312, gb18030
- 解决运行wamp提示“MSVCR110.dll”丢失的问题!
- oracle 如何查看oracle数据库版本
- 关于使用mybatis的一个惨痛教训
- 使用排序数组/链表/preorder构建二叉搜索树
- find命令之时间戳使用示例