poj 1573(搜索)
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
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
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
#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(搜索)的更多相关文章
- 模拟 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 ...
- catch that cow POJ 3278 搜索
catch that cow POJ 3278 搜索 题意 原题链接 john想要抓到那只牛,John和牛的位置在数轴上表示为n和k,john有三种移动方式:1. 向前移动一个单位,2. 向后移动一个 ...
- [Vjudge][POJ][Tony100K]搜索基础练习 - 全题解
目录 POJ 1426 POJ 1321 POJ 2718 POJ 3414 POJ 1416 POJ 2362 POJ 3126 POJ 3009 个人整了一些搜索的简单题目,大家可以clone来练 ...
- poj 2251 搜索
Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13923 Accepted: 5424 D ...
- poj 1011 搜索减枝
题目链接:http://poj.org/problem?id=1011 #include<cstdio> #include<cstring> #include<algor ...
- 生日蛋糕 POJ - 1190 搜索 数学
http://poj.org/problem?id=1190 题解:四个剪枝. #define _CRT_SECURE_NO_WARNINGS #include<cstring> #inc ...
- poj 2531 搜索剪枝
Network Saboteur Time Limit: 2000 MS Memory Limit: 65536 KB 64-bit integer IO format: %I64d , %I64u ...
随机推荐
- [bzoj]1003: [ZJOI2006]物流运输
Description 物流公司要把一批货物从码头A运到码头B.由于货物量比较大,需要n天才能运完.货物运输过程中一般要转停好几个码头.物流公司通常会设计一条固定的运输路线,以便对整个运输过程实施严格 ...
- LeetCode945-使数组唯一的最小增量
问题:使数组唯一的最小增量 给定整数数组 A,每次 move 操作将会选择任意 A[i],并将其递增 1. 返回使 A 中的每个值都是唯一的最少操作次数. 示例 1: 输入:[1,2,2] 输出:1 ...
- 创建 Django 步骤
1.创建项目 django-admin startproject 项目名称 2.创建APP python manage.py startapp app名称 3.修改settings.py文件 3.1设 ...
- STM32CUBEMX入门学习笔记2:关于STM32芯片使用内部flash
找到正点原子的官网,下载他的HAL库:http://www.openedv.com/thread-109778-1-1.html 找到此例程,并打开其工程文件. 找到此文件,复制到自己工程里 复制到自 ...
- 笔记-python-centos环境下安装配置
笔记-python-centos环境下安装配置 1. 准备工作 环境准备 centos6.5 mini,已有python 2.6.6 下载源码包 Python官网下载Gzipped sour ...
- 请求报文&响应报文
转自黑马程序员视频教程
- 如何理解redo和undo的作用
目录 如何理解redo和undo的作用 redo undo UNDO和REDO的区别 如何理解redo和undo的作用 redo 重做日志(redo)包含所有数据产生的历史改变记录,是oracle在线 ...
- 【USACO09FEB】 庙会班车 Fair Shuttle 贪心+线段树
Although Farmer John has no problems walking around the fair to collect prizes or see the shows, his ...
- JS手风琴特效
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>& ...
- session的工作原理、django的超时时间设置及session过期判断
1.session原理 cookie是保存在用户浏览器端的键值对 session是保存在服务器端的键值对 session服务端中存在的数据为: session = { 随机字符串1:{ 用户1的相关信 ...