题目地址: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)
*/

最新文章

  1. List集合去重的一种方法
  2. [codevs1105][COJ0183][NOIP2005]过河
  3. Art-template模板
  4. Python统计百分比及排序
  5. Kendo UI for ASP.NET MVC 的一些使用经验(转)
  6. 作业一:创建个人技术博客、自我介绍、简单的C程序
  7. Displaying SharePoint Lists or Libraries in other sites 显示其他站点的List
  8. Ibatis中传List参数
  9. 【转】iOS开发--一步步教你彻底学会『iOS应用间相互跳转』
  10. Python zxing 库解析(条形码二维码识别)
  11. 4位或者5位led数码显示,485通信modbus,支持任意小数点写入,工业标准设置,可和plc,dcs,组态完美对接,支持定制修改
  12. HTML+CSS - 搜索 And 高级搜索
  13. nginx反向代理二级域名注意事项
  14. web前端异步数据交互(长连接)
  15. utf-8并不&quot;兼容&quot; gb2312, gb18030
  16. 解决运行wamp提示“MSVCR110.dll”丢失的问题!
  17. oracle 如何查看oracle数据库版本
  18. 关于使用mybatis的一个惨痛教训
  19. 使用排序数组/链表/preorder构建二叉搜索树
  20. find命令之时间戳使用示例

热门文章

  1. 最近打算体验一下discuz,有不错的结构化数据插件
  2. PHP导出数据到CSV文件函数/方法
  3. [Effective JavaScript 笔记]第44条:使用null原型以防止原型污染
  4. Tooltip jqueryui
  5. [codeforces 325]B. Stadium and Games
  6. Dynamic Morphing Square(动态变形矩阵)
  7. js判断元素是否隐藏的方法
  8. css+div绝对定位
  9. python在线文档
  10. iOS 和Android中的正则表达式简单使用