POJ 1753 Flip Game 状态压缩,暴力 难度:1
Flip Game
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 4863 Accepted: 1983
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:
Choose any one of the 16 pieces.
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
Source
Northeastern Europe 2000
大意:翻转棋,每次必须翻转相邻的所有和本身棋子,也就是说如果存在就是上下左右本身,棋盘4*4,全白全黑都算赢
输入:初始状态,4*4行
输出:单行输出需要多少步,如果无解是“Impossible”
#include <cstdio>
using namespace std;
int vis[65536];//就一组数据所以不初始化
int heap[65536];//16<32,用int和0,1存储整个棋盘
int index;//heap里存了多少组数据
int inver(int l,int i,int j){
int t=l;
if(i)l^=1<<(19-j-4*i);//按照左上角i=j=0;
if(i!=3)l^=1<<(11-j-4*i);
if(j)l^=1<<(16-i*4-j);
if(j!=3)l^=1<<(14-i*4-j);
l^=1<<(15-i*4-j);
if(vis[l]==0){
vis[l]=vis[t]+1;
heap[index++]=l;
}
return l;//没用上
}
int bfs(){
for(int k=0;k<index;k++){
if(heap[k]==0||heap[k]==65535)return vis[heap[k]];/*这题输入数据少*/
for(int i=0;i<4;i++){/*错误思路,循环顺序i,j,k,如果是对每一个翻转位置都尝试所有状态,
那么可能在翻转到某个位置之前,输出非最小步数*/
for(int j=0;j<4;j++){
inver(heap[k],i,j);
}
}
}
return -1;
}
int main(){
int l=0;
char ch;
for(int i=0;i<4;i++){
for(int j=0;j<4;j++){
ch=getchar();
if(ch=='b')l^=1<<(15-4*i-j);//输入数据小,直接翻转
}
getchar();
}
vis[l]=1;
heap[index++]=l;
int ans=bfs()-1;//为了区别有无填充,vis[i]=1,但实际翻转次数为0
if(ans>-1)printf("%d\n",ans);
else printf("Impossible\n");
return 0;
}
POJ 1753 Flip Game 状态压缩,暴力 难度:1的更多相关文章
- POJ 1753 Flip Game(状态压缩+BFS)
题目网址:http://poj.org/problem?id=1753 题目: Flip Game Description Flip game is played on a rectangular 4 ...
- 枚举 POJ 1753 Flip Game
题目地址:http://poj.org/problem?id=1753 /* 这题几乎和POJ 2965一样,DFS函数都不用修改 只要修改一下change规则... 注意:是否初始已经ok了要先判断 ...
- poj 1753 Flip Game(bfs状态压缩 或 dfs枚举)
Description Flip game squares. One side of each piece is white and the other one is black and each p ...
- POJ 1753 Flip Game (状态压缩 bfs+位运算)
Flip game is played on a rectangular 4x4 field with two-sided pieces placed on each of its 16 square ...
- POJ 1753. Flip Game 枚举or爆搜+位压缩,或者高斯消元法
Flip Game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 37427 Accepted: 16288 Descr ...
- POJ 1753 Flip Game (状压+暴力)
题目链接:http://poj.org/problem?id=1753 题意: 给你一个4*4的棋盘,上面有两种颜色的棋子(一种黑色,一种白色),你一次可以选择一个棋子翻转它(黑色变成白色,同理反之) ...
- POJ - 1753 Flip Game(状压枚举)
https://vjudge.net/problem/POJ-1753 题意 4*4的棋盘,翻转其中的一个棋子,会带动邻接的棋子一起动.现要求把所有棋子都翻成同一种颜色,问最少需要几步. 分析 同一个 ...
- POJ 1753 Flip Game(高斯消元+状压枚举)
Flip Game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 45691 Accepted: 19590 Descr ...
- POJ 3254. Corn Fields 状态压缩DP (入门级)
Corn Fields Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 9806 Accepted: 5185 Descr ...
随机推荐
- C#中“==”和equals()的区别
如以下代码: 1 2 3 4 5 6 7 8 9 int age = 25; short newAge = 25; Console.WriteLine(age == newAge); //t ...
- Android ViewFlipper的使用分析
[ViewFlipper]——基础 1.ViewPager 和ViewFliping的区别: 最显著的区别就是ViewPager在滑动的时候内部的View默认就能够跟随手指滑动,而 ViewFlipi ...
- 网络性能测试工具iperf详细使用图文教程【转载】
原文:http://blog.163.com/hlz_2599/blog/static/142378474201341341339314/ 参考:http://man.linuxde.net/iper ...
- openfire控制台登录不了的解决
不知道为什么,openfire服务器后台,发生几次密码都登录不上去的问题,密码肯定没错,第一次就是默认的用户名:admin,密码:admin,然,第三天,说我密码错误,后来重装软件才弄好, 然后又发生 ...
- 闹钟--alarmManager
1.AlarmManager,顾名思义,就是“提醒”,是Android中 常用的一种系统级别的提示服务,在特定的时刻为我们广播一个指定的Intent.简单的说就是我们设定一个时间,然后在该时间到来 时 ...
- Java内部接口的调用方式
package com.hs.review; public class Person { public static void main(String[] args) { Person p1 = ne ...
- ajax views
https://julian.pustkuchen.com/en/drupal-7-api-trigger-views-ajax-refresh-javascript-or-php-using-aja ...
- Docker 使用指南 (二)—— 搭建本地仓库
版权声明:本文由田飞雨原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/94 来源:腾云阁 https://www.qclou ...
- linux笔记:shell基础和bash的基本功能
shell的优势:可以直接调用linux系统命令 shell脚本的基本写法:脚本名以.sh结尾程序的第一行为#!/bin/bash,告诉系统这是一个shell脚本以#作为注释shell脚本的执行方式: ...
- decorators.xml的用法 (转)
spring,hibernate框架做的,对了,还有jQuery. 我用jquery做异步请求到后台,生成json数据返回前台生成下拉输入框,请求到后台以后,成功生成了json数据并根据struts的 ...