模拟 --- 搜索模拟

Robot
Motion










Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 10130   Accepted: 4932

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)

【题目来源】

Mid-Central
USA 1999

http://poj.org/problem?id=1573

【题目大意】
给定一个网格,每个格子上都有相关的行走方向。玩过“管道工人”这个小游戏的人就很好理解了,这个和那个原理是一样的。

很水的题,一遍AC,只要思路清晰。


#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<bitset>
#define MAX 1000
using namespace std;

struct Node
{
char c;
int step;
bool vis;
};
Node Map[MAX][MAX];
int step1;

int pa_x,pa_y; //当前结点的前驱

void make_Map(int n,int m)
{
int i,j;
for(i=0;i<=n+1;i++)
{
for(j=0;j<=m+1;j++)
{
Map[i][j].step=0;
Map[i][j].vis=false;
}
}
for(i=0;i<=n+1;i++)
Map[i][0].c=Map[i][m+1].c=‘Q‘;
for(i=0;i<=m+1;i++)
Map[0][i].c=Map[n+1][i].c=‘Q‘;
}

int go(int n,int m)
{
int i,j;
if(Map[n][m].c==‘Q‘)
{
printf("%d step(s) to exit\n",step1);
return 1;
}
if(Map[n][m].vis)
{
printf("%d step(s) before a loop of %d step(s)\n",Map[n][m].step,Map[pa_x][pa_y].step+1-Map[n][m].step);
return true;
}
Map[n][m].step=step1++;
Map[n][m].vis=true;
pa_x=n;
pa_y=m;
switch(Map[n][m].c)
{
case ‘N‘:go(n-1,m);break;
case ‘E‘:go(n,m+1);break;
case ‘S‘:go(n+1,m);break;
case ‘W‘:go(n,m-1);break;
}
return false;
}

int main()
{
int n,m,v;
while(scanf("%d%d%d",&n,&m,&v),n+m+v)
{
getchar();
step1=0;
make_Map(n,m);
int i,j;
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
scanf("%c",&Map[i][j].c);
}
getchar();
}
go(1,v);
}
return 0;
}

模拟 --- 搜索模拟

时间: 2024-10-02 01:00:20

模拟 --- 搜索模拟的相关文章

组队赛#1 解题总结 ZOJ 3803 YY&#39;s Minions (DFS搜索+模拟)

YY's Minions Time Limit: 2 Seconds      Memory Limit: 65536 KB Despite YY's so much homework, she would like to take some time to play with her minions first. YY lines her minions up to an N*M matrix. Every minion has two statuses: awake or asleep. W

poj 3087 Shuffle&#39;m Up (模拟搜索)

Shuffle'm Up Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5953   Accepted: 2796 Description A common pastime for poker players at a poker table is to shuffle stacks of chips. Shuffling chips is performed by starting with two stacks of

【BZOJ4524】[Cqoi2016]伪光滑数 堆(模拟搜索)

[BZOJ4524][Cqoi2016]伪光滑数 Description 若一个大于1的整数M的质因数分解有k项,其最大的质因子为Ak,并且满足Ak^K<=N,Ak<128,我们就称整数M为N-伪光滑数.现在给出N,求所有整数中,第K大的N-伪光滑数. Input 只有一行,为用空格隔开的整数N和K 2 ≤ N ≤ 10^18, 1 ≤ K ≤ 800000,保证至少有 K 个满足要求的数 Output 只有一行,为一个整数,表示答案. Sample Input 12345 20 Sample

MFC中热键&模拟键盘&模拟鼠标的使用

1. 热键的使用 热键不用了的话一定要卸载,否则下次启动时会被占用. 函数原型 注册函数 BOOL RegisterHotKey( HWND hWnd, // handle to window int id, // hot key identifier UINT fsModifiers, // key-modifier options UINT vk // virtual-key code ); hWnd------窗口句柄: id------热键的标识:(如果是exe 这个标识的范围就在0-4

火灾动态模拟消防模拟软件Thunderhead.Engineering.PyroSim.v2015.3.0810.Win64 1CD

火灾动态模拟消防模拟软件Thunderhead.Engineering.PyroSim.v2015.3.0810.Win64 1CD消防员3D立体定位追踪系统Seer3D v2.10 1CDPyroSim对于火灾动态模拟器(FDS)的图形用户界面.它被用来创建模拟火灾在火灾的准确预测烟气运动,温度和浓度的毒素.PyroSim为FDS和Smokeview的一个图形用户界面.一些PyroSim的主要功能包括:- 新的3D几何体创建和编辑工具.- 集成执行官方NIST版本FDS和Smokeview的的

驱动级模拟驱动级模拟:直接读写键盘的硬件端口!

驱动级模拟驱动级模拟:直接读写键盘的硬件端口! 有一些使用DirectX接口的游戏程序,它们在读取键盘操作时绕过了windows的消息机制,而使用DirectInput.这是因为有些游戏对实时性控制的要求比较高,比如赛车游戏,要求以最快速度响应键盘输入.而windows消息由于是队列形式的,消息在传递时会有不少延迟,有时1秒钟也就传递十几条消息,这个速度达不到游戏的要求.而DirectInput则绕过了windows消息,直接与键盘驱动程序打交道,效率当然提高了不少.因此也就造成,对这样的程序无

Codeforces Good Bye 2016 D 模拟搜索?

给出烟花的爆炸方式和爆炸次数 问最后有多少个格子会被炸到 如果dfs的话会超时... 利用模拟每一层来搜索..? 思想就是一开始有一个爆炸点向上 然后模拟完第一段 会产生一个爆炸点 朝两个方向 就用vector来存 每一层都处理一段的爆炸点 产生新一段的爆炸点 因为5*30=150 所以图建300就可以了 300 * 300 * 30的时间复杂度 但是常数很大..不过无所谓啦.. 需要注意的是 一个爆炸点可能会同时出现两次朝同一个方向开始爆炸的烟花 这个是没有意义的 所以拿一个数组来记录 不然最

Kilani and the Game-吉拉尼的游戏 CodeForce#1105d 模拟 搜索

题目链接:Kilani and the Game 题目原文 Kilani is playing a game with his friends. This game can be represented as a grid of size ??×??, where each cell is either empty or blocked, and every player has one or more castles in some cells (there are no two castle

P1312 Mayan游戏 [模拟][搜索]

P1312 Mayan游戏 这道题跟斗地主都是大模拟啊!稍微挂一点可能就爆零了! 图肯定能存,直接二维数组扔进去即可. 然后套dfs模板,搜索就先照那样搜. 核心操作有两个:一个是判断那些块可以消掉,一个是把那些可以消的消了. 第一个操作的核心代码是这样的: for(int i = 1; i <= 5; i++) { for(int j = 1; j <= 7; j++) { if(G[i][j] && i - 1 >= 1 && i + 1 <=