poj1573&&hdu1035 Robot Motion(模拟)
转载请注明出处:http://blog.csdn.net/u012860063?
viewmode=contents
题目链接:
HDU: pid=1035">http://acm.hdu.edu.cn/showproblem.php?pid=1035
POJ: id=1573">http://poj.org/problem? id=1573
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
which the robot enters from the north. The possible entry columns are numbered starting with one at the left. Then come the rows of the direction instructions. Each grid will have at least one and at most 10 rows and columns of instructions. The lines of instructions
contain only the characters N, S, E, or W with no blanks. The end of input is indicated by a row containing 0 0 0.
Output
on some number of locations repeatedly. The sample input below corresponds to the two grids above and illustrates the two forms of output. The word "step" is always immediately followed by "(s)" whether or not the number before it is 1.
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)
纯模拟题;
代码例如以下:
#include <iostream>
using namespace std;
#include <cstring>
#define M 1000
char mat[M][M];
int re[M][M];
int n, m, s;
int ans, i, j, k;
void F()
{
i = 1; j = s;
k = 0;
for(int l = 1; ; l++)
{
if(re[i][j] != 0)
{
ans = k - re[i][j];
break;
}
if(i>n || i<1 || j>m || j<1)
{
break;
}
if(mat[i][j] == 'N')
{
k++;
re[i][j] = k;
i-=1;
continue;
}
if(mat[i][j] == 'S')
{
k++;
re[i][j] = k;
i+=1; continue;
}
if(mat[i][j] == 'W')
{
k++;
re[i][j] = k;
j-=1;
continue;
}
if(mat[i][j] == 'E')
{
k++;
re[i][j] = k;
j+=1;
continue;
}
}
}
int main()
{
while(cin>>n>>m>>s)
{
ans = 0;
if(n == 0 && m == 0 && s == 0)
break;
memset(mat,0,sizeof(mat));
memset(re,0,sizeof(re));
for(i = 1; i <= n; i++)
{
for(j = 1; j <= m; j++)
{
cin>>mat[i][j];
}
}
F();
if(ans == 0)
cout<<k<<" step(s) to exit"<<endl;
else
cout<<re[i][j]-1<<" step(s) before a loop of "<<ans+1<<" step(s)"<<endl;
}
return 0;
}
poj1573&&hdu1035 Robot Motion(模拟)的更多相关文章
- HDU-1035 Robot Motion 模拟问题(水题)
题目链接:https://cn.vjudge.net/problem/HDU-1035 水题 代码 #include <cstdio> #include <map> int h ...
- [ACM] hdu 1035 Robot Motion (模拟或DFS)
Robot Motion Problem Description A robot has been programmed to follow the instructions in its path. ...
- HDU-1035 Robot Motion
http://acm.hdu.edu.cn/showproblem.php?pid=1035 Robot Motion Time Limit: 2000/1000 MS (Java/Others) ...
- hdu1035 Robot Motion (DFS)
Robot Motion Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tot ...
- POJ-1573 Robot Motion模拟
题目链接: https://vjudge.net/problem/POJ-1573 题目大意: 有一个N*M的区域,机器人从第一行的第几列进入,该区域全部由'N' , 'S' , 'W' , 'E' ...
- POJ1573 Robot Motion(模拟)
题目链接. 分析: 很简单的一道题, #include <iostream> #include <cstring> #include <cstdio> #inclu ...
- HDU1035 Robot Motion
Problem Description A robot has been programmed to follow the instructions in its path. Instructions ...
- POJ 1573 Robot Motion 模拟 难度:0
#define ONLINE_JUDGE #include<cstdio> #include <cstring> #include <algorithm> usin ...
- poj1573 Robot Motion
Robot Motion Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 12507 Accepted: 6070 Des ...
随机推荐
- C++创建对象的三种方式
C++在创建对象的时候,有三种方式: #include <iostream> using namespace std; class A { private: int n; public: ...
- jquery自定义分页插件
//每次只显示5个页码(function ($) { //设定页码方法,初始化 $.fn.setPager = function (options) { var opts = $.extend({}, ...
- mac下 配置 Apache Php Mysql
参考 http://www.guomii.com/posts/30136 参考 http://forums.mysql.com/read.php?11,600754,600754 MacOS 10.8 ...
- Log4J logger图片
- vs2010根据字符串内容添加断点
在vs中我们可以直接用表达式.数值型比较直接用操作符即可. 如i==2,i<2; 但是字符型比较呢? 加入我们有一个名为string的变量,定义如下: char *string="Tw ...
- __FILE__,__LINE__,FUNCTION__实现代码跟踪调试(linux下c语言编程 )
root@xuanfei-desktop:~/cpropram/2# cat global.h //头文件#ifndef CLOBAL_H #define GLOBAL_H ...
- 解题报告 HDU1159 Common Subsequence
Common Subsequence Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u ...
- Codeforces 41D Pawn 简单dp
题目链接:点击打开链接 给定n*m 的矩阵 常数k 以下一个n*m的矩阵,每一个位置由 0-9的一个整数表示 问: 从最后一行開始向上走到第一行使得路径上的和 % (k+1) == 0 每一个格子仅仅 ...
- MCU开发之I2C通信
程序状态字PSW是8位寄存器,用于存放程序运行的状态信息,PSW中各位状态通常是在指令执行的过程中自动形成的,但也可以由用户根据需要采用传送指令加以改变.各个标志位的意义如下: PSW.7(Cy):进 ...
- ActionBar开启Overlay Mode(覆盖模式)
以下内容参考自Android官网http://developer.android.com/training/basics/actionbar/overlaying.html#EnableOverlay ...