Robot Motion
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 12351   Accepted: 5982

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

There
will be one or more grids for robots to navigate. The data for each is
in the following form. On the first line are three integers separated by
blanks: the number of rows in the grid, the number of columns in the
grid, and the number of the column in 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

For
each grid in the input there is one line of output. Either the robot
follows a certain number of instructions and exits the grid on any one
the four sides or else the robot follows the instructions on a certain
number of locations once, and then the instructions 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)

Source

 
用vis 数组做计数器。如果碰到已标记的就证明走回来了。
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<math.h>
#include<queue>
#include<iostream>
using namespace std;
char graph[][];
int n,m,k;
int vis[][];
struct Node
{
int x,y;
int step;
} s;
bool check(int x,int y)
{
if(x<||x>=n||y<||y>=m) return false;
return true;
}
void bfs()
{
memset(vis,,sizeof(vis));
Node s;
s.x = ,s.y = k-,s.step=;
queue<Node> q;
q.push(s);
vis[s.x][s.y] = ;
while(!q.empty())
{
Node now = q.front();
q.pop();
Node next;
if(graph[now.x][now.y]=='W')
{
next.x = now.x;
next.y = now.y-;
}
if(graph[now.x][now.y]=='S')
{
next.x = now.x+;
next.y = now.y;
}
if(graph[now.x][now.y]=='E')
{
next.x = now.x;
next.y = now.y+;
}
if(graph[now.x][now.y]=='N')
{
next.x = now.x-;
next.y = now.y;
}
next.step = now.step+;
if(check(next.x,next.y))
{
if(vis[next.x][next.y]) /// 如果被访问过了,则进入了循环
{
printf("%d step(s) before a loop of %d step(s)\n",vis[next.x][next.y]-,next.step-vis[next.x][next.y]);
return ;
}
else
{
vis[next.x][next.y] = next.step;
q.push(next);
}
}
else
{
printf("%d step(s) to exit\n",next.step-);
return;
} }
return;
} int main()
{
int t = ;
while(scanf("%d%d%d",&n,&m,&k)!=EOF&&n+m+k)
{
for(int i=; i<n; i++){
scanf("%s",graph[i]);
}
bfs();
}
return ;
}

还写了个DFS的。

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<math.h>
#include<queue>
#include<iostream>
using namespace std;
int graph[][];
int n,m,k;
int vis[][];
struct Node
{
int x,y;
int step;
}s;
bool check(int x,int y)
{
if(x<||x>=n||y<||y>=m) return false;
return true;
}
void dfs(int x,int y,int cnt){
vis[x][y] = cnt;
int nextx,nexty,step;
if(graph[x][y]==){
nextx = x;
nexty = y - ;
}
if(graph[x][y]==){
nextx = x+;
nexty = y;
}
if(graph[x][y]==){
nextx = x;
nexty = y + ;
}
if(graph[x][y]==){
nextx = x-;
nexty = y;
}
step = cnt+;
if(check(nextx,nexty)){
if(vis[nextx][nexty]){
printf("%d step(s) before a loop of %d step(s)\n",vis[nextx][nexty]-,step-vis[nextx][nexty]);
return;
}else{
dfs(nextx,nexty,step);
}
}else{
printf("%d step(s) to exit\n",step-);
}
}
int main()
{
int t = ;
while(scanf("%d%d%d",&n,&m,&k)!=EOF&&n+m+k)
{
char s[];
for(int i=; i<n; i++){
scanf("%s",s);
for(int j=;j<m;j++){
if(s[j]=='W') graph[i][j]=;
if(s[j]=='S') graph[i][j]=;
if(s[j]=='E') graph[i][j]=;
if(s[j]=='N') graph[i][j]=;
}
}
memset(vis,,sizeof(vis));
dfs(,k-,);
}
return ;
}

poj 1573(搜索)的更多相关文章

  1. 模拟 POJ 1573 Robot Motion

    题目地址:http://poj.org/problem?id=1573 /* 题意:给定地图和起始位置,robot(上下左右)一步一步去走,问走出地图的步数 如果是死循环,输出走进死循环之前的步数和死 ...

  2. POJ 1573 Robot Motion(BFS)

    Robot Motion Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 12856   Accepted: 6240 Des ...

  3. POJ 1573 Robot Motion(模拟)

    题目代号:POJ 1573 题目链接:http://poj.org/problem?id=1573 Language: Default Robot Motion Time Limit: 1000MS ...

  4. catch that cow POJ 3278 搜索

    catch that cow POJ 3278 搜索 题意 原题链接 john想要抓到那只牛,John和牛的位置在数轴上表示为n和k,john有三种移动方式:1. 向前移动一个单位,2. 向后移动一个 ...

  5. [Vjudge][POJ][Tony100K]搜索基础练习 - 全题解

    目录 POJ 1426 POJ 1321 POJ 2718 POJ 3414 POJ 1416 POJ 2362 POJ 3126 POJ 3009 个人整了一些搜索的简单题目,大家可以clone来练 ...

  6. poj 2251 搜索

    Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13923   Accepted: 5424 D ...

  7. poj 1011 搜索减枝

    题目链接:http://poj.org/problem?id=1011 #include<cstdio> #include<cstring> #include<algor ...

  8. 生日蛋糕 POJ - 1190 搜索 数学

    http://poj.org/problem?id=1190 题解:四个剪枝. #define _CRT_SECURE_NO_WARNINGS #include<cstring> #inc ...

  9. poj 2531 搜索剪枝

    Network Saboteur Time Limit: 2000 MS Memory Limit: 65536 KB 64-bit integer IO format: %I64d , %I64u ...

随机推荐

  1. 根据参数优化nginx的服务性能

    一.优化nginx服务的worker进程数 在高并发.高访问量的Web服务场景,需要事先启动好更多的nginx进程,以保证快速响应并处理大量并发用户的请求. 1).优化nginx进程对应的配置 优化n ...

  2. 《Java并发编程实战》读书笔记一 -- 简介

    <Java并发编程实战>读书笔记一 -- 简介 并发的历史 并发的历史,也是人类利用有限的资源去提高生产效率的一个的例子. 设想现在有台计算机,这台计算机具有以下的资源: 单核CPU一个 ...

  3. zookeeper伪集群(一)

    Zookeeper的安装和配置十分简单, 既可以配置成单机模式, 也可以配置成伪集群模式.集群模式. 本人将对伪集群.集群进行重点介绍: 铺垫: 1.集群必须是奇数(2N+1),伪集群和集群一致. 2 ...

  4. OpenCV中的图像形态学转换

    两个基本的形态学操作是腐蚀和膨胀.他们的变化构成了开运算,闭运算,梯度等.下面以这张图为例 1.腐蚀 这个操作会把前景物体的边界腐蚀掉. import cv2 import numpy as np i ...

  5. Java-basic-4-数据类型

    Number类 装箱:将内置数据类型作为包装类对象使用:拆箱:相反 public class test{ public static void main(String args[]) { // box ...

  6. python寻找模块的路径顺序

    >>> import sys >>> sys.path ['', '/Library/Frameworks/Python.framework/Versions/3. ...

  7. JAVA基础篇—基本数据类型

    Java8种基本数据类型:4个整型byte 1字节 8bit 范围-128~127 short 2字节 16bit 范围-2^15 -1~2^15 -1 int 4字节 32bit 范围-2^31-1 ...

  8. <原创>在PE最后一节中插入补丁程序(附代码)

    完整文件  http://files.cnblogs.com/Files/Gotogoo/在PE最后一节中插入补丁程序.zip 在PE文件最后一节中插入补丁程序,是最简单也是最有效的一种,因为PE最后 ...

  9. hdu1787 GCD Again poj 2478 Farey Sequence 欧拉函数

    hdu1787,直接求欧拉函数 #include <iostream> #include <cstdio> using namespace std; int n; int ph ...

  10. xcode8.1 autolayout 找不到 Update Frames 按钮