HDU-3320
Alice’s Cube
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1866 Accepted Submission(s): 591
Alice has received a hypercube toy as her birthday present. This hypercube has 16 vertices, numbered from 1 to 16, as illustrated below. On every vertex, there is a light bulb that can be turned on or off. Initially, eight of the light bulbs are turned on and the other eight are turned off. You are allowed to switch the states of two adjacent light bulbs with different states (“on” to “off”, and “off” to “on”; specifically, swap their states) in one operation.
Given the initial state of the lights, your task is to calculate the minimum number of steps needed to achieve the target state, in which the light bulbs on the sub cube (1,2,3,4)-(5,6,7,8) are turned off, and the rest of them are turned on.
For each test case there are 16 numbers in a single line, the i-th number is 1 meaning the light of the i-th vertex on the picture is on, and otherwise it’s off.
3
0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1
0 1 0 0 0 0 0 0 1 0 1 1 1 1 1 1
0 0 0 0 0 0 1 0 1 0 1 1 1 1 1 1
/**
题意:现在给出16盏灯的状态 然后每盏灯都有4个相连的灯
如果要改变当前灯的状态必须是它和它相邻的灯的状态不一样
要求最后转变的结果是1~8灭 9~16亮 并且操作步数<=3
如果满足要求 输出最小的步数,否则输出-1
做法:bfs() + 剪枝
**/
#include <iostream>
#include <cmath>
#include <algorithm>
#include <stdio.h>
#include <string.h>
#include <queue>
using namespace std;
#define INF 100000000
int step[( << ) + ];
int num[][] = {
{},
{, , , },
{, , , },
{, , , },
{, , , },
{, , , },
{, , , },
{, , , },
{, , , },
{, , , },
{, , , },
{, , , },
{, , , },
{, , , },
{, , , },
{, , , },
{, , , }
};
struct Node
{
int step;
int flag[];
};
void init()
{
for(int i = ; i <= ( << ) + ; i++)
{
step[i] = INF;
}
}
int eed = ( << ) - ( << );
int getnum(int aa[])
{
int sum = ;
for(int i = ; i >= ; i--)
{
sum = sum * + aa[i];
}
return sum;
}
Node Begin;
void bfs()
{
queue<Node>que;
Node now, tmp;
Begin.step = ;
que.push(Begin);
while(!que.empty())
{
now = que.front();
que.pop();
if(now.step >= ) {
continue;
}
int tt = getnum(now.flag);
if(tt == eed) {
break;
}
int numm = ;
for(int i = ; i <= ; i++)
{
if(now.flag[i] == ) {
numm++;
}
}
if(now.step + numm > ) {
continue;
}
for(int i = ; i <= ; i++)
{
for(int j = ; j < ; j++)
{
if(num[i][j] > i && now.flag[i] != now.flag[num[i][j]])
{
int ttt = now.flag[num[i][j]];
now.flag[num[i][j]] = now.flag[i];
now.flag[i] = ttt;
now.step ++;
int res = getnum(now.flag);
if(step[res] > now.step)
{
step[res] = now.step;
que.push(now);
}
now.step --;
ttt = now.flag[num[i][j]];
now.flag[num[i][j]] = now.flag[i] ;
now.flag[i] = ttt;
}
}
}
}
}
int main()
{
int T;
scanf("%d", &T);
int Case = ;
while(T--)
{
int tmp = ;
init();
for(int i = ; i <= ; i++)
{
scanf("%d", &Begin.flag[i]);
if(i <= && Begin.flag[i] == )
{
tmp++;
}
}
printf("Case #%d: ", Case++);
if(tmp > ) {
printf("more\n");
continue;
}
int st;
st = getnum(Begin.flag);
step[st] = ;
bfs();
if(step[eed] <= ) {
printf("%d\n", step[eed]);
}
else {
printf("more\n");
}
}
return ;
}
HDU-3320的更多相关文章
- hdu 3320 计算几何(三维图形几何变换)
openGL Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Subm ...
- 转载:hdu 题目分类 (侵删)
转载:from http://blog.csdn.net/qq_28236309/article/details/47818349 基础题:1000.1001.1004.1005.1008.1012. ...
- HDOJ 2111. Saving HDU 贪心 结构体排序
Saving HDU Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- 【HDU 3037】Saving Beans Lucas定理模板
http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...
- hdu 4859 海岸线 Bestcoder Round 1
http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...
- HDU 4569 Special equations(取模)
Special equations Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u S ...
- HDU 4006The kth great number(K大数 +小顶堆)
The kth great number Time Limit:1000MS Memory Limit:65768KB 64bit IO Format:%I64d & %I64 ...
- HDU 1796How many integers can you find(容斥原理)
How many integers can you find Time Limit:5000MS Memory Limit:32768KB 64bit IO Format:%I64d ...
- hdu 4481 Time travel(高斯求期望)(转)
(转)http://blog.csdn.net/u013081425/article/details/39240021 http://acm.hdu.edu.cn/showproblem.php?pi ...
- HDU 3791二叉搜索树解题(解题报告)
1.题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=3791 2.参考解题 http://blog.csdn.net/u013447865/articl ...
随机推荐
- DPDK 网卡RSS(receive side scaling)简介
网卡RSS(receive side scaling)简介 RSS是一种网卡驱动技术,能让多核系统中跨多个处理器的网络收包处理能力高效能分配.注意:由于同一个核的处理器超线程共享同一个执行引擎,这个效 ...
- hadoop自定义数据类型
统计某手机数据库的每个手机号的上行数据包数量和下行数据包数量 数据库类型如下: 数据库内容如下: 下面自定义类型SimLines,类似于平时编写的model import java.io.DataIn ...
- IP数据报格式 及分组转发算法
ip数据报分首部和数据两部分组成: 首部分为固定部分和可变部分 版本——占 4 位,指 IP 协议的版本 目前的 IP 协议版本号为 4 (即 IPv4) 首部长度——占 4 位,可表示的最大数值 是 ...
- 团队作业4——第一次项目冲刺(Alpha版本)-第三篇
项目冲刺——第三阶段 前两阶段很ok,目测这三天可以搞定! 分工讨论 大体上搞定,设置困难度的功能还未完成. 团队成员 任务 郭达 整合各种代码 刘德培 数据库完善和其他人对接 石浩洋 完善PH ...
- OpenCV彩色图像转灰度图
核心函数cvSplit(). #include<cv.h> #include<highgui.h> int main(int argc, char** argv) { IplI ...
- javaScript的流程控制语句学习笔记
JavaScript提供了5种流程控制语句,if条件判断语句,switch语句,for循环语句,while循环语句,do-while循环语句. 1.条件判读语句 对变量或表达式进行判定,并根据判定结果 ...
- 使用XML传递数据
HTML <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF- ...
- oracle默认查询当前表空间的数据 当夸空间查询时候 需要指定具体的用户空间
- BZOJ4476 JSOI2015送礼物(分数规划+单调队列)
看到这个式子当然先二分答案.得max-min-(j-i+k)ans>=0. 显然max-min相同的情况下所选区间长度越短越好,所以max和min都应该取在边界.那么实际上我们根本不用管端点是否 ...
- [洛谷P4838]P哥破解密码
题目大意:求长度为$n$的$01$串中,没有连续至少$3$个$1$的串的个数 题解:令$a_1$为结尾一个$1$的串个数,$a_2$为结尾两个$1$的串的个数,$b$为结尾是$0$的串的个数.$a_1 ...