博客
关于我
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索引
查看>>
Mysql索引
查看>>
mysql索引
查看>>
mysql索引
查看>>
Mysql索引,索引的优化,如何避免索引失效案例
查看>>
Mysql索引、命令重点介绍
查看>>
mysql索引、索引优化(这一篇包括所有)
查看>>
Mysql索引一篇就够了
查看>>
MySQL索引一篇带你彻底搞懂(一次讲清实现原理加优化实战,面试必问)
查看>>
MySQL索引下沉:提升查询性能的隐藏秘
查看>>
MySql索引为什么使用B+树
查看>>
MySQL索引为什么是B+树
查看>>
WARNING!VisualDDK wizard was unable to find any DDK/WDK installed on your system.
查看>>
MySQL索引介绍及百万数据SQL优化实践总结
查看>>
Mysql索引优化
查看>>
MySQl索引创建
查看>>
mysql索引创建及使用注意事项
查看>>
mysql索引创建和使用注意事项
查看>>
MySQL索引原理以及查询优化
查看>>
Mysql索引合并(index merge)导致的死锁问题
查看>>