博客
关于我
Number Game( ZOJ - 3180,思维 + 逆推)
阅读量:251 次
发布时间:2019-03-01

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

一.题目链接:

二.题目大意:

六个数 a,b,c,x,y,z.

每次可进行一次操作,选择一个数,赋值为剩下的两个数相加  - 1.

问是否可以将 x,y,z 转变为 a,b,c. (无序)

三.分析:

正推的话会炸掉.

如果逆推,注意操作的特点.

假设 a,b,c 升序

易得 c == a + b - 1.

则可得上一状态为 a,b, b - a - 1.

网上一些其他的 AC 代码貌似没有考虑全情况,居然过了!woccccc,这么暴力都可以!!!orzzz

比如当 x,y,z 为(1 1 1)或(0 0 0)时会无限循环下去,这里要特判一下.

四.代码实现:

#include 
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define eps 1e-6#define PI acos(-1.0)#define ll long long intusing namespace std;int a[3];int b[3];int c[3][3];bool check(){ for(int i = 0; i < 3; ++i) { bool flag = 1; for(int j = 0; j < 3; ++j) { if(a[j] != c[i][j]) { flag = 0; break; } } if(flag) return 1; } return 0;}bool fun(){ bool flag; sort(a, a + 3); sort(b, b + 3); flag = 1; for(int i = 0; i < 3; ++i)///若起初就相等 { if(a[i] != b[i]) flag = 0; } if(flag) return 1; c[0][0] = b[1] + b[2] - 1, c[0][1] = b[1], c[0][2] = b[2];///b的3种下一状态 c[1][0] = b[0] + b[2] - 1, c[1][1] = b[0], c[1][2] = b[2]; c[2][0] = b[1] + b[0] - 1, c[2][1] = b[1], c[2][2] = b[0]; sort(*c, *(c + 1)), sort(*(c + 1), *(c + 2)), sort(*(c + 2), *(c + 3)); flag = 1; for(int i = 0; i < 3; ++i)///操作之后,没有增加的项 { for(int j = 0; j < 3; ++j) { if(c[i][j] > b[j]) flag = 0; } } if(flag) return 0; int cnt = 0; while(1) { cnt++; sort(a, a + 3); if(check()) return 1; else if(a[0] < b[0]) return 0; else a[2] = a[1] - a[0] + 1; }}int main(){ int T; scanf("%d", &T); while(T--) { for(int i = 0; i < 3; ++i) scanf("%d", &a[i]); for(int i = 0; i < 3; ++i) scanf("%d", &b[i]); bool flag = fun(); if(flag) printf("Yes\n"); else printf("No\n"); } return 0;}

 

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

你可能感兴趣的文章
MySQL - 4种基本索引、聚簇索引和非聚索引、索引失效情况、SQL 优化
查看>>
MySQL - ERROR 1406
查看>>
mysql - 视图
查看>>
MySQL - 解读MySQL事务与锁机制
查看>>
MTTR、MTBF、MTTF的大白话理解
查看>>
mt_rand
查看>>
mysql -存储过程
查看>>
mysql /*! 50100 ... */ 条件编译
查看>>
mudbox卸载/完美解决安装失败/如何彻底卸载清除干净mudbox各种残留注册表和文件的方法...
查看>>
mysql 1264_关于mysql 出现 1264 Out of range value for column 错误的解决办法
查看>>
mysql 1593_Linux高可用(HA)之MySQL主从复制中出现1593错误码的低级错误
查看>>
mysql 5.6 修改端口_mysql5.6.24怎么修改端口号
查看>>
MySQL 8.0 恢复孤立文件每表ibd文件
查看>>
MySQL 8.0开始Group by不再排序
查看>>
mysql ansi nulls_SET ANSI_NULLS ON SET QUOTED_IDENTIFIER ON 什么意思
查看>>
multi swiper bug solution
查看>>
MySQL Binlog 日志监听与 Spring 集成实战
查看>>
MySQL binlog三种模式
查看>>
multi-angle cosine and sines
查看>>
Mysql Can't connect to MySQL server
查看>>