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 ...
随机推荐
- NOIP模拟赛 篮球比赛2
篮球比赛2(basketball2.*) 由于Czhou举行了众多noip模拟赛,也导致放学后篮球比赛次数急剧增加.神牛们身体素质突飞猛进,并且球技不断精进.这引起了体育老师彩哥的注意,为了给校篮球队 ...
- poj1654 Area
题目描述: vjudge POJ 题解: 本以为是水题结果是神题 计算几何求多边形面积. 考虑到结果一定是整数或者整数/2,我们应该用long long 来存…… 用double会死…… 还有日常只能 ...
- Linux 用户管理(三)
一.userdel --delete a user account and related files -r --remove 删除用户及家目录 二.id --print real and effe ...
- PHP获取文件夹内所有文件包括子目录文件的名称或路径
/* * new getFile($_dir[,$_emptyDir,$_fileType]); * @parma $_dir 是目录名称 * @parma $_emptyDir 是否获取空文件夹,选 ...
- ASP.NET Web网站中App_Code文件夹的作用及使用场景
原文地址:Web Site项目和ASP.NET Web Application中App_Code文件夹的作用作者:宾的宾 我现在要建一个ASP.NET的网站了,不难吧,开始动手.如下图: 这种方法建立 ...
- BZOJ 2465: [中山市选2009]小球
难度在于读题 #include<cstdio> #include<algorithm> using namespace std; int a[1000005]; struct ...
- python 提交form-data之坑
#coding=utf-8 import requests from requests_toolbelt import MultipartEncoder #requests库上传 files = {& ...
- LoadRunner11使用方法以及注意点收集
一:安装loadrunner http://jingyan.baidu.com/article/f7ff0bfc1cc82c2e26bb13b7.html http://www.cnblogs.com ...
- Apache 根据不同的端口 映射不同的站点
以前,在本地新建个项目,总是在Apache的htdocs目录下新建个项目目录,今年弄了个别人写好的网站源码,因为该系统的作者假定网站是放在根目录的,放在二级目录下会出错.所以无奈,只能想办法,根据端口 ...
- Mongodb 删除记录里的某个字段
//例如要把User表中address字段删除 db.User.update({},{$unset:{'address':''}},false, true)