【POJ2632】Crashing Robots
本题知识点:模拟
模拟机器人的运作过程,分别有三种功能,L 则是左转90°,R 则是右转90°,L 则是前进1格。让我们去模拟并判断它们的状态。
输入:
第一行是测试样例
第二行分别是矩形的长、宽(比如,5 4)
// 题目是讲得很清楚的
// 4
// 3
// 2
// 1
//   1 2 3 4 5
第三行分别是机器人的个数以及指令条数
接下来是先输入每个机器人的初始状态(有多少个机器人就多少行),3个数值分别对应机器人所在位置的(x, y)以及机器人所面向的方向(因为 F 指令只能让机器人向前,所以懂得东南西北是很重要的一件事)
同样,接下来有多少条指令就输入多少行,3个数值分别对应机器人的号数,对应指令,以及执行该指令的次数。
等一切输入完后就可以执行(模拟)了。
输出有三种情况
一种是撞墙的
一种是撞机器人的
剩下的是每条指令都通过并没有发生意外(撞墙或撞机器人)的
对于前两种,我们得只输出一次就可以不管后面的了,因为题目要求只输出一次情况
但如果在输入时就开始模拟的话,中断了就会影响后面的输入,所以我弄了个结构体去存执行指令
剩下的,就看自己是否细心与耐心了
我语言比较菜,所以代码写得很长,很暴力,感觉有些地方还可以写的简练一点,但由于自己懒,想快水过去好睡觉了。
数据很小。
// POJ 2632
#include<iostream>
#include<cstdio>
using namespace std;
int T;
int W, H, robot_n, t;
struct node{
    int w, h;
    char to;
}rob[102];
struct e{
    int who, re;
    char done;
}take[102];
//char pla[102][102];
void show(){
    for(int i = 1; i <= robot_n; i++){
        printf("i:%d w:%d h:%d\n", i, rob[i].w, rob[i].h);
    }
}
int main()
{
    scanf("%d", &T);
    while(T--){
        bool last = true;
        // take in
        scanf("%d %d", &W, &H);
        scanf("%d %d", &robot_n, &t);
        for(int i = 1; i <= robot_n; i++) scanf("%d %d %c", &rob[i].w, &rob[i].h, &rob[i].to);
        for(int i = 1; i <= t; i++) scanf("%d %c %d", &take[i].who, &take[i].done, &take[i].re);
        // done!
        for(int i= 1; i <= t; i++){
            int it = take[i].who;
            char bi = take[i].done;
            int num = take[i].re;
            // 转向
            if(bi == 'L'){
                num = num % 4;
                while(num > 0){
                    if(rob[it].to == 'N') rob[it].to = 'W';
                    else if(rob[it].to == 'W') rob[it].to = 'S';
                    else if(rob[it].to == 'S') rob[it].to = 'E';
                    else if(rob[it].to == 'E') rob[it].to = 'N';
                    num--;
                }
            }
            else if(bi == 'R'){
                num = num % 4;
                while(num > 0){
                    if(rob[it].to == 'N') rob[it].to = 'E';
                    else if(rob[it].to == 'E') rob[it].to = 'S';
                    else if(rob[it].to == 'S') rob[it].to = 'W';
                    else if(rob[it].to == 'W') rob[it].to = 'N';
                    num--;
                }
            }
            // 前进
            else if(bi == 'F'){
                char go = rob[it].to;
                // 北
                if(go == 'N'){
                    while(num > 0){
                        rob[it].h++;
                        // wall
                        if(rob[it].h > H){
                            printf("Robot %d crashes into the wall\n", it);
                            last = false;
                            break;
                        }
                        // robot
                        bool can = true; // 跳出遍历
                        for(int i = 1; i <= robot_n; i++){
                            if(i == it) continue;
                            if(rob[it].h == rob[i].h && rob[it].w == rob[i].w){
                                printf("Robot %d crashes into robot %d\n", it, i);
                                can = false;
                                last = false;
                                break;
                            }
                        }
                        if(!can) break;
                        num--;
                    }
                }
                // 西
                else if(go == 'W'){
                    while(num > 0){
                        rob[it].w--;
                        // wall
                        if(rob[it].w < 1){
                            printf("Robot %d crashes into the wall\n", it);
                            last = false;
                            break;
                        }
                        // robot
                        bool can = true;
                        for(int i = 1; i <= robot_n; i++){
                            if(i == it) continue;
                            if(rob[it].h == rob[i].h && rob[it].w == rob[i].w){
                                printf("Robot %d crashes into robot %d\n", it, i);
                                can = false;
                                last = false;
                                break;
                            }
                        }
                        if(!can) break;
                        num--;
                    }
                }
                // 南
                else if(go == 'S'){
                    while(num > 0){
                        rob[it].h--;
                        // wall
                        if(rob[it].h < 1){
                            printf("Robot %d crashes into the wall\n", it);
                            last = false;
                            break;
                        }
                        // robot
                        bool can = true;
                        for(int i = 1; i <= robot_n; i++){
                            if(i == it) continue;
                            if(rob[it].h == rob[i].h && rob[it].w == rob[i].w){
                                printf("Robot %d crashes into robot %d\n", it, i);
                                can = false;
                                last = false;
                                break;
                            }
                        }
                        if(!can) break;
                        num--;
                    }
                }
                // 东
                else if(go == 'E'){
                    while(num > 0){
                        rob[it].w++;
                        // wall
                        if(rob[it].w > W){
                            printf("Robot %d crashes into the wall\n", it);
                            last = false;
                            break;
                        }
                        // robot
                        bool can = true;
                        for(int i = 1; i <= robot_n; i++){
                            if(i == it) continue;
                            if(rob[it].h == rob[i].h && rob[it].w == rob[i].w){
                                printf("Robot %d crashes into robot %d\n", it, i);
                                can = false;
                                last = false;
                                break;
                            }
                        }
                        if(!can) break;
                        num--;
                    }
                }
            }
            // 中断剩余操作防止过多输出
            if(!last) break;
        }
        // 跳过 OK
        if(!last) continue;
        printf("OK\n");
    }
    return 0;
}
												
											【POJ2632】Crashing Robots的更多相关文章
- 【题解】ARC101F Robots and Exits(DP转格路+树状数组优化DP)
		
[题解]ARC101F Robots and Exits(DP转格路+树状数组优化DP) 先删去所有只能进入一个洞的机器人,这对答案没有贡献 考虑一个机器人只能进入两个洞,且真正的限制条件是操作的前缀 ...
 - 【ZOJ1003】Crashing Balloon(DFS)
		
Crashing Balloon Time Limit: 2 Seconds Memory Limit: 65536 KB On every June 1st, the Children's ...
 - 【seo】title / robots / description / canonical
		
1.title title,就是浏览器上显示的那些内容,不仅用户能看到,也能被搜索引擎检索到(搜索引擎在抓取网页时,最先读取的就是网页标题,所以title是否正确设置极其重要. 1)title一般不超 ...
 - 【HDOJ6229】Wandering Robots(马尔科夫链,set)
		
题意:给定一个n*n的地图,上面有k个障碍点不能走,有一个机器人从(0,0)出发,每次等概率的不动或者往上下左右没有障碍的地方走动,问走无限步后停在图的右下部的概率 n<=1e4,k<=1 ...
 - 【agc004e】Salvage Robots
		
题目大意 一个n*m的矩阵,矩阵内有一个出口和若干个机器人,每一步操作可以使所有的机器人向任意方向移动一格,如果机器人出了边界就爆炸.求最多可以让多少个机器人走到出口. 解题思路 发现,移动所有机器人 ...
 - 【题解】CF24D Broken Robots(收敛性)
		
[题解]CF24D Broken Robots http://codeforces.com/problemset/problem/24/D 解1(不会写,口胡的) 获得一个比较显然的转移式子 \(dp ...
 - poj2632 Crashing Robots
		
Crashing Robots Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9859 Accepted: 4209 D ...
 - POJ2632——Crashing Robots
		
Crashing Robots DescriptionIn a modernized warehouse, robots are used to fetch the goods. Careful pl ...
 - 【题解】CF#24 D-Broken Robots
		
在某次考试的时候用过的办法,懒人必备……[笑哭] 一个非常显然的 dp,我们用 \(f[i][j]\) 表示第 \(i\) 行第 \(j\) 列的格子走到最后一排的期望步数转移即为 \(f[i][j] ...
 
随机推荐
- crunch制作字典
			
安装 安装crunch sudo apt-get install crunch 语法 crunch <min> max<max> <characterset> -t ...
 - 【夯实基础】- https和http的主要区别
			
HTTPS和HTTP的区别主要如下: 1.https协议需要到ca申请证书,一般免费证书较少,因而需要一定费用. 2.http是超文本传输协议,信息是明文传输,https则是具有安全性的ssl加密传输 ...
 - ConnectionString属性(网速慢的情况下研究Connect Timeout)
			
ConnectionString 类似于 OLE DB 连接字符串,但并不相同.与 OLE DB 或 ADO 不同,如果“Persist Security Info”值设置为 false(默认值),则 ...
 - CSS揭秘(引言)
			
1.标准的制定过程 a 人员结构:W3C会员公司的成员.特邀专家.W3C工作人员 b 尽管“CSS3”非常流行,但它实际上并没有在任何规范中定义过.它实际上是指一个非正式的集合,包括CSS规范第三版再 ...
 - vue动态循环出的多个select出现过的变为disabled
			
<template> <div class="artcle"> <el-form label-width="100px" :mod ...
 - Vue项目整体架构记要
			
此文记录初次学习Vue的一些记要,可做为参考,导图有些生涩,故意不再加以修复完善,以持初心!
 - 如何使用Prometheus采集SAP ABAP Netweaver的应用日志数据
			
Prometheus是一套开源的系统监控报警框架.它启发于Google的borgmon 监控系统,由工作在 SoundCloud 的 google 前员工在 2012 年创建,作为社区开源项目进行开发 ...
 - 继 首次使用DoNetCore EFCore DbFirst 更新数据实体
			
//EFCore DB First 步骤 //第一步:Install-Package Microsoft.EntityFrameworkCore.SqlServer -version 2.1.1 // ...
 - A quick introduction to Source Insight for seamless development platform between Linux and Windows
			
前言 Source Insight是一个面向项目开发的程序编辑器和代码浏览器,它拥有内置的对C/C++, C#和Java等程序的分析.能分析源代码并在工作的同时动态维护它自己的符号数据库,并自动显示有 ...
 - CentOS Linux更改MySQL数据库目录位置
			
引言: 由于MySQL的数据库太大,默认安装的/var盘已经再也无法容纳新增加的数据,没有办法,只能想办法转移数据的目录. 下面我整理一下把MySQL从/var/lib/mysql目录下面转移到/ho ...