快速切题 poj1573
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 10708 | Accepted: 5192 |
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)
题意:一个迷宫,机器人从某行最上方进入,跟随每一步到达的迷宫的指示行动,问机器人是否能到达迷宫外(任意一边都可以出),或者会在何时进入圈
应用时:15min
实际用时:51min
原因:读题,x,y用反
#define ONLINE_JUDGE
#include<cstdio>
#include <cstring>
#include <algorithm>
using namespace std; int A,B,sx,sy;
char maz[101][101];
int vis[101][101];
const int dx[4]={0,1,0,-1};
const int dy[4]={-1,0,1,0}; int dir(char ch){
if(ch=='N')return 0;
else if(ch=='E')return 1;
else if(ch=='S')return 2;
return 3;
}
void solve(){
memset(vis,0,sizeof(vis));
sx--;sy=0;
int step=0;
int fx,fy;
while(!vis[sy][sx]&&sx>=0&&sx<A&&sy>=0&&sy<B&&++step){
fx=sx;fy=sy;
vis[sy][sx]=step;
sx=dx[dir(maz[fy][fx])]+fx;
sy=dy[dir(maz[fy][fx])]+fy;
}
if(sx<0||sy<0||sx>=A||sy>=B)printf("%d step(s) to exit\n",step);
else {
printf("%d step(s) before a loop of %d step(s)\n",vis[sy][sx]-1,step+1-vis[sy][sx]);
}
}
int main(){
#ifndef ONLINE_JUDGE
freopen("output.txt","w",stdout);
#endif // ONLINE_JUDGE
while(scanf("%d%d%d",&B,&A,&sx)==3&&A&&B){ for(int i=0;i<B;i++)scanf("%s",maz[i]);
solve();
}
return 0;
}
快速切题 poj1573的更多相关文章
- 快速切题sgu127. Telephone directory
127. Telephone directory time limit per test: 0.25 sec. memory limit per test: 4096 KB CIA has decid ...
- 快速切题sgu126. Boxes
126. Boxes time limit per test: 0.25 sec. memory limit per test: 4096 KB There are two boxes. There ...
- 快速切题 sgu123. The sum
123. The sum time limit per test: 0.25 sec. memory limit per test: 4096 KB The Fibonacci sequence of ...
- 快速切题 sgu120. Archipelago 计算几何
120. Archipelago time limit per test: 0.25 sec. memory limit per test: 4096 KB Archipelago Ber-Islan ...
- 快速切题 sgu119. Magic Pairs
119. Magic Pairs time limit per test: 0.5 sec. memory limit per test: 4096 KB “Prove that for any in ...
- 快速切题 sgu118. Digital Root 秦九韶公式
118. Digital Root time limit per test: 0.25 sec. memory limit per test: 4096 KB Let f(n) be a sum of ...
- 快速切题 sgu117. Counting 分解质因数
117. Counting time limit per test: 0.25 sec. memory limit per test: 4096 KB Find amount of numbers f ...
- 快速切题 sgu116. Index of super-prime bfs+树思想
116. Index of super-prime time limit per test: 0.25 sec. memory limit per test: 4096 KB Let P1, P2, ...
- 快速切题 sgu115. Calendar 模拟 难度:0
115. Calendar time limit per test: 0.25 sec. memory limit per test: 4096 KB First year of new millen ...
随机推荐
- 使用maven开发MR
1.安装mvn(可以省略,因为eclipse自带maven插件) 1.1. 上传安装包到linux 1.2. 解压maven安装包 mkdir /usr/maven -bin.tar.gz -C /u ...
- 【Tomca安装与启动】tomcatLinux环境安装与启动
一.安装 1.下载tomcat安装包 2.解压安装包 3.配置环境变量 打开~/.bash_profile文件,输入一下两句话: export TOMCAT_HOME=/Users/enniu1/De ...
- Linux下指定pip install和make install安装路径
在Linux下直接用pip install packageName,有些文件会被放到根目录下,如果没有sudo权限的话,是会安装失败的.这个以后我们就需要指定安装的目录了. pip install - ...
- 广州工业大学2016校赛 F 我是好人4 dfs+容斥
Problem F: 我是好人4 Description 众所周知,我是好人!所以不会出太难的题,题意很简单 给你n个数,问你1000000000(含1e9)以内有多少个正整数不是这n个数任意一个的倍 ...
- 代码中特殊的注释技术 -- TODO、FIXME和XXX的用处
借鉴地址:https://www.cnblogs.com/pengyingh/articles/2445826.html 在阅读一些代码时,经常会遇到诸如:TODO.FIXME和XXX的单词,它们是有 ...
- java编写编译器和解释器
on 2012-07-14 21:24 Bang 阅读(102) 评论(0) 编辑 收藏 续 第二部分 初始后端实现 框架后端支持编译器和解释器.现在框架抽象类Backend有两个极简版实现,一个 ...
- Linux上安装Oracle的教程-centos7
一.安装Oracle前准备 1.创建运行oracle数据库的系统用户和用户组 [humf@localhost ~]$ su root #切换到root Password: [root@lo ...
- Java 常用对象-BigInteger类
2017-11-02 21:57:09 BigInteger类:不可变的任意精度的整数.所有操作中,都以二进制补码形式表示 BigInteger(如 Java 的基本整数类型).BigInteger ...
- Ubuntu 18.04 install JDK8.201
sudo add-apt-repository ppa:webupd8team/java Press Enter to continue. sudo apt update sudo apt insta ...
- 『PyTorch』第一弹_静动态图构建if逻辑对比
对比TensorFlow和Pytorch的动静态图构建上的差异 静态图框架设计好了不能够修改,且定义静态图时需要使用新的特殊语法,这也意味着图设定时无法使用if.while.for-loop等结构,而 ...