Java实现算法竞赛入门经典例题-蚂蚁
2024-10-09 05:53:22
问题描述
一根长度为L厘米的木棍上有n只蚂蚁,每只蚂蚁要么朝左爬,要么朝右爬,速度为1厘米/秒。
当两只蚂蚁相撞时,二者同时掉头(掉头时间忽略不计)。
给出每只蚂蚁的初始位置和朝向,计算T秒之后每只蚂蚁的位置。
输入格式
输入的第一行为数据组数。每组数据的第一行为3个正整数L, T, n(0≤n≤10 000);以下n行每行描述一只蚂蚁的初始位置,
其中,整数x为蚂蚁距离木棍左端的距离(单位:厘米),字母表示初始朝向(L表示朝左,R表示朝右)。
输出格式
对于每组数据,输出n行,按输入顺序输出每只蚂蚁的位置和朝向(Turning表示正在碰撞)。
在第T秒之前已经掉下木棍的蚂蚁(正好爬到木棍边缘的不算)输出Fell off。
样例输入1
2
10 1 4
1 R
5 R
3 L
10 R
10 2 3
4 R
5 L
8 R
样例输出1
Case #1:
2 Turning
6 R
2 Turning
Fell off
Case #2:
3 L
6 R
10 R
PS:
当两只蚂蚁相撞我可以当作两只蚂蚁穿过去
虽然我再不停的转头,但是我的相对位置一直没变
我最开始掉下去得永远是最靠边得两只
package 第七次模拟;
import java.util.Arrays;
import java.util.Scanner;
public class Demo2蚂蚁 {
public static class Node implements Comparable<Node> {
int id;// 输入顺序
int location;// -1,0,1
int p;// 位置
@Override
public int compareTo(Node o) {
// TODO 自动生成的方法存根
if (this.p > o.p) {
return 1;
} else if (this.p < o.p) {
return -1;
}
return 0;
}
}
public static Node[] start;
public static Node[] end;
public static int[] order;
public static String[] loca = { "L", "Turning", "R" };;
public static void main(String[] args) {
int num=1;
Scanner sc = new Scanner(System.in);
int count = sc.nextInt();
while (count-- > 0) {
int l = sc.nextInt();
int t = sc.nextInt();
int n = sc.nextInt();
start = new Node[n];
end = new Node[n];
order = new int[n];
for (int i = 0; i < n; i++) {
start[i] = new Node();
start[i].p = sc.nextInt();
String c = sc.next();
if (c.equals("L"))
start[i].location = -1;
else
start[i].location = 1;
start[i].id = i;
end[i] = new Node();
end[i].id = 0;
end[i].p = start[i].p + t * start[i].location;
end[i].location = start[i].location;
}
Arrays.sort(start);
Arrays.sort(end);
for (int j = 0; j < n; j++) {
order[start[j].id] = j;
}
for (int j = 0; j < n - 1; j++) {
if (end[j].p == end[j + 1].p) {
end[j].location = 0;
end[j + 1].location = 0;
}
}
System.out.println("Case #"+ num++ +":");
for (int i = 0; i < n; i++) {
int temp = order[i];
if (end[temp].p > l || end[temp].p < 0) {
System.out.println("Fell off\n");
} else {
System.out.println(end[temp].p + " " + loca[end[temp].location + 1]);
}
}
}
}
}
最新文章
- Java 枚举类的基本使用
- PHP+Mysql+easyui点击左侧tree菜单对应表名右侧动态生成datagrid加载表单数据(二)
- python无意中发现的
- vs 2012 InstallShield Limited Edition Project 打包windows服务解析
- Java 集合系列 08 Map架构
- 1009-2的N次方
- struts2中的标签“# ”,“%{ }”,“%{# }”
- hdu3397 Sequence operation
- C#并行和多线程编程
- css和css3学习
- Django自定义过滤器中is_safe和need_autoescape两个参数的理解
- LeetCode--032--最长有效括号(java)
- Tests for Variances
- VS2010 SP1安装失败之”此计算机的状态不支持此安装“()
- [转]stetho使用介绍
- 【安装】Mysql在Linux上安装
- HDOJ-1124 Factorial 数论
- Vivado中xilinx_BRAM IP核使用
- css两列等高布局
- python基础===flask使用整理(转)
热门文章
- Java中Error和Exception的异同以及运行时异常(Runtime exception)与检查型异常(checked exception)的区别
- Pytorch使用分布式训练,单机多卡
- 【蓝桥杯C组】备赛基础篇之高精度算法
- android学习流程确立
- Dynamics 365 联系人Contact的快速创建窗体,如何知道父窗体是哪个实体,通过window.top.parent.Xrm.Page.getUrl()可以知道父窗体的URL
- 使用基于MVC2模式创建新闻网站
- iscroll在谷歌浏览器中bug
- PAT-1132 Cut Integer (整数分割)
- 蓝桥杯 试题 历届试题 填字母游戏 博弈+dfs剪枝
- Pandas读取文件报错UnicodeDecodeError: &#39;utf-8&#39; codec can&#39;t decode byte 0xb6 in position 0: invalid start byte