1028 - Carl the Ant
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
n
50), m( 1
m
100), and d ( 1
d
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
x, y
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), a1, a2, a3,..., 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
2
4 7 4
0 4
2 4
2 2
-2 2
4 7 2
0 4
2 4
2 2
-2 2
Sample Output
Case 1:
Carl finished the path at time 13
The ants finished in the following order:
0 2 1 3 4 5 6
The last ant finished the path at time 29 Case 2:
Carl finished the path at time 13
The ants finished in the following order:
0 4 1 5 2 6 3
The last ant finished the path at time 19
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
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<n;i++)
{
scanf("%d%d",&x2,&y2);
if(x1>x2) t=0;
if(y1<y2) t=1;
if(x1<x2) t=2;
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<sta;i++) if(ant[i].x<0) ok++;
for(i=0;i<sta;i++) if(ant[i].x>=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<sta;i++) if(!go[i] && ant[i].x>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<sta;i++)
if(!go[i]&&ant[i].x>=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<sta;i++)
if(!go[i]&&ant[i].x>=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<m;i++)
printf(" %d",list[i]);
printf("\n");
printf("The last ant finished the path at time %d\n",T+1);
if(cases<N)
printf("\n");
} int main()
{
int X,Y;
scanf("%d",&N);
for(cases=1;cases<=N;cases++)
{
init();
X=100;Y=100;ex=-1;ey=-1;
for(T=0;fin<m;T++)
{
if(T<Rs)
{
map[X][Y]=route[T];
X+=Add[route[T]][0];Y+=Add[route[T]][1];
if(T==Rs-1)
{
ex=X;ey=Y;
}
}
if(T%D==0&&sta<m)
{
if(ants[100][100][map[100][100]]) ants[100][100][map[100][100]]=sta;
ant[sta].x=100;ant[sta].y=100;
ant[sta].len=0;ant[sta].wait=0;ant[sta].dir=map[100][100];
sta++;
}
work();
}
write();
}
return 0;
}
1028 - Carl the Ant的更多相关文章
- Jenkins 安装的HTML Publisher Plugin 插件无法展示ant生成的JunitReport报告
最近在做基于jenkins ant junit 的测试持续集成,单独ant junit生成的junitreport报告打开正常,使用Jenkins的HTML Publisher Plugin 插件无 ...
- React中使用Ant Table组件
一.Ant Design of React http://ant.design/docs/react/introduce 二.建立webpack工程 webpack+react demo下载 项目的启 ...
- [Ant]Ant简易教程
前言 Apache Ant,是一个将软件编译.测试.部署等步骤联系在一起加以自动化的一个工具,大多用于Java环境中的软件开发.由Apache软件基金会所提供. Ant是纯Java语言编写的,所以具有 ...
- jenkins / ant / jmeter 持续集成接口自动化
1. 将 jmeter 脚本放在/var/lib/jenkins/workspace/Jmeter_auto/jmxpath路径下 2. 点击http://jk.facebank.net.cn/job ...
- Maven与Ant比较
Maven与Ant比较 0 « 上一篇:Jenkins学习三:介绍一些Jenkins的常用功能» 下一篇:Jenkins学习四:Jenkins 邮件配置 posted @ 2015-03-25 16: ...
- 一.Jmeter+Ant+Jenkins搭建持续集成接口性能自动化测试
微创新作品信息 1)微创新作品描述 A.为什么诞生: 1. 接口测试是测试系统组件间接口的一种测试.接口测试主要用于检测外部系统与系统之间以及内部各个子系统之间的交互点.测试的重点是要检查数据的交换, ...
- Eclipce结合Ant进行编译、打包、传输、运行
注意: 用Ant构建时,build path只能是单级的,如默认的src,如果是类似basePath/jsr253这样的话,运行Ant build时会报错,说找不到jsr253. (此文讲述的是以an ...
- ant 使用指南
一.概述 ant 是一个将软件编译.测试.部署等步骤联系在一起加以自动化的一个工具,大多用于Java环境中的软件开发.在实际软件开发中,有很多地方可以用到ant. 开发环境: System:Windo ...
- 在Eclipse中集成Ant配置
提要:本文将向你展示如何使用Eclipse设置为Ant所用的属性值和环境变量,并简要分析如何配置Ant编辑器以便从Eclipse内部操作Ant文件. 一. 修改Ant Classpath 在使用一个可 ...
随机推荐
- 代码以兼容高亮方式发布.xml
函数名: abort 功 能: 异常终止一个进程 用 法: void abort(void); 程序例: #include <stdio.h> #include <st ...
- SQL 教程学习进度备忘
书签:跳过:另外跳过的内容有待跟进 __________________ 学习资源:W3School. _________________ 跳过的内容: 1. “SQL select”底部的“ AD ...
- 说说Python 中的文件操作 和 目录操作
我们知道,文件名.目录名和链接名都是用一个字符串作为其标识符的,但是给我们一个标识符,我们该如何确定它所指的到底是常规文件文件名.目录名还是链接名呢?这时,我们可以使用os.path模块提供的isfi ...
- ListView蛮好用
知识点如下: 1. ListView的基本用法 2. ArrayAdapter和SimpleAdapter的用法 3. OnScrollListener 和 OnItemClickListener 4 ...
- 《Linux命令行与shell脚本编程大全》 第一、二章 学习笔记
第一章:初识Linux shell Linux内核负责以下4个主要功能: 1.系统内存管理 2.软件程序管理 3.硬件设备管理 4.文件系统管理 1.系统内存管理 内核不仅管理服务器上的可用物理内存, ...
- gcc命令行详解
介绍] ----------------------------------------- 常见用法: GCC 选项 GCC 有超过100个的编译选项可用. 这些选项中的许多你可能永远都不会用到, 但 ...
- SQL SERVER 2008 R2 SP3 发布
今晚上刚发现,微软很低调啊 下载地址:http://www.microsoft.com/zh-cn/download/details.aspx?id=44271 整合SP3的Express系列版本还没 ...
- TopFreeTheme精选免费模板【20130617】
今天给大家推荐8款最新的WordPress和Joomla主题,它们绝大部分都是屏幕自适应主题,Mobile相当友好.如果你喜欢它们,就赶快收藏起来吧. Spacing – 来自Themeforest的 ...
- Hadoop2.2.0 自动切换HA环境搭建
自动切换的HA,比手动切换HA集群多了一个zookeeper集群 机器分配: zookeeper:hadoop4,hadoop5,hadoop6 namenode:hadoop4,hadoop5 da ...
- Linux下静态库生成和使用
Linux下静态库生成和使用 一.静态库概念 1.库是预编译的目标文件(object files)的集合,它们可以被链接进程序.静态库以后缀为”.a”的特殊的存档(archive file)存储. ...