博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
1028 - Carl the Ant
阅读量:6432 次
发布时间:2019-06-23

本文共 5296 字,大约阅读时间需要 17 分钟。

 

Ants leave small chemical trails on the ground in order to mark paths for other ants to follow. Ordinarily these trails follow rather straight lines. But in one ant colony there is an ant named Carl, and Carl is not an ordinary ant. Carl will often zigzag for no apparent reason, sometimes crossing his own path numerous times in the process. When other ants come to an intersection, they always follow the path with the strongest scent, which is the most recent path that leads away from the intersection point.

Ants are 1 centimeter long, move and burrow at 1 centimeter per second, and follow their paths exactly (bending at right angles when moving around corners). Ants cannot cross or overlap each other. If two ants meet at the exact same instant at an intersection point, the one that has been on Carl's path the longest has the right of way; if they don't arrive at the same time at the intersection point, the ant that has been waiting the longest at the intersection will move first.

Carl burrows up from the ground to start at the origin at time 0. He then walks his path and burrows back down into the ground at the endpoint. The rest of the ants follow at regular intervals. Given the description of Carl's path and when the other ants start the path, you are to determine how long it takes the entire set of ants to finish burrowing back into the ground. All the ants are guaranteed to finish.

 

Input 

Input consists of several test cases. The first line of the input file contains a single integer indicating the number of test cases .

The input for each test case starts with a single line containing three positive integers n ( 1$ \le$n$ \le$50), m1$ \le$m$ \le$100), and d ( 1$ \le$d$ \le$100). Here, n is the number of line segments in Carl's path, m is the number of ants traveling the path (including Carl), and d is the time delay before each successive ant's emergence. Carl (who is numbered 0) starts at time 0. The next ant (ant number 1) will emerge at time d, the next at time 2d, and so on. If the burrow is blocked, the ants will emerge as soon as possible in the correct order.

Each of the next n lines for the test case consists of a unique integer pair x y ( -100$ \le$xy$ \le$100), which is the endpoint of a line segment of Carl's path, in the order that Carl travels. The first line starts at the origin (0,0) and the starting point of every subsequent line is the endpoint of the previous line.

For simplicity, Carl always travels on line segments parallel to the axes, and no endpoints lie on any segment other than the ones which they serve as an endpoint. Input line segments will only intersect orthogonally. Every pair of segments can have at most one common point. The common point will be strictly inside both segments.

 

Output 

The output for each case is described as follows:

 

 

Case C:

Carl finished the path at time t1

The ants finished in the following order:

a1a2a3...am

The last ant finished the path at time t2

 

 

Here, C is the case number (starting at 1), a1a2a3,..., am are the ant numbers in the order that they go back underground, and t1 and t2 are the times (in seconds) at which Carl and the last ant finish going underground. You should separate consecutive cases with a single blank line.

 

Sample Input 

24 7 40 42 42 2-2 24 7 20 42 42 2-2 2

 

Sample Output 

Case 1:Carl finished the path at time 13The ants finished in the following order:0 2 1 3 4 5 6The last ant finished the path at time 29Case 2:Carl finished the path at time 13The ants finished in the following order:0 4 1 5 2 6 3The last ant finished the path at time 19

 

 

#include
#include
#include
struct info{ int x,y,len,wait,dir;};const int MaxN=50;const int MaxM=100;const int MaxL=250;const int Add[4][2]={-1,0,0,1,1,0,0,-1};int N,cases,n,m,D,Rs,fin,sta,T,ex,ey;int route[MaxN*MaxL];int map[MaxL+1][MaxL+1];int ants[MaxL+1][MaxL+1][4];int list[MaxM];info ant[MaxM];void init(){ int i,j,k,t,x1,y1,x2,y2; scanf("%d%d%d",&n,&m,&D); x1=0;y1=0;Rs=0; for(i=0;i
x2) t=0; if(y1
y2) t=3; for(;x1!=x2||y1!=y2;) { route[Rs]=t; Rs++; x1+=Add[t][0]; y1+=Add[t][1]; } } fin=0;sta=0; for(i=0;i<=MaxL;i++) for(j=0;j<=MaxL;j++) for(k=0;k<4;k++) ants[i][j][k]=-1;}void work(){ bool ok; int i,j,x,y,d,p,tmp; int go[MaxM]; memset(go,0,sizeof(go)); ok=0; for(i=0;i
=0) { x=ant[i].x;y=ant[i].y; for(j=0;j<4;j++) if(ants[x][y][j]>=0) { tmp=ants[x][y][j]; if(ant[tmp].wait>ant[i].wait || (ant[tmp].wait==ant[i].wait && ant[tmp].len>ant[i].len)) { p=1;go[i]=1;ok++; break; } } } do{ ok=0; for(i=0;i
0) { d=map[ant[i].x][ant[i].y]; x=ant[i].x+Add[d][0];y=ant[i].y+Add[d][1]; if(ants[x][y][d]>=0&&go[ants[x][y][d]]==1) { go[i]=1;ok=1; continue; } } }while(ok); for(i=0;i
=0) { ant[i].len++;ant[i].wait=0; ants[ant[i].x][ant[i].y][ant[i].dir]=-1; } else if(ant[i].x>=0) ant[i].wait++; for(i=0;i
=0) { x=ant[i].x;y=ant[i].y; if(x==100&&y==100&&i!=sta-1) { ants[100][100][map[100][100]]=i+1; } d=map[x][y]; ant[i].x+=Add[d][0];ant[i].y+=Add[d][1];ant[i].dir=d; if(ant[i].x==ex&&ant[i].y==ey) { list[fin]=i; fin++; ant[i].x=-1; } else ants[ant[i].x][ant[i].y][d]=i; }}void write(){ int i; printf("Case %d:\n",cases); printf("Carl finished the path at time %d\n",ant[0].len+1); printf("The ants finished in the following order:\n"); printf("%d",list[0]); for(i=1;i

 

 

转载地址:http://ylxga.baihongyu.com/

你可能感兴趣的文章
虚拟文件系统相关结构描述【续】
查看>>
我的友情链接
查看>>
思科通配符(Cisco Wildcard Mask)
查看>>
PHP cURL快速入门
查看>>
在errpt中报E87EF1BE的解决方法(转载)
查看>>
aix chfs及mklvcopy报错的解决方法
查看>>
取消新增的constraints
查看>>
MAC OS X 使用记录
查看>>
Azure 中使用 iPerf 进行网络带宽测试
查看>>
OPTIMIZE TABLE
查看>>
flask框架+pygal+sqlit3搭建图形化业务数据分析平台
查看>>
Fedora24下MySQL开发环境搭建
查看>>
shell实战训练营Day20
查看>>
jQuery 之 TAB切换菜单
查看>>
mysql 数据库集群搭建:(二)3台CentOS-7安装Percona-XtraDB-Cluster-57集群
查看>>
Jenkins实战演练之Windows系统节点管理
查看>>
MySQL高可用架构之MHA
查看>>
1.8 nginx域名跳转
查看>>
PHP面向对象之接口编程
查看>>
使用 Docker Compose 管理多个容器实例
查看>>