POJ_1573 Robot Motion

本文介绍了一道编程题,通过模拟机器人在指令网格中的移动来找出其退出路径或循环路径。使用了递归方法,并借助父指针来判断循环的开始。
问题来源:[url]http://poj.org/problem?id=1573[/url]

Robot Motion
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 7083 Accepted: 3402

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)


题目大意:模拟一个机器人在特定的棋盘中行走,每个棋盘的格子规定好了机器人移动的方向,每次移动只能走一个格子,问机器人退出棋盘时走的步数,如果不能退出棋盘,输出机器人进入循环前走的步数和每次循环中包含的步数。

分析:简单模拟题,注意第二种情况,即发生循环的时候如何得到循环前的步数,这里想到用父指针。

代码:

#include<stdio.h>
#include<string.h>
#define SIZE 12

typedef int point[2];
point father[SIZE][SIZE];
int state[SIZE][SIZE];
char maze[SIZE][SIZE];
int R,C,S;

void solve(int r,int c,int lr,int lc,int dis)
{
if(r<=0||r>R||c<=0||c>C)
{
printf("%d step(s) to exit\n",dis-1);
return;
}
if(state[r][c]!=0)
{
printf("%d step(s) before a loop of %d step(s)\n",state[father[r][c][0]][father[r][c][1]],dis-state[father[r][c][0]][father[r][c][1]]-1);
return;
}
father[r][c][0] = lr;
father[r][c][1] = lc;
state[r][c] = dis;
switch(maze[r][c])
{
case 'N':solve(r-1,c,r,c,dis+1);break;
case 'S':solve(r+1,c,r,c,dis+1);break;
case 'E':solve(r,c+1,r,c,dis+1);break;
case 'W':solve(r,c-1,r,c,dis+1);break;
}
return;
}

int main()
{
//freopen("datain.txt","r",stdin);
//freopen("dataout.txt","w",stdout);
while(scanf("%d%d%d%*c",&R,&C,&S),R||C||S)
{
for(int i=1;i<=R;i++)
gets(maze[i]);
memset(state,0,sizeof(state));
for(int i=1;i<=R;i++)
{
for(int j=C;j>0;j--)
{
maze[i][j] = maze[i][j-1];
}
}

solve(1,S,0,S,1);
}
return 0;
}

源码地址: https://pan.quark.cn/s/3916362e5d0a 在C#编程平台下,构建一个曲线编辑器是一项融合了图形用户界面(GUI)构建、数据管理及数学运算的应用开发任务。 接下来将系统性地介绍这个曲线编辑器开发过程中的核心知识点:1. **定制曲线面板展示数据曲线**: - 控件选用:在C#的Windows Forms或WPF框架中,有多种控件可用于曲线呈现,例如PictureBox或用户自定义的UserControl。 通过处理重绘事件,借助Graphics对象执行绘图动作,如运用DrawCurve方法。 - 数据图形化:通过线性或贝塞尔曲线连接数据点,以呈现数据演变态势。 这要求掌握直线与曲线的数学描述,例如两点间的直线公式、三次贝塞尔曲线等。 - 坐标系统与缩放比例:构建X轴和Y轴,设定坐标标记,并开发缩放功能,使用户可察看不同区间内的数据。 2. **在时间轴上配置多个关键帧数据**: - 时间轴构建:开发一个时间轴组件,显示时间单位刻度,并允许用户在特定时间点设置关键帧。 时间可表现为连续形式或离散形式,关键帧对应于时间轴上的标识。 - 关键帧维护:利用数据结构(例如List或Dictionary)保存关键帧,涵盖时间戳和关联值。 需考虑关键帧的添加、移除及调整位置功能。 3. **调整关键帧数据,通过插值方法获得曲线**: - 插值方法:依据关键帧信息,选用插值方法(如线性插值、样条插值,特别是Catmull-Rom样条)生成平滑曲线。 这涉及数学运算,确保曲线在关键帧之间无缝衔接。 - 即时反馈:在编辑关键帧时,即时刷新曲线显示,优化用户体验。 4. **曲线数据的输出**: - 文件类型:挑选适宜的文件格式存储数据,例如XML、JSON或...
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值