POJ 1573 Robot Motion模拟
来源:互联网 发布:js 时间排序函数 编辑:程序博客网 时间:2024/06/05 02:59
题目:http://poj.org/problem?id=1573
题目大意:一个棋牌上布满了N,E,S,W指令,人从北边的一个初始列按照棋牌的指令开始移动。直到走出棋牌,或者出现循环为止。
N表示向北(上)移动一格,
E表示向东(右)移动一格,
S表示向南(下)移动一格,
W表示向西(左)移动一格
分析:因为棋牌只有10*10,也就是最多只有100步而已,因此可以用模拟的算法。我们声明一个char的棋牌map用于记录指令,一个初始值为全0的棋牌用于记录走过的路径。进行模拟直到人走出棋牌或者发现path不为0了,证明出现了环。
代码如下:
#include <stdio.h>#include<string.h>#define ONLINEvoid online(){#ifdef ONLINE#elsefreopen("1573.in", "r", stdin);freopen("1573.out", "w", stdout);#endif}const int LEN = 11;int r, c, s;char map[LEN][LEN];int path[LEN][LEN];int steps, loops;//模拟void imitate(int x, int y, int s){if (x <=0 || x > r || y <= 0 || y > c){steps = s;loops = -1;return ;}if (path[x][y] != 0){steps = path[x][y]-1;loops = s - steps;return;}path[x][y] = s +1;if (map[x][y] == 'N'){imitate(x-1, y, path[x][y]);}else if (map[x][y] == 'S'){imitate(x+1,y, path[x][y]);}else if (map[x][y] == 'W'){imitate(x, y-1, path[x][y]);}else if (map[x][y] == 'E'){imitate(x, y+1, path[x][y]);}}//打印void print(){if (loops == -1){printf("%d step(s) to exit\n", steps);}elseprintf("%d step(s) before a loop of %d step(s)\n", steps, loops);}//读取数据void read(){while (true){scanf("%d%d%d\n", &r, &c, &s);if (r==0 && c == 0 && s == 0){break;}for(int i =1; i <= r; i ++){for (int j=1; j <= c; j ++){scanf("%c", &map[i][j]);}//end for jscanf("\n");}//end for i;memset(path, 0, LEN * LEN * sizeof(int));imitate(1, s, 0);print();}//end while }int main(){online();read();return 0;}运行结果如下:1573Accepted176K0MSC++1423B2011-08-02 22:36:30
- 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(模拟)
- HDU 1035 && poj 1573 Robot Motion【模拟】
- POJ 1573 Robot Motion(模拟水题)
- POJ 1573 Robot Motion 水模拟
- POJ 1573 Robot Motion 模拟机器人行走
- 数据结构(C语言版)规范代码之图(邻接表的拓扑排序)
- [HKEY_LOCAL_MACHINE\init]键值的含义
- proc文件系统解析
- 面试,杯具了也可以从中找到不足
- VC和Perl的互相调用收集
- POJ 1573 Robot Motion模拟
- 数据结构(C语言版)规范代码之图(邻接多重表遍历)
- 2B
- 35岁的程序员,何去何从?
- JavaScript学习思索
- java多线程实现火车售票系统 以及java中的同步的实现 同步块 和同步方法同时 同步
- 数据库还原
- C语言测试题
- Hibernate的多条件查询通用方法(查询条件个数不限,能进行模糊、精确2种查...