UVA 10651 Pebble Solitaire
2016-04-15 19:32
441 查看
【题意】12个位置,有些有鹅卵石,有些是空的,2个连续的鹅卵石,如果其左边连着的那个是空的,那么第二个鹅卵石可移动到那个空的位置上,并移除第1个鹅卵石;如果其右边连着的那个是空的,那么第一个鹅卵石可移动到那个空的位置上,并移除第2个鹅卵石。问最后最少可以剩下多少个鹅卵石?
【分析】由于3个连续出现的鹅卵石才有可能发生交换,所以枚举所有能够出现的状态,交换直接相邻3位取异或即可!记得回溯!
【AC代码】
【分析】由于3个连续出现的鹅卵石才有可能发生交换,所以枚举所有能够出现的状态,交换直接相邻3位取异或即可!记得回溯!
【AC代码】
#include <stdio.h> #include <string.h> #include <iostream> using namespace std; const int inf = 0x3f3f3f3f; int ans,n; char s[13]; int dfs(int sta) { int temp,last=0; for(int i=1; i<=10; i++) { if((sta&(1<<(i-1))&&sta&(1<<i)&&!(sta&(1<<(i+1))))||(!(sta&(1<<(i-1)))&&sta&(1<<i)&&sta&(1<<(i+1)))) { sta^=(1<<(i-1)); sta^=(1<<i); sta^=(1<<(i+1)); temp = dfs(sta); ans = min(ans,temp); sta^=(1<<(i-1)); sta^=(1<<i); sta^=(1<<(i+1)); } } if(last==0) { int cnt=0; for(int i=0; i<12; i++) { if(sta&(1<<i))cnt++; } ans = min(ans,cnt); } return ans; } int main() { while(~scanf("%d",&n)) { for(int i=0; i<n; i++) { int sta=0; scanf("%s",s); for(int j=0; j<12; j++) { if(s[j]=='o') { sta = sta^(1<<j); } } ans = inf; dfs(sta); printf("%d\n",ans); } } return 0; }
相关文章推荐
- Training Deep Neural Networks
- Leetcode题解 217. Contains Duplicate
- CPaintDC、CClientDC、CWindowDC和CMetaFileDC类的主要特点及区别
- 进行删除文件操作时碰到错误:open failed: EBUSY (Device or resource busy)
- saiku执行速度慢
- saiku执行速度慢
- 服务器知识:RAID技术图解
- spark 源码里面的sealed trait 存在原因
- 172. Factorial Trailing Zeroes
- 浅谈RAII&智能指针
- UVA_10245_ The Closest Pair Problem
- 调用http://apistore.baidu.com网站的接口
- a、button、input点击出现蓝色边框,如何去掉
- codeforces 652C C. Foe Pairs(尺取法+线段树查询一个区间覆盖线段)
- 进程控制开发[fork() exec exit _exit wait waitpid 守护进程]
- 219. Contains Duplicate II
- 编译过程中,报failed to resolve:com.andriod.databinding.adapters:1.1
- Lesson 3: More About Jobs and Job Details
- textview 计算宽
- CDISC SDTM CM domain 学习笔记