poj 1753Flip Game---枚举

poj1753枚举 该题记录了本人研究该题的记录,有些是心得,希望能给大家帮助。 立即下载

Description

Flip game is played on a rectangular 4x4 field with two-sided pieces placed on each of its 16 squares. One side of each piece is white and the other one is black and each piece is lying either it's black or white side up. Each round you flip 3 to 5 pieces, thus changing the color of their upper side from black to white and vice versa. The pieces to be flipped are chosen every round according to the following rules:
  1. Choose any one of the 16 pieces.
  2. Flip the chosen piece and also all adjacent pieces to the left, to the right, to the top, and to the bottom of the chosen piece (if there are any).

Consider the following position as an example:

bwbw
wwww
bbwb
bwwb
Here "b" denotes pieces lying their black side up and "w" denotes pieces lying their white side up. If we choose to flip the 1st piece from the 3rd row (this choice is shown at the picture), then the field will become:

bwbw
bwww
wwwb
wwwb
The goal of the game is to flip either all pieces white side up or all pieces black side up. You are to write a program that will search for the minimum number of rounds needed to achieve this goal.

Input

The input consists of 4 lines with 4 characters "w" or "b" each that denote game field position.

Output

Write to the output file a single integer number - the minimum number of rounds needed to achieve the goal of the game from the given position. If the goal is initially achieved, then write 0. If it's impossible to achieve the goal, then write the word "Impossible" (without quotes).

Sample Input

bwwb
bbwb
bwwb
bwww

Sample Output

4

看了别人的,才知道要这样做啊

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
bool map[4][4];///存储初始的地图
bool tp[4][4];///翻牌后的地图
char ls[4][4];
bool flag[4][4];
int k,key;
void dfs(int n, int m)///深搜
{
    if(n == k)///翻了k张牌
    {
        for(int i = 0; i < 4; i++)
        {
            for(int j = 0; j < 4; j++)
                tp[i][j] = map[i][j];
        }
        for(int i = 0; i < 4; i++)///翻完牌后的情况
        {
            for(int j  = 0; j < 4; j++)
            {
                if(flag[i][j])
                {
                     tp[i][j] = !tp[i][j];
                    if(i-1>= 0)
                        tp[i-1][j] = !tp[i-1][j];
                    if(i+1 < 4)
                        tp[i+1][j] = !tp[i+1][j];
                    if(j-1 >= 0)
                        tp[i][j-1] = !tp[i][j-1];
                    if(j+1 < 4)
                        tp[i][j+1] = !tp[i][j+1];
                }
            }
        }
        bool mm = tp[0][0];
        key = 1;
        for(int i = 0; i < 4; i++)///看是否完成
        {
                for(int j = 0; j < 4; j++)
                {
                        if(tp[i][j] != mm)
                        {
                            key = 0;
                             break;
                        }
                }
                if(key == 0)
                   break;
        }
        return ;
    }
    for(int i = m+1; i < 16; i++)///枚举翻的牌,用flag标记
    {
        int x = i/4;
        int y = i%4;
        flag[x][y] = 1;
        dfs(n+1,i);
        if(key)
            return ;
        flag[x][y] = 0;
    }
}
int main()
{
    while(~scanf("%s",ls[0]))///为了多组输入
    {
        for(int i = 1; i < 4; i++)
            scanf("%s",ls[i]);
        for(int  i = 0; i < 4; i++)///把英文的变为一个0,1的,可以用位运算
        {
            for(int j = 0; j < 4; j++)
            {
                if(ls[i][j] == 'w')
                    map[i][j] = 1;
                else
                    map[i][j] = 0;
            }
        }
        key = 0;///重要的控制变量
        for(k = 0; k <= 16; k++)///枚举1步2步还是
        {
             memset(flag,0,sizeof(flag));///清空标记
            dfs(0,-1);///开始翻牌
            if(key)
                break;
        }
        if(key)
            printf("%d\n",k);
        else
            printf("Impossible\n");
    }
    return 0;
}

另外,把云飞巨 的代码沾上,我是看他的思路

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
char s[5][5];
bool flag[5][5];
int k;
bool bh[5][5];
bool mark;
bool tmp[5][5];
void dfs_search(int cnt,int cont)
{
    if(cnt==k)
    {
    	for(int i=0;i<4;i++)
		{
			for(int j=0;j<4;j++)
			{
				tmp[i][j]=bh[i][j];
			}
		}
        for(int i=0; i<4; i++)
        {
            for(int j=0; j<4; j++)
            {
                if(flag[i][j])
                   {
                   	tmp[i][j]=!tmp[i][j];
                   	if(i-1>=0)
						tmp[i-1][j]=!tmp[i-1][j];
					if(i+1<4)
						tmp[i+1][j]=!tmp[i+1][j];
					if(j-1>=0)
						tmp[i][j-1]=!tmp[i][j-1];
					if(j+1<4)
						tmp[i][j+1]=!tmp[i][j+1];
                   }
            }
        }
        bool x=tmp[0][0];
        mark=1;
        for(int i=0; i<4; i++)
        {
            for(int j=0; j<4; j++)
            {
                if(x!=tmp[i][j])
                {
                    mark=0;
                    break;
                }
            }
            if(mark==0)
                break;
        }
        return ;
    }
    for(int i=cont+1; i<16; i++)
    {
        int x=i/4;
        int y=i%4;
        flag[x][y]=1;
        dfs_search(cnt+1,i);
        if(mark)
            return;
        flag[x][y]=0;
    }
}
int main()
{

    while(~scanf("%s",s[0]))
    {

        for(int i=1; i<4; i++)
        {
            scanf("%s",s[i]);
        }
        mark=0;
        for(int i=0; i<4; i++)
        {
            for(int j=0; j<4; j++)
                if(s[i][j]=='w')
                    bh[i][j]=1;
                else
                    bh[i][j]=0;
        }
        mark=0;
        for(k=0; k<=16; k++)
        {
            memset(flag,0,sizeof(flag));
            dfs_search(0,-1);
            if(mark)
                break;
        }
        if(mark)
            printf("%d\n",k);
        else
            printf("Impossible\n");
    }
    return 0;
}


基于深度强化学习技术优化无线网络中的资源分配(一)DQN部分代码 这是一个GitHub上的开源项目,链接:https://github.com/alexis-anzaldo/Deep-Reinforcement-Learning-for-resource-allocation-in-wireless-networks该项目旨在通过深度强化学习DQL技术优化无线网络中的资源分配,特别关注小型蜂窝和超密集无线蜂窝网络中的功率控制问题,主要目标是提高DRL模型的学习效率和网络吞吐量。 阅读详情

相关推荐

Unity游戏开发客户端面经——数据结构(初级)

Unity游戏开发客户端面经 之 数据结构篇

Sea3752的博客 1万+

POJ 1753 Flip Game【暴搜+深搜】

Description Flip game is played on a rectangular 4x4 field with two-sided pieces placed on each of its 16 squares. One side of each piece is white and the other one is black and each piece is lying e...

weixin_33768481的博客 812

保姆级教程:手把手教你用Jacquard V2数据集训练机器人抓取模型(附完整代码)

本文提供了一份详细的保姆级教程,手把手教你使用Jacquard V2数据集训练机器人抓取模型。从环境配置、数据准备到模型架构设计、训练优化和实际部署,全程解析并附完整代码,帮助开发者快速掌握视觉抓取技术。

weixin_29031057的博客 322

poj1753:Flip Game

附原文: http://blog.sina.com.cn/s/blog_6635898a0100ivzv.html 题意:一个4*4的矩阵,每一#include using namespace std; bool isgoal(); void flip(int row, int cow); void dfs(int row, int cow, int ste); int dr[5] = {-1

我是任玉琢 925

openwrt编译及第一个安装包教程

Date: 2017.03.14 Made:SuperDeverloper Email: na1206@live.com Target: For mt7688 based board说明:本人在学习过程中走了不少弯路,希望本文档能为后来者提供便利。欢迎复制和使用本文档。本文档记录了搭建openwrt编译环境以及快速创建ipk包(openwrt上的安装程序)。本文档在实际应用中做过几次修改,以帮

SuperDevloper的博客 1万+

ROS 2 Slam Toolbox实战:从零跑通2D激光建图

SLAM(同步定位与地图构建)是移动机器人实现自主导航的基础技术,其核心在于通过传感器数据实时估计位姿并构建环境栅格地图。slam_toolbox作为ROS 2 Humble及后续LTS版本官方主推的2D SLAM后端,采用图优化+闭环检测架构,兼顾实时性、鲁棒性与工程可维护性。相比cartographer和hector_slam,它原生支持ROS 2、参数精简、诊断友好,特别适合硬件资源有限、缺乏IMU或深度相机的差速机器人平台。本文聚焦真实部署场景,覆盖RPLIDAR等常见2D激光雷达与TurtleBo

weixin_30596165的博客 364

Flip Game(BFS+状态压缩 | 枚举+状态压缩)

Description Flip game is played on a rectangular 4x4 field with two-sided pieces placed on each of its 16 squares. One side of each piece is white and the other one is black and each piece is lyin...

一身清贫怎敢入繁华 744

滤波算法实现大全——基于匀速直线运动模型的kalman滤波算法

滤波算法实现大全——基于匀速直线运动模型的kalman滤波算法

jane0819的博客 530

[POJ 2449] Remmarguts' Date 第K短路

http://poj.org/problem?id=2449题意:输入 n, m 表示给一个 n 个顶点 m 条边的有向图,然后输入 m 条边,最后一行输入 s, t, k,要求这个图中 s 点到 t 点的第 k 短路径。思路:用 A* 求,估价函数f(i)=g(i) + h(i)。g(i) 就是 s 点到 i 点的某条路径的长度

AcmHonor的专栏 665

骑砍2霸主MOD开发(9)-游戏大地图(MapScene)

Settlement类型由town,castle,castle_village,village,hideout五种类型组成,通过对GameEntity中添加tag实现类型的加载.大地图中军团(MobileParty)的移动速度,AI寻路,食物消耗等均和当前MobileParty所处地形有关,地形统一被定义为TerrainType,类型有森林,荒漠等.MapScene.Read("Main_map", ref sceneInitializationData, "") 装载Main_map大地图。

骑砍战团&霸主MOD开发 1628

T113-S3-官方SDK分区添加

本篇文章将展示如何编译T113-S3的SDK以及在sdk中添加用户自定义分区,让您能够轻松地开始在您的项目中使用T113-S3芯片。T113-S3是一款功能强大的嵌入式处理器,适用于各种应用,包括物联网、工业自动化和智能家居等。T113-S3-SDK是特别为T113-S3芯片设计的软件开发工具包(Software Development Kit)。它包含了一系列用于开发和定制嵌入式应用程序的工具、库和示例代码,方便开发者在T113-S3平台上开展软件开发工作。

flypig has a dream! 2371

POJ 1753 Flip Game (枚举)

Description Flip game is played on a rectangular 4x4 field with two-sided pieces placed on each of its 16 squares. One side of each piece is white and the other one is black and each piece is lying e

小坏蛋_千千 1773

面试问题总结——关于YOLO系列(三)

接着上篇的的YOLO系列面试问题总结,加油,冲! 三.YOLOv3 1.YOLOv3主干网络BackBone——Darknet-53网络结构(因为有53个卷积层) (原论文中Darknet53的尺寸是在图片分类训练集上训练的,所以输入的图像尺寸是256x256,而在YOLOv3中输入尺寸是416×416)   ①上图中,Top-1 Accuracy和Top-5 Accuracy是指什么呢?区别在哪呢?   我们知道ImageNet有大概1000个分类,而模型预测某张图片时,会给出1000个按概率从高.

boss-dog 1万+

POJ 1753 Flip Game (递归枚举)

原文地址:http://www.cnblogs.com/shuaiwhu/archive/2012/04/27/2474041.html POJ 1753,题目链接http://poj.org/problem?id=1753,翻译一下整个题目的大概意思: 有4*4的正方形,每个格子要么是黑色,要么是白色,当把一个格子的颜色改变(黑->白或者白->黑)时,其周围上下左右(如果存在

o19871108的专栏 416

C# WinForm 基于DirectShow.dll编写的程序,可边预览边保存视频,音频和图片

基于微软的DirectShow.dll的二次开发1.自动识别连接的摄像头跟麦克风且可以对其进行选择(默认选中第一个)2.可录制wmv和avi两种格式的视频并且都带有音频,avi可以录制的分辨率和帧数进行选择,wmv默认进行了淸晰度优化如不喜欢可去除代码中有相关提示3.可在预览点开之后进行拍照,也可边录制视频边拍照4.添加了视频和音频的压缩功能(酌情使用)这次二次开发发现C#对DirectShow的相关信息太少,这次做完也不敢藏私共享出来,还望大家指正

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

【POJ】1753 - Flip Game(bfs,枚举)

Flip Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 37629   Accepted: 16368 Description Flip game is played on a rectangular 4x4 field with two-sided

wyg1997 808

lwip-2.0.3

lwip是瑞典计算机科学院(SICS)的Adam Dunkels 开发的一个小型开源的TCP/IP协议栈。实现的重点是在保持TCP协议主要功能的基础上减少对RAM 的占用。

POJ 1753 Flip Game【枚举】

题目链接: http://poj.org/problem?id=1753 题意: 由白块黑块组成的4*4方格,每次换一个块的颜色,其上下左右的块也会被换成相反的颜色。问最少换多少块,使得最终方格变为全白或者全黑~ 分析: 典型的枚举。 这种问题被称为开关问题,关键是要对周围的块翻动对自身的影响进行记录。后一排决定了前一排的最终状态,枚举第一排的情况即可...

An55511的博客 209

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

poj 1753 Flip Game 高斯消元+枚举

poj 1753 Flip Game 高斯消元+枚举 题目链接:http://poj.org/problem?id=1753 题目描述: Flip Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 40652   Accepted: 17652 D

而濡木染 884

POJ.1753 Flip Game(高斯消元+枚举自由元 两次)

题意:给你一个4*4的矩阵,每个位置对应着一个状态(开或者关),然后如果你按动某一个位置,那么上下左右位置都会被影响二改变状态,问最少经过几次达到全开或者全馆的状态。 分析:其实对于每一个位置都可以列一个线性方程,构成一个方程组,a[i][j]就是表示按动j位置时对i位置有影响,然后对于方程组用高斯消元求解,然后对于存在自由元的情况需要枚举判断符合的个数,最后取最小值。 #include <...

八百标兵奔北坡的博客 300

POJ 1753 Flip Game 枚举

POJ1753解题报告,枚举方法

Puyuacm的专栏 667

枚举 | POJ1753.Flip Game

枚举 | POJ1753.Flip Game Link:POJ1753.Flip Game Description Flip game is played on a rectangular 4x4 field with two-sided pieces placed on each of its 16 squares. One side of each piece is white and the...

Redemption1997的博客 294

POJ 1753 Flip Game(递归枚举)

题目:1753 题意:有一个4*4的棋盘,棋盘上有黑白格,每一次你可以翻其中的一个格子。一个格子(x,y)如果被翻,它相邻的前后左右四个格子(如果在棋盘上)也要翻转。现在给你一个初始的棋盘状态,问把这个棋盘翻转到全黑或全白的最少次数;若不能达到全黑或全白,输出Impossible。 题解:对于每一个格子,翻奇数任意次和翻一次没有区别,翻偶数任意次和翻0次没有区别,所有直接递归枚举所有的状态即可...

qq_36552817的博客 402

poj 1753 Flip game 枚举

BFS搜索 + 位运算提速。。枚举每一个状态, 进行up, left, down, right操作。。当状态state = 0xffff 或0时,说明全部为黑或白。。。 对于一个状态没有访问过就入队,并且标志为已访问。。 View Code #include <iostream> #include <cstdlib> #include <stdlib....

weixin_30597269的博客 174

poj1753 Flip Game(枚举,DFS)

题意:一个4*4的围棋棋盘,黑白子随机,每次翻转同时翻转四周的棋子,最少翻转几次使所有棋子同色。 代码,非常漂亮的DFS #include <cstdio> #include <iostream> using namespace std; int map[10][10]={0};//考虑到要不断翻转,我们用0和1代表棋盘黑白两种颜色,这样翻转就可以用“非”运算符来代替了 int dir[5][2]={{-1,0},{1,0},{0,-1},{0,1},{0,0}}; int sum;

For_ACM的博客 288

[POJ1753]Flip Game【枚举】

Flip Game Time Limit: 1000MS Memory Limit: 65536K Description Flip game is played on a rectangular 4x4 field with two-sided pieces placed on each of its 16 squares. On...

aiweiluan5095的博客 164

POJ1753——DFS+枚举——Flip Game

Description Flip game is played on a rectangular 4x4 field with two-sided pieces placed on each of its 16 squares. One side of each piece is white and the other one is black and each piece is...

dgjm4087的博客 2261

POJ 1753 Flip Game【翻转棋盘+枚举+dfs】

方法:一、枚举(此处所用)           二、用二进制记录下标(尚未实现)          三、类比于玩魔方游戏(思路来自黄超,尚未实现) 原题链接:http://poj.org/problem?id=1753 Flip Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissi

free斩 6662
上一篇: 最小生成数
下一篇: poj-2965 The Pilots Brothers' refrigerator -- 枚举
RIPwangh
博客等级 码龄12年 42粉丝 172原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值