POJ 1573 Robot Motion
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 12978 | Accepted: 6290 |
Description

A robot has been programmed to follow the instructions in its path. Instructions for the next direction the robot is to move are laid down in a grid. The possible instructions are
N north (up the page)
S south (down the page)
E east (to the right on the page)
W west (to the left on the page)
For example, suppose the robot starts on the north (top) side of Grid 1 and starts south (down). The path the robot follows is shown. The robot goes through 10 instructions in the grid before leaving the grid.
Compare what happens in Grid 2: the robot goes through 3 instructions only once, and then starts a loop through 8 instructions, and never exits.
You are to write a program that determines how long it takes a robot to get out of the grid or how the robot loops around.
Input
Output
Sample Input
- 3 6 5
- NEESWE
- WWWESS
- SNWWWW
- 4 5 1
- SESWE
- EESNW
- NWEEN
- EWSEN
- 0 0 0
Sample Output
- 10 step(s) to exit
- 3 step(s) before a loop of 8 step(s)
Source
- #include <cstdio>
- #include <cstring>
- char s[15][15];
- int n, m , start;
- int dis[15][15];
- bool inGrid(int x, int y)
- {
- return 1 <= x && x <= n && 1 <= y && y <= m;
- }
- void solve()
- {
- memset(dis, 0, sizeof(dis));
- int time = 0;
- int x = 1, y = start;
- while(1){
- if(!inGrid(x, y)){
- printf("%d step(s) to exit\n", time);
- break;
- }
- if(dis[x][y] != 0){
- printf("%d step(s) before a loop of %d step(s)\n", dis[x][y]-1, time-dis[x][y]+1);
- break;
- }
- dis[x][y] = ++time;
- if(s[x][y] == 'N') --x;
- else if(s[x][y] == 'S') ++x;
- else if(s[x][y] == 'E') ++y;
- else if(s[x][y] == 'W') --y;
- }
- }
- int main()
- {
- while(scanf("%d%d%d", &n, &m, &start), n){
- for(int i = 1; i <= n; ++i)
- scanf("%s", &s[i][1]);
- solve();
- }
- return 0;
- }
POJ 1573 Robot Motion的更多相关文章
- 模拟 POJ 1573 Robot Motion
题目地址:http://poj.org/problem?id=1573 /* 题意:给定地图和起始位置,robot(上下左右)一步一步去走,问走出地图的步数 如果是死循环,输出走进死循环之前的步数和死 ...
- POJ 1573 Robot Motion(BFS)
Robot Motion Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 12856 Accepted: 6240 Des ...
- POJ 1573 Robot Motion(模拟)
题目代号:POJ 1573 题目链接:http://poj.org/problem?id=1573 Language: Default Robot Motion Time Limit: 1000MS ...
- poj 1573 Robot Motion【模拟题 写个while循环一直到机器人跳出来】
...
- POJ 1573 Robot Motion 模拟 难度:0
#define ONLINE_JUDGE #include<cstdio> #include <cstring> #include <algorithm> usin ...
- Poj OpenJudge 百练 1573 Robot Motion
1.Link: http://poj.org/problem?id=1573 http://bailian.openjudge.cn/practice/1573/ 2.Content: Robot M ...
- poj 1573 Robot Motion_模拟
又是被自己的方向搞混了 题意:走出去和遇到之前走过的就输出. #include <cstdlib> #include <iostream> #include<cstdio ...
- PKU 1573 Robot Motion(简单模拟)
原题大意:原题链接 给出一个矩阵(矩阵中的元素均为方向英文字母),和人的初始位置,问是否能根据这些英文字母走出矩阵.(因为有可能形成环而走不出去) 此题虽然属于水题,但是完全独立完成而且直接1A还是很 ...
- 【POJ - 1573】Robot Motion
-->Robot Motion 直接中文 Descriptions: 样例1 样例2 有一个N*M的区域,机器人从第一行的第几列进入,该区域全部由'N' , 'S' , 'W' , 'E' ,走 ...
随机推荐
- poj 2031 Building a Space Station(最小生成树,三维,基础)
只是坐标变成三维得了,而且要减去两边的半径而已 题目 //最小生成树,只是变成三维的了 #define _CRT_SECURE_NO_WARNINGS #include<stdlib.h> ...
- 架构探险——从零开始写Java Web框架》第二章照作
沉下来慢慢看实现了. 越来越觉得可以和DJANGO作对比. package org.smart4j.chapter2.model; /** * Created by sahara on 2016/3/ ...
- JavaC 编译目录下所有的UTF-8编码的java文件
javac -encoding UTF-8 *.java
- Servlet 各种path路径比较
假定你的web application 名称为news,你在浏览器中输入请求路径: http://localhost:8080/news/main/list.jsp 则执行下面向行代码后打印出如下结果 ...
- [DLX]HDOJ4069 Squiggly Sudoku
题意:有9*9的格子 每个格子 由五部分组成:上(16).右(32).下(64).左(128).和该格的数值(0~9) 若上下左右有分割格子的线 就加上相应的数, 该格的数值若为0,则是未知 1~9 ...
- @JsonFormat时间不对
实际时间为:2015-07-06 20:20:23 1. @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") private Date ...
- iOS UICollectionView简单使用
UICollectionView 和 UICollectionViewController 类是iOS6 新引进的API,用于展示集合视图,布局更加灵活,可实现多列布局,用法类似于UITableVie ...
- NSMutableArray 初始化与添加删除程序
Person *person1=[[Person alloc]initWithName:@"Kenshin"]; Person *person2=[[P ...
- 在Android里完美实现基站和WIFI定位
来自:http://www.cnblogs.com/coffeegg/archive/2011/10/01/2197129.html 众所周知的,在OPhone和大部分国产的Android定制机里不支 ...
- mysql字符串区分大小写的问题
一.1. CREATE TABLE NAME(name VARCHAR(10)); 对这个表,缺省情况下,下面两个查询的结果是一样的: SELECT * FROM TABLE NAME WHERE n ...