• Filp Game


    Flip Game
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 25573   Accepted: 11052

    题目链接: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 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

    代码实现:
     1 #include<stdio.h>
     2 #include<string.h>
     3 #include<stdlib.h>
     4 int map[8][8];
     5 int step;
     6 int flag;
     7 int check()
     8 {
     9     int i,j;
    10     for(i=1;i<=4;i++)
    11     for(j=1;j<=4;j++)
    12     if(map[i][j]!=map[1][1])
    13     return 0;
    14     return 1;
    15 }
    16 void fanzhuan(int x,int y)
    17 {
    18     map[x][y+1]=!map[x][y+1];
    19     map[x][y-1]=!map[x][y-1];
    20     map[x+1][y]=!map[x+1][y];
    21     map[x-1][y]=!map[x-1][y];
    22     map[x][y]=!map[x][y];
    23 }
    24 void dfs(int x,int y,int sum)
    25 {
    26     if(sum==step)
    27     {
    28         flag=check();
    29         return ;
    30     }
    31     if(x<=4)
    32     {
    33         fanzhuan(x,y);
    34         if(y<=3)//必须是3,因为下面的y+1让y变成了4,达到最大值
    35             dfs(x,y+1,sum+1);
    36         else dfs(x+1,1,sum+1);
    37         if(flag==1)return ;
    38         else
    39         {
    40             fanzhuan(x,y);
    41             if(y<=3)
    42             dfs(x,y+1,sum);
    43             else
    44             dfs(x+1,1,sum);
    45         }
    46     }
    47     else return ;
    48 }
    49 int main()
    50 {
    51     memset(map,0,sizeof(map));
    52     int i;
    53     char str[6][6];
    54     for(i=0;i<=3;i++)
    55     scanf("%s",str[i]);
    56     int t;
    57     for(i=0;i<=3;i++)
    58     {
    59         for(t=0;t<=3;t++)
    60         if(str[i][t]=='b')
    61         map[i+1][t+1]=1;
    62     }
    63     for(step=0;step<=16;step++)
    64     {
    65         dfs(1,1,0);
    66         if(flag)
    67         {
    68             printf("%d
    ",step);
    69             break;
    70         }
    71     }
    72     if(flag==0)printf("Impossible
    ");
    73     return 0;
    74 }
    View Code
  • 相关阅读:
    CopyOnWriteArrayList
    volatile可见性和指令重排
    数据库的隔离级别 小强斋
    Hibernate>component映射和复合主键映射 小强斋
    数据库的隔离级别 小强斋
    hibernate>多对多关联映射 小强斋
    hibernate>Collection映射 小强斋
    hibernate>继承 小强斋
    hibernate>继承 小强斋
    hibernate>lazy 小强斋
  • 原文地址:https://www.cnblogs.com/kuangdaoyizhimei/p/3254483.html
Copyright © 2020-2023  润新知