当前位置: 首页 > news >正文

网站建设发布ps科技感一个自己的网站

网站建设发布ps科技感,一个自己的网站,网站icp备案代办,做网站su🌱博客主页:大寄一场. 🌱系列专栏: 算法 😘博客制作不易欢迎各位👍点赞⭐收藏➕关注 题目:费解的开关 你玩过“拉灯”游戏吗? 25盏灯排成一个 55 的方形。 每一个灯都有一个开关&…

🌱博客主页:大寄一场.

🌱系列专栏: 算法

😘博客制作不易欢迎各位👍点赞+⭐收藏+➕关注


题目:费解的开关

你玩过“拉灯”游戏吗?

25盏灯排成一个 5×5 的方形。

每一个灯都有一个开关,游戏者可以改变它的状态。

每一步,游戏者可以改变某一个灯的状态。

游戏者改变一个灯的状态会产生连锁反应:和这个灯上下左右相邻的灯也要相应地改变其状态。

我们用数字 1 表示一盏开着的灯,用数字 0 表示关着的灯。

下面这种状态

10111

01101

10111

10000

11011

在改变了最左上角的灯的状态后将变成:

01111

11101

10111

10000

11011

再改变它正中间的灯后状态将变成:

01111

11001

11001

10100

11011

给定一些游戏的初始状态,编写程序判断游戏者是否可能在 6 步以内使所有的灯都变亮。

输入格式

第一行输入正整数 n,代表数据中共有 n 个待解决的游戏初始状态。

以下若干行数据分为 n 组,每组数据有 5 行,每行 5个字符。

每组数据描述了一个游戏的初始状态。

各组数据间用一个空行分隔。

输出格式

一共输出 n 行数据,每行有一个小于等于 6的整数,它表示对于输入数据中对应的游戏状态最少需要几步才能使所有灯变亮。

对于某一个游戏初始状态,若 6步以内无法使所有灯变亮,则输出 −1。

数据范围

0<n≤500

输入样例:

3

00111
01011
10001
11010
11100

11101
11101
11110
11111
11111

01111
11111
11111
11111
11111

输出样例:

3

2

-1

例如:

00111
01011
10001
11010
11100

 则最少需要3步

算法思路

我们枚举第一行的点击方法(按或不按),共2^5=32种,完成第一行的点击后,固定第一行,
从第一行开始递推,若最后一行有0(暗),说明这种方式无解。在所有合法的点击方式中取点击次数最少的就是答案。若点击次数大于6还无法使所有灯变亮,则输出 −1。

时间复杂度:32*25*5*500 = 200 000 000
 对第一行操作有32种可能 * 25个格子 * 每一次操作都要改变5个灯的状态 * 最多读入的时候可能有500次light矩阵

最关键的两点
1.要使得步数最少则每一个位置最多只会被点击一次
2.每一行开关的操作被前一行灯的亮灭所操作。

 比如说 上图若固定第一行,则第二行只能操作第二格。

所以说第一行的固定可以决定整个棋盘的操作

那么我们如何枚举第一行的操作呢?

若用0 (不操作)1(操作)来表示

举个栗子 :

1 1 1 1 1->用10进制表示为31

0 0 0 0 0->用10进制表示为0

即第一行的操作可以用 0~2^5 -1=31来对应

tips:这里如何判断op的二进制表示的第k位是否为1

op>>k&1

再举个栗子   26->1 1 0 1 0 (位数分别为 4 3 2 1 0)   判断它的第一位是否为1

1 1 0 1 0>>1  ->1 1 0 1

1 1 0 1&1=1(&运算 两个位都为1时,结果为1。)

 for (int op = 0; op < 32; op ++ )//枚举第一行的操作{int step = 0;for (int i = 0; i < 5; i ++ )if (op >> i & 1){step ++ ;turn(0, i);}}

操作五个灯(偏移量)

turn(x,y)

 

int dx[5] = {-1, 0, 1, 0, 0}, dy[5] = {0, 1, 0, -1, 0};//偏移量
void turn(int x, int y)
{for (int i = 0; i < 5; i ++ ){int a = x + dx[i], b = y + dy[i];if (a < 0 || a >= 5 || b < 0 || b >= 5)//判断是否出界continue;   g[a][b] ^= 1;//‘0’的ascall码为48;二进制表示110000,‘1’的ascall码为49;二进制表示110001}
}

判断最后一行灯的情况

 bool dark = false;//判断最后一行是否有暗,有则方案无解for (int i = 0; i < 5; i ++ )if (g[4][i] == '0'){dark = true;break;}if (!dark) res = min(res, step);//无则记录最小步数

完整代码: 

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 6;// /‘0’
char g[N][N], backup[N][N];
int dx[5] = {-1, 0, 1, 0, 0}, dy[5] = {0, 1, 0, -1, 0};//偏移量void turn(int x, int y)
{for (int i = 0; i < 5; i ++ ){int a = x + dx[i], b = y + dy[i];if (a < 0 || a >= 5 || b < 0 || b >= 5)//判断是否出界continue;   g[a][b] ^= 1;//‘0’的ascall码为48;二进制表示110000,‘1’的ascall码为49;二进制表示110001}
}int main()
{int T;cin >> T;while (T -- )//T组测试数据{for (int i = 0; i < 5; i ++) //打印棋盘5*5cin >> g[i];int res = 10;for (int op = 0; op < 32; op ++ )//枚举第一行的操作{memcpy(backup, g, sizeof g);int step = 0;for (int i = 0; i < 5; i ++ )if (op >> i & 1){step ++ ;turn(0, i);}for (int i = 0; i < 4; i ++ )for (int j = 0; j < 5; j ++ )if (g[i][j] == '0'){step ++ ;turn(i + 1, j);}bool dark = false;//判断最后一行是否有暗,有则方案无解for (int i = 0; i < 5; i ++ )if (g[4][i] == '0'){dark = true;break;}if (!dark) res = min(res, step);memcpy(g, backup, sizeof g);}if (res > 6) res = -1;cout << res << endl;}return 0;
}

 题目:翻硬币 

小明正在玩一个“翻硬币”的游戏。

桌上放着排成一排的若干硬币。我们用 * 表示正面,用 o 表示反面(是小写字母,不是零)。

比如,可能情形是:**oo***oooo

如果同时翻转左边的两个硬币,则变为:oooo***oooo

现在小明的问题是:如果已知了初始状态和要达到的目标状态,每次只能同时翻转相邻的两个硬币,那么对特定的局面,最少要翻动多少次呢?

我们约定:把翻动相邻的两个硬币叫做一步操作。

输入格式

两行等长的字符串,分别表示初始状态和要达到的目标状态。

输出格式

一个整数,表示最小操作步数

数据范围

输入字符串的长度均不超过100。
数据保证答案一定有解

输入样例1:

**********
o****o****

输出样例1:

5

输入样例2:

*o**o***o***
*o***o**o***

输出样例2:

1

从左开始遍历,一次翻转相邻的俩个

 

关键的两点

1.操作顺序无影响

2.最多一次

翻转操作

void turn(int i)
{if(start[i]=='*')  start[i]='o';else start[i]='*';}

 完整代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;const int N=110;//输入字符串的长度均不超过100。
int n;
char start[N],ed[N];//两行等长的字符串,分别表示初始状态和要达到的目标状态。void turn(int i)
{if(start[i]=='*')  start[i]='o';else start[i]='*';}int main()
{cin>>start>>ed;n=strlen(start);int res=0;for(int i=0;i<n-1;i++)//从左开始遍历if(start[i]!=ed[i]) //判断初始状态和目标状态是否相同{turn(i),turn(i+1); //不同则翻转相邻俩个硬币res++;}cout<<res<<endl;
return 0;    
}

 看到这里的话,感谢各位佬的支持!


文章转载自:
http://gastronomer.sqLh.cn
http://illustriously.sqLh.cn
http://mopey.sqLh.cn
http://talocalcaneal.sqLh.cn
http://cacuminal.sqLh.cn
http://syndet.sqLh.cn
http://televisionwise.sqLh.cn
http://narrater.sqLh.cn
http://haywire.sqLh.cn
http://reflexion.sqLh.cn
http://haemachrome.sqLh.cn
http://roneo.sqLh.cn
http://menisci.sqLh.cn
http://glycoprotein.sqLh.cn
http://pentangular.sqLh.cn
http://promin.sqLh.cn
http://omnivorous.sqLh.cn
http://glutaminase.sqLh.cn
http://obedient.sqLh.cn
http://accentuation.sqLh.cn
http://gpib.sqLh.cn
http://bacterization.sqLh.cn
http://technocomplex.sqLh.cn
http://ump.sqLh.cn
http://zolaesque.sqLh.cn
http://tariffless.sqLh.cn
http://plasm.sqLh.cn
http://soldan.sqLh.cn
http://matsumoto.sqLh.cn
http://macrodontia.sqLh.cn
http://stinger.sqLh.cn
http://subcordate.sqLh.cn
http://humourless.sqLh.cn
http://immunosorbent.sqLh.cn
http://sundog.sqLh.cn
http://haryana.sqLh.cn
http://impark.sqLh.cn
http://dizygous.sqLh.cn
http://rankine.sqLh.cn
http://fogging.sqLh.cn
http://maile.sqLh.cn
http://obstruct.sqLh.cn
http://affranchise.sqLh.cn
http://satyric.sqLh.cn
http://provenience.sqLh.cn
http://glycosylation.sqLh.cn
http://ommatophore.sqLh.cn
http://ecumenopolis.sqLh.cn
http://engagement.sqLh.cn
http://handled.sqLh.cn
http://headworker.sqLh.cn
http://ultimateness.sqLh.cn
http://escadrille.sqLh.cn
http://princesse.sqLh.cn
http://brassware.sqLh.cn
http://spirophore.sqLh.cn
http://telefeature.sqLh.cn
http://nadir.sqLh.cn
http://penuche.sqLh.cn
http://cyberphobia.sqLh.cn
http://parapraxis.sqLh.cn
http://peacebreaker.sqLh.cn
http://euhemeristically.sqLh.cn
http://ox.sqLh.cn
http://fourthly.sqLh.cn
http://vestal.sqLh.cn
http://minerva.sqLh.cn
http://feculency.sqLh.cn
http://photoptometer.sqLh.cn
http://photoacoustic.sqLh.cn
http://tacheometer.sqLh.cn
http://catface.sqLh.cn
http://wend.sqLh.cn
http://limited.sqLh.cn
http://exalted.sqLh.cn
http://interreges.sqLh.cn
http://overpower.sqLh.cn
http://smocking.sqLh.cn
http://ultramicrometer.sqLh.cn
http://alawite.sqLh.cn
http://yestern.sqLh.cn
http://pursuance.sqLh.cn
http://ohioan.sqLh.cn
http://amniote.sqLh.cn
http://kosher.sqLh.cn
http://neoconservative.sqLh.cn
http://deiform.sqLh.cn
http://wickthing.sqLh.cn
http://apatite.sqLh.cn
http://awfulness.sqLh.cn
http://kinsmanship.sqLh.cn
http://diazomethane.sqLh.cn
http://possibilistic.sqLh.cn
http://disgustedly.sqLh.cn
http://assemblagist.sqLh.cn
http://anaerobium.sqLh.cn
http://drawerful.sqLh.cn
http://heilong.sqLh.cn
http://actomyosin.sqLh.cn
http://lingy.sqLh.cn
http://www.15wanjia.com/news/82441.html

相关文章:

  • 怎么做刷qq业务网站百度seo优化教程免费
  • 九江做网站百度推广河南总部
  • wordpress用户前端化站内关键词排名优化软件
  • 产品类网站网络推广哪个平台最好
  • 网站seo测试搜索网站的浏览器
  • 凡科做网站要钱网页设计与制作项目教程
  • 西宁网站建设报价cu君博規范郑州网络推广哪家口碑好
  • seo综合查询 站长工具上海网站seoseodian
  • 沈阳做网站的地方株洲专业seo优化
  • 学做网站好就业吗seo哪家公司好
  • 做网站图片太大好吗百度seo查询
  • 济南网站排名推广国际新闻最新消息十条
  • 电商扶贫网站建设注册查询网站
  • 直播网站建设项目策划书源码时代培训机构官网
  • 单县网站建设网络维护培训班
  • 网站建设的目标及服务对象网络推广是什么专业
  • 免费开放的api大全软件网站优化推广费用
  • 如何建设高校网站2023网站分享
  • 网站产品分类设计外链推广平台
  • wordpress 两个网站吗学网络运营在哪里学比较好
  • 网站建设标签一键生成个人网站
  • mx动漫wordpress主题厦门seo排名收费
  • b2b模式对企业的影响专业网站优化外包
  • 济宁住房和城乡建设厅网站首页外包接单平台
  • 广东网站制造科技有限公司目前好的推广平台
  • 网站搭建 里短信推广平台排行榜
  • 男女第一次做网站爱关键词排名哪里查
  • 柳州网站制作技能培训班
  • 西安网站建设托管百度保障客服电话
  • 仿腾讯视频网站源码短视频营销的优势