zoukankan      html  css  js  c++  java
  • Flip Game poj1753

    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 32961   Accepted: 14407

    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
    

    Source

    Northeastern Europe 2000

    dfs枚举

     1 #include<stdio.h>
     2 #include<string.h>
     3 #include<algorithm>
     4 using namespace std;
     5 
     6 int Map[6][6],a[5]={0,0,1,0,-1},b[5]={0,1,0,-1,0};
     7 int flg,step;
     8 
     9 int judge(int Map[6][6])
    10 {
    11     int i,j;
    12     for(int i=1;i<=4;i++)
    13         for(j=1;j<=4;j++)
    14         {
    15             if(Map[i][j]!=Map[1][1])
    16                 return 0;
    17         }
    18     return 1;
    19 }
    20 
    21 int flip(int r,int c)
    22 {
    23     for(int i=0;i<5;i++)
    24     {
    25         if(Map[r+a[i]][c+b[i]]==1)
    26             Map[r+a[i]][c+b[i]]=0;
    27         else
    28             Map[r+a[i]][c+b[i]]=1;
    29     }
    30 }
    31 
    32 void dfs(int r,int c,int deep)
    33 {
    34     int i,j;
    35     if(deep==step)
    36     {
    37         flg=judge(Map);
    38         return;
    39     }
    40     if(flg==1 || r>4)
    41     {
    42         return;
    43     }
    44     flip(r,c);
    45     if(c<4)
    46         dfs(r,c+1,deep+1);
    47     else
    48         dfs(r+1,1,deep+1);
    49     flip(r,c);
    50     if(c<4)
    51         dfs(r,c+1,deep);
    52     else
    53         dfs(r+1,1,deep);
    54     return;
    55 }
    56 
    57 
    58 int main()
    59 {
    60     int i,j;
    61     char k;
    62     memset(Map,0,sizeof(Map));
    63     for(i=1;i<=4;i++)
    64     {
    65         for(j=1;j<=4;j++)
    66         {
    67             scanf("%c",&k);
    68             if(k=='b')
    69                 Map[i][j]=1;
    70         }
    71         getchar();
    72     }
    73     for(step=0;step<=16;step++)
    74     {
    75         dfs(1,1,0);
    76         if(flg)
    77             break;
    78     }
    79     if(flg)
    80         printf("%d
    ",step);
    81     else
    82         printf("Impossible
    ");
    83     return 0;
    84 }
    View Code
  • 相关阅读:
    sqlalchemy访问Oracle数据库报错:UnicodeDecodeError: 'big5' codec can't decode byte 0xfb in position 2: illegal multibyte sequence
    Mac如何安装FastDfs
    Django执行Sql语句笔记
    跑DRF框架分页源码笔记
    Python Paginator分页学习
    Python Excel笔记
    npm run dev报错解决方法
    npm install --global vue-cli 报错 [..................] / rollbackFailedOptional: verb npm-session abfa82f3041ebc02
    MS17_010漏洞攻击Windows7
    虚拟机启动黑屏
  • 原文地址:https://www.cnblogs.com/cyd308/p/4444453.html
Copyright © 2011-2022 走看看