Problem D: Flip Five

大致题意:3 * 3的黑白格,在翻转的时候会本身和四周的都翻转,问最小翻转几次变成全部是白色
解题思路:把3 * 3 = 9 个格子进行全排列,然后穷举然后找翻转的最小次数

#include <iostream>
#include <algorithm>
#include <cstdio>

using namespace std;
int dr[] = {0,1,0,-1,0};
int dc[] = {0,0,1,0,-1};
int a[1000];
bool tmp[10][10];

void change(int s){
    int x = s / 3;
    int y = s - x * 3;
    for(int i = 0;i < 5;i++){
        int xx = x + dr[i];
        int yy = y + dc[i];
        if(xx >= 0 && yy >= 0 && xx < 3 && yy < 3)
            tmp[xx][yy] = !tmp[xx][yy];
    }
}
bool check(){
    for(int i = 0;i < 3;i++){
        for(int j = 0;j < 3;j++)
            if(tmp[i][j])
                return false;
    }
    return true;
}

int main()
{
#ifndef ONLINE_JUDGE
  // freopen("in.in","r",stdin);
#endif
    int t;
    cin >> t;
    while(t--){
        char str[10][10];
        for(int i = 0;i < 3;i++){
            cin >> str[i];
        }
        for(int i = 0;i < 1000;i++)
            a[1000] = 100;
        int num = 0;
        for(int i = 0;i < (1 << 9);i++){
        int cnt = 0;
        for(int r = 0;r < 3;r++){
            for(int c = 0;c < 3;c++){
                if(str[r][c] == ‘*‘)
                    tmp[r][c] = 1;
                else
                    tmp[r][c] = 0;
            }
        }
        for(int j = 0;j < 9;j++){
            if(i & (1 << j)){
                change(j);
                cnt++;
            }
        }
        if(check()){
            a[num++] = cnt;
        }
        }
        sort(a,a+num);
        cout << a[0] << endl;
    }
    return 0;
}

Code

for(int i = 0;i < (1 << 9);i++){
     for(int j = 0;j < 9;j++){
         if(i & (1 << j)){
        }
    }
}        
时间: 2024-10-19 21:39:31

Problem D: Flip Five的相关文章

hdu 4146 Flip Game

Flip Game Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 1800    Accepted Submission(s): 589 Problem Description Flip game is played on a square N*N field with two-sided pieces placed on each

uva10327 - Flip Sort

Flip Sort Sorting in computer science is an important part. Almost every problem can be solved effeciently if sorted data are found. There are some excellent sorting algorithm which has already acheived the lower bound nlgn. In this problem we will a

poj1753 Flip Game(枚举Enum+dfs)

转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://poj.org/problem?id=1753 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 o

Flip Game I &amp;&amp; II

Flip Game I Problem Description: You are playing the following Flip Game with your friend: Given a string that contains only these two characters: + and -, you and your friend take turns to flip two consecutive "++" into "--". The game

Gambler&#39;s Ruin Problem and 3 Solutions

In my stochastic processes class, Prof Mike Steele assigned a homework problem to calculate the ruin probabilities for playing a game where you with $1 with probability p and lose $1 with probability 1-p. The probability of winning is not specified,

POJ 1753 Flip Game (DFS + 枚举)

题目:http://poj.org/problem?id=1753 这个题在开始接触的训练计划的时候做过,当时用的是DFS遍历,其机制就是把每个棋子翻一遍,然后顺利的过了,所以也就没有深究. 省赛前一次做PC2遇到了几乎一模一样的题,只不过是把棋盘的界限由4X4改为了5X5,然后一直跑不出结果来,但是当时崔老湿那个队过了,在最后总结的时候,崔老湿就说和这个题一样,不过要枚举第一行进行优化. 我以为就是恢复第一行然后第二行以此类推,不过手推一下结果是6不是4,就知道这个有问题. 问了崔老湿,问了+

SPOJ IITWPC4F - Gopu and the Grid Problem (双线段树区间修改 区间查询)

Gopu and the Grid Problem Gopu is interested in the integer co-ordinates of the X-Y plane (0<=x,y<=100000). Each integer coordinate contain a lamp, initially all the lamps are in off mode. Flipping a lamp means switching it on if it is in off mode a

POJ1753 Flip Game(bfs、枚举)

链接:http://poj.org/problem?id=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 either i

spoj IITWPC4F - Gopu and the Grid Problem 线段树

IITWPC4F - Gopu and the Grid Problem no tags Gopu is interested in the integer co-ordinates of the X-Y plane (0<=x,y<=100000). Each integer coordinate contain a lamp, initially all the lamps are in off mode. Flipping a lamp means switching it on if