poj 1573 Robot Motion
来源:互联网 发布:sql server创建视图 编辑:程序博客网 时间:2024/06/05 11:38
题意:给出3个数字,分别为指令矩阵的行数、列数和机器人进入矩阵时所在列。机器人每次都从矩阵的北边进入矩阵,矩阵中的字符指代机器人的移动方向,输出机器人在矩阵中的移动步数;如果机器人进入死循环,输出进入死循环前的移动步数和死循环的大小。
分析:模拟机器人在矩阵走,如果某个方格经过了两次,说明进入了死循环。
#include<cstdio>#include<cstring>using namespace std;const int MAX = 101;const int dx[] = {0, -1, 0, 1}, dy[] = {-1, 0, 1, 0};int dir[MAX][MAX], cnt[MAX][MAX];int main() { int n, m, s; while(scanf("%d%d%d", &n, &m, &s) != EOF) { if(!n && !m && !s) { return 0; } for(int i = 1; i <= n; i++) { getchar(); for(int j = 1; j <= m; j++) { char ch = getchar(); if(ch == 'W') { dir[i][j] = 0; } else if(ch == 'N') { dir[i][j] = 1; } else if(ch == 'E') { dir[i][j] = 2; } else { dir[i][j] = 3; } } } int x = 1, y = s, step = 0, loop = 0, d; memset(cnt, 0, sizeof(cnt)); while(x > 0 && x <= n && y > 0 && y <= m && cnt[x][y] != 2) { step++; loop += cnt[x][y]; cnt[x][y]++; d = dir[x][y]; x += dx[d]; y += dy[d]; } if(!loop) { printf("%d step(s) to exit\n", step); } else { printf("%d step(s) before a loop of %d step(s)\n", step - loop * 2, loop); } } return 0;}
0 0
- 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
- poj 1573 Robot Motion
- poj 1573 Robot Motion
- POJ 1573 Robot Motion
- Robot Motion POJ 1573
- c语言----交换a,b的值(分别用临时变量,加减法,异或实现)
- 解决listview加载网络图片乱跳
- 构造方法
- android事件分发源码解析(下)
- Fragment的使用
- poj 1573 Robot Motion
- 成员方法
- 地址栏中去掉端口号 java
- AcDream1007
- 读书笔记:C++ primer 5th edition--chapter12.动态内存
- 最优化算法 之 PSO算法
- hdu5378 Leader in Tree Land(概率dp+逆元)
- 数据结构(二)基本概念
- 8. String to Integer (atoi)--2016/10/10