• [POJ1753]Flip Game


    [POJ1753]Flip Game

    试题描述

    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. 

    输入

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

    输出

    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).

    输入示例

    bwwb
    bbwb
    bwwb
    bwww

    输出示例

    4

    数据规模及约定

    见“试题描述

    题解

    考完 NOIP 又来刷 POJ 啦!

    这题就是个大暴搜嘛。。。

    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    #include <cctype>
    #include <algorithm>
    #include <queue>
    using namespace std;
    
    int read() {
    	int x = 0, f = 1; char c = getchar();
    	while(!isdigit(c)){ if(c == '-') f = -1; c = getchar(); }
    	while(isdigit(c)){ x = x * 10 + c - '0'; c = getchar(); }
    	return x * f;
    }
    
    #define maxn 65546
    #define oo 2147483647
    
    int step[maxn];
    queue <int> Q;
    void BFS(int s) {
    	memset(step, -1, sizeof(step));
    	step[s] = 0;
    	Q.push(s);
    	while(!Q.empty()) {
    		int u = Q.front(); Q.pop();
    		bool Map[4][4];
    		for(int i = 0; i < 4; i++)
    			for(int j = 0; j < 4; j++)
    				Map[i][j] = (u >> i * 4 + j & 1);
    		/*for(int i = 0; i < 4; i++)
    			for(int j = 0; j < 4; j++)
    				printf("%d%c", Map[i][j], j < 3 ? ' ' : '
    ');
    		putchar('
    ');*/
    		for(int i = 0; i < 4; i++)
    			for(int j = 0; j < 4; j++) {
    				Map[i][j] ^= 1;
    				if(i) Map[i-1][j] ^= 1;
    				if(j) Map[i][j-1] ^= 1;
    				if(i < 3) Map[i+1][j] ^= 1;
    				if(j < 3) Map[i][j+1] ^= 1;
    				int v = 0;
    				for(int x = 0; x < 4; x++)
    					for(int y = 0; y < 4; y++)
    						v |= ((int)Map[x][y] << x * 4 + y);
    				if(step[v] < 0) step[v] = step[u] + 1, Q.push(v);
    				Map[i][j] ^= 1;
    				if(i) Map[i-1][j] ^= 1;
    				if(j) Map[i][j-1] ^= 1;
    				if(i < 3) Map[i+1][j] ^= 1;
    				if(j < 3) Map[i][j+1] ^= 1;
    			}
    	}
    	return ;
    }
    
    int main() {
    	int s = 0;
    	char ts[10];
    	for(int i = 0; i < 4; i++) {
    		scanf("%s", ts);
    		for(int j = 0; j < 4; j++)
    			s |= ((ts[j] == 'b') << i * 4 + j);
    	}
    	
    	BFS(s);
    	
    	int all = 65535;
    	if(step[0] < 0 && step[all] < 0) puts("Impossible");
    	else {
    		int ans = oo;
    		if(step[0] >= 0) ans = min(ans, step[0]);
    		if(step[all] >= 0) ans = min(ans, step[all]);
    		printf("%d
    ", ans);
    	}
    	
    	return 0;
    }
    
  • 相关阅读:
    数据库操作语句大全(sql)
    尚未在 Web 服务器上注册ASP.NET 4.5。安装VS15后的问题(转)
    ASP.NET通过http/https的POST方式,发送和接受XML文件内容(转)
    C# 的关键字详细介绍(转)
    12个css高级技巧.html
    CentOS上安装elasticsearch
    springboot例子
    ajax的XmlHttpRequest对象常用方法
    阿里云oss操作
    Python出现Could not find a version that satisfies the requirement openpyxl (from versions: )
  • 原文地址:https://www.cnblogs.com/xiao-ju-ruo-xjr/p/6098824.html
Copyright © 2020-2023  润新知