博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
poj 3185 The Water Bowls 反转(开关)
阅读量:4114 次
发布时间:2019-05-25

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

 
有20个碗排成一排,有些碗口朝上,有些碗口朝下。每次可以反转其中的一个碗,但是在反转该碗时,该碗左右两边的碗也跟着被反转(如果该碗为边界上的碗,则只有一侧的碗被反转)。求最少需要反转几次,可以使得所有碗口均朝上。
#include #include 
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define MM(a) memset(a,0,sizeof(a)) typedef long long ll; typedef unsigned long long ULL; const int mod = 1000000007; const int inf = 0x3f3f3f3f; using namespace std; int a1[25], a2[25]; int solve() { int ans1= 0,ans2=0; memcpy(a2, a1, sizeof(a1)); for (int i = 2; i <= 20;i++) if (a1[i - 1] % 2 == 1) { a1[i]++; a1[i + 1]++; ans1++; } if (a1[20] % 2 == 1) ans1 = inf; ans2++; a2[1] = 1 - a2[1]; a2[2] = 1 - a2[2]; for (int i = 2; i <= 20;i++) if (a2[i-1]%2==1) { a2[i]++; a2[i + 1]++; ans2++; } if (a2[20] % 2 == 1) ans2 = inf; return ans1 < ans2 ? ans1: ans2; } int main() { memset(a1, 0, sizeof(a1)); memset(a2, 0, sizeof(a2)); while (~scanf("%d",&a1[1])) { for (int i = 2; i <= 20; i++) scanf("%d", &a1[i]); printf("%d\n", solve()); } return 0; }
分析:反转问题最关键的是枚举最初状态,因为后面的状态都可以由前一状态推出,而最初状态
是需要枚举的,,因为题意中后面的状态可以影响到前面的状态的,故最初状态只能枚举确定;
wa代码:
#include #include 
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define MM(a) memset(a,0,sizeof(a)) typedef long long ll; typedef unsigned long long ULL; const int mod = 1000000007; const int inf = 0x3f3f3f3f; using namespace std; int a[25],f[25]; int solve() { int ans = 0; memset(f, 0, sizeof(f)); for (int i = 1; i <= 18;i++) if ((a[i] + f[i]) % 2 == 1) { f[i + 1]++; f[i + 2]++; ans++; } if ((a[19] + f[19]) % 2 == 1) ans++; return ans; } int main() { for (int i = 1; i <= 20; i++) scanf("%d", &a[i]); printf("%d\n", solve()); return 0; }

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

你可能感兴趣的文章
vue项目打包后无法运行报错空白页面
查看>>
Vue 解决部署到服务器后或者build之后Element UI图标不显示问题(404错误)
查看>>
element-ui全局自定义主题
查看>>
facebook库runtime.js
查看>>
vue2.* 中 使用socket.io
查看>>
openlayers安装引用
查看>>
js报错显示subString/subStr is not a function
查看>>
高德地图js API实现鼠标悬浮于点标记时弹出信息窗体显示详情,点击点标记放大地图操作
查看>>
初始化VUE项目报错
查看>>
vue项目使用安装sass
查看>>
HTTP和HttpServletRequest 要点
查看>>
在osg场景中使用GLSL语言——一个例子
查看>>
关于无线PCB中 中50欧姆的特性阻抗的注意事项
查看>>
Spring的单例模式源码小窥
查看>>
后台服务的变慢排查思路(轻量级应用服务器中测试)
查看>>
MySQL中InnoDB事务的默认隔离级别测试
查看>>
微服务的注册与发现
查看>>
bash: service: command not found
查看>>
linux Crontab 使用 --定时任务
查看>>
shell编程----目录操作(文件夹)
查看>>