poj 1573 - Robot Motion(BFS递推)
来源:互联网 发布:linux syslogd 编辑:程序博客网 时间:2024/05/20 14:16
题目链接:POJ 1573- Robot Motion
题目大意:给出一张图,图上标明了可行走的方向一个机器人从指定的位置进入,问能否离开这张图(任意方向)
解题思路:bfs因为只有一个方向,即移动方向已经确定,所以不需要递归,代码量也很少,所以建议尝试递推。
#include <stdio.h>#include <string.h>const int N = 105;const int dir[][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };int r, c, begin;int rec, cnt; int g[N][N], vis[N][N];int change(char ch) {switch(ch) {case 'N': return 0;case 'S': return 1;case 'W': return 2;case 'E': return 3;}}bool solve() {int x = 0, y = begin - 1;memset(vis, 0, sizeof(vis));cnt = 0;while (1) {if (x < 0 || x >= r || y < 0 || y >= c) return true;cnt++;vis[x][y] = cnt;int d = g[x][y];x += dir[d][0];y += dir[d][1];if (vis[x][y]) {rec = vis[x][y] - 1;return false;}}}int main () {char ch;while (scanf("%d%d%d", &r, &c, &begin), r || c || begin) {for (int i = 0; i < r; i++) {scanf("%c", &ch);for (int j = 0; j < c; j++) {scanf("%c", &ch);g[i][j] = change(ch);}}if (solve())printf("%d step(s) to exit\n", cnt);elseprintf("%d step(s) before a loop of %d step(s)\n", rec, cnt - rec);}return 0;}
- poj 1573 - Robot Motion(BFS递推)
- POJ 1573 Robot Motion(BFS)
- POJ 1573 Robot Motion(BFS)
- Robot Motion(BFS)
- poj 1573 Robot Motion
- Poj 1573 Robot Motion
- poj 1573 Robot Motion
- POJ 1573 - Robot Motion
- poj-1573-Robot Motion
- POJ 1573 Robot Motion
- POJ 1573 Robot Motion
- POJ-1573-Robot Motion
- poj 1573 Robot Motion
- POJ 1573 Robot Motion
- POJ 1573 Robot Motion.
- poj 1573 Robot Motion
- POJ -1573 Robot Motion
- POJ 1573:Robot Motion
- 【GUI编程之准备--1】MAC 编译wxWidgets 源码
- 权限管理
- 正方形(一)
- java中list的基本用法
- <script type="text/javascript" defer="defer">
- poj 1573 - Robot Motion(BFS递推)
- 关于python文件夹操作
- linux下C/C++ IDE
- SCSF - Part 1 Modules and Shells
- 今天来聊聊IE10 CSS Hack
- OSGEARTH三维地形开源项目
- 海量文档查同或聚类问题 -- Locality Sensitive Hash 算法 (转)
- IOS中TextField点击取消键盘
- 黑马程序员:JAVA整数数据类型取值范围及为什么是这个范围?