BNUoj 50401 Right turn 转向问题
来源:互联网 发布:java三年工作经验工资 编辑:程序博客网 时间:2024/06/02 04:07
J. Right turn
Time Limit: 1000ms
Memory Limit: 65536KB
64-bit integer IO format: %lld Java class name:Main SubmitStatus
frog is trapped in a maze. The maze is infinitely large and divided into grids. It also consists ofn obstacles, where the i -th obstacle lies in grid (xi,yi) .
frog is initially in grid (0,0) , heading grid (1,0) . She moves according to The Law of Right Turn: she keeps moving forward, and turns right encountering a obstacle.
The maze is so large that frog has no chance to escape. Help her find out the number of turns she will make.
Input
The input consists of multiple tests. For each test:
The first line contains 1 integer n (0≤n≤103 ). Each of the following n lines contains 2 integers xi,yi . (|xi|,|yi|≤109,(xi,yi)≠(0,0) , all (xi,yi) are distinct)
Output
For each test, write 1 integer which denotes the number of turns, or ‘‘-1′′ if she makes infinite turns.
Sample Input
21 00 -110 141 00 10 -1-1 0
Sample Output
20-1
SubmitStatus
此题的题意是,在一个无穷大的图中 ,输入一个n个障碍的坐标,,没遇到障碍,就会一直笔直的走,遇到障碍就向右转向.
判断一共需要转几次方向.
转向无数次时输出-1.(一直转圈)
#include<iostream>#include<cstring>#include<cstdlib>#include<cstdio>#define INF 0x3f3f3f3fusing namespace std;struct node{int x,y;}p[10000];int vis[10000][4];int num=0,n;int PAN(){ memset(vis,0,sizeof(vis)); int x0=0,y0=0,dir=0,x1,y1,biao; while(1) { if(dir==0) { biao=-1; x1=INF,y1=y0; for(int i=0;i<n;i++){if(p[i].y==y1&&p[i].x>x0&&p[i].x<x1){x1=p[i].x;biao=i;}}if(biao==-1) //在此方向的前方没有障碍return num;if(vis[biao][dir]) //访问过了说明重复了return -1;x0=x1-1; } else if(dir==1) { biao=-1; x1=x0,y1=-INF; for(int i=0;i<n;i++){if(p[i].x==x1&&p[i].y<y0&&p[i].y>y1){y1=p[i].y;biao=i;}}if(biao==-1)return num;if(vis[biao][dir])return -1;y0=y1+1; } else if(dir==2) { biao=-1; x1=-INF,y1=y0; for(int i=0;i<n;i++){if(p[i].y==y1&&p[i].x<x0&&p[i].x>x1){x1=p[i].x;biao=i;}}if(biao==-1)return num;if(vis[biao][dir])return -1;x0=x1+1; } else if(dir==3) { biao=-1; x1=x0,y1=INF; for(int i=0;i<n;i++){if(p[i].x==x1&&p[i].y>y0&&p[i].y<y1){y1=p[i].y;biao=i;}}if(biao==-1)return num;if(vis[biao][dir])return -1;y0=y1-1; } num++; vis[biao][dir]=1; //访问过的就标记 dir=(dir+1)%4; //四个方向遍历 }}int main(){while(~scanf("%d",&n)){num=0;for(int i=0;i<n;i++){scanf("%d%d",&p[i].x,&p[i].y);}printf("%d\n",PAN());}}
1 0
- BNUoj 50401 Right turn 转向问题
- 三分 Problem 1005 Turn the corner 车转向问题
- HDU 3912 Turn Right
- hdu3912 Turn Right
- Right turn bunoj
- SCU4445-Right turn
- 英语练习105 Turn right
- HDU 3912 Turn Right(模拟)
- SCU-4445 Right turn(模拟)
- HDU 3912 Turn Right 题解
- 棋盘问题 BNUOJ 1440
- HDU-3912 Turn Right 简单搜索
- ACM篇:HDU 3912 -- Turn Right
- 页面转向问题
- Javascript转向问题
- 页面转向问题
- Struts的转向问题
- Js中的转向问题
- CSS轮廓,突出元素
- poj 2068 Nim 博弈论dp
- SCU 4438 Censor (KMP)
- JavaScript跨域总结与解决办法
- python的timer带参数传递
- BNUoj 50401 Right turn 转向问题
- ccs框模型与可视化格式模型
- 关于APP中,广告栏实现目视循环播放的效果
- Linux<一>:Linux启动及登陆
- NOIP快速幂复习
- C语言之进制、位运算符、数组
- POJ 3335 Rotating Scoreboard (半平面内核判断)
- Android 全局获取Context(随时随地的使用Context)
- Ubuntu下useradd使用笔记