poj 1753 Flip Game 枚举+递推 poj 3363 Annoying painting tool

来源:互联网 发布:我国少儿编程教育现状 编辑:程序博客网 时间:2024/05/14 09:59

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

bwwbbbwbbwwbbwww

Sample Output

4
这道题目以前用的递归做的上次碰到同样的竟然因为图太大tle了,所以学习了用递推
就是枚举第一行然后下面的根据第一行的情况进行暴力就行。比如你要让图全部变成b
当你枚举完成后图中第一行为第一个为w那么就让第二行第一个反转,因为只有这样才能不损坏到它周围的地方。
代码:
#include<cstdio>#include<iostream>#include<algorithm>#include<cstring>using namespace std;char map[8][8];int a[8][8],b[8][8],ch;int step;int pan(){    for(int i=0; i<4; i++)        for(int j=0; j<4; j++)            if(a[i][j]!=a[0][0])                return 0;    return 1;}int fan(int x,int y){    a[x][y]=!a[x][y];    if(x>0)        a[x-1][y]=!a[x-1][y];    if(x<3)        a[x+1][y]=!a[x+1][y];    if(y>0)        a[x][y-1]=!a[x][y-1];    if(y<3)        a[x][y+1]=!a[x][y+1];    step++;}int dfs(int x,int y){    for(int j=y; j<4; j++)        if(a[x][j]!=ch)        {            fan(x+1,j);        }    for(int i=x+1; i<3; i++)        for(int j=0; j<4; j++)            if(a[i][j]!=ch)            {                fan(i+1,j);            }    if(pan())        return step;    else        return step+20;}int main(){        for(int i=0; i<4; i++)            scanf("%s",map[i]);        for(int i=0; i<4; i++)            for(int j=0; j<4; j++)                if(map[i][j]=='b')                    b[i][j]=1;                else                    b[i][j]=0;        int maxl=20;        int x=20;        step=0;        for(int p=0; p<4; p++)        {            step=0;            for(int x=0; x<4; x++)                for(int y=0; y<4; y++)                    a[x][y]=b[x][y];            ch=a[0][p];            x=dfs(0,p);            if(maxl>x)                maxl=x;        }        for(int i=0; i<4; i++)        {            for(int p=0; p<4; p++)            {                for(int x=0; x<4; x++)                    for(int y=0; y<4; y++)                        a[x][y]=b[x][y];                step=0;                fan(0,i);                ch=a[0][p];                x=dfs(0,p);                if(maxl>x)                    maxl=x;            }            for(int j=0; j<i; j++)            {                for(int p=0; p<4; p++)                {                    step=0;                    if(i==j)                        continue;                    for(int x=0; x<4; x++)                        for(int y=0; y<4; y++)                            a[x][y]=b[x][y];                    fan(0,i);                    fan(0,j);                    ch=a[0][p];                    x=dfs(0,p);                    if(maxl>x)                        maxl=x;                }                for(int k=0; k<j; k++)                {                    for(int p=0; p<4; p++)                    {                        step=0;                        if(i==j||i==k||j==k)                            continue;                        for(int x=0; x<4; x++)                            for(int y=0; y<4; y++)                                a[x][y]=b[x][y];                        fan(0,i);                        fan(0,j);                        fan(0,k);                        ch=a[0][p];                        x=dfs(0,p);                        if(maxl>x)                            maxl=x;                    }                    for(int l=0; l<k; l++)                    {                        for(int p=0; p<4; p++)                        {                            step=0;                            if(i==j||i==k||i==l||j==k||j==l||k==l)                                continue;                            for(int x=0; x<4; x++)                                for(int y=0; y<4; y++)                                    a[x][y]=b[x][y];                            fan(0,i);                            fan(0,j);                            fan(0,k);                            fan(0,l);                            ch=a[0][p];                            x=dfs(0,p);                            if(maxl>x)                                maxl=x;                        }                    }                }            }        }        if(maxl!=20)            printf("%d\n",maxl);        else            printf("Impossible\n");    return 0;}

题目描述

Maybe you wonder what an annoying painting tool is? First of all, the painting tool we speak of supports only black and white. Therefore, a picture consists of a rectangular area of pixels, which are either black or white. Second, there is only one operation how to change the colour of pixels:

Select a rectangular area of r rows and c columns of pixels, which is completely inside the picture. As a result of the operation, each pixel inside the selected rectangle changes its colour (from black to white, or from white to black).

Initially, all pixels are white. To create a picture, the operation described above can be applied several times. Can you paint a certain picture which you have in mind?

输入

The input contains several test cases. Each test case starts with one line containing four integers n, m, r and c. (1 ≤ r ≤ n ≤ 100, 1 ≤ c ≤ m ≤ 100), The following nlines each describe one row of pixels of the painting you want to create. The ith line consists of m characters describing the desired pixel values of the ith row in the finished painting (\'0\' indicates white, \'1\' indicates black).

The last test case is followed by a line containing four zeros.

输出

For each test case, print the minimum number of operations needed to create the painting, or -1 if it is impossible.


示例输入

3 3 1 10101010104 3 2 10111100111103 4 2 20110011100000 0 0 0

示例输出

46-1

提示

这道题目因为他从当前的点开始反转以后不涉及前面已经反转的变化,算一考虑的情况要少

#include<cstdio>#include<iostream>#include<algorithm>#include<cstring>using namespace std;int n,m,r,c;char map[105][105],a[105][105];int step;int panduan(){    for(int i=0; i<n; i++)        for(int j=0; j<m; j++)            if(a[i][j]!=map[i][j])                return 0;    return 1;}int dfs(int x,int y){    for(int i=x; i<n; i++)        for(int j=y; j<m; j++)            if(map[i][j]!=a[i][j])            {                if(i+r<=n&&j+c<=m)                {                    //printf("%d %d\n",i,j);                    step++;                    for(int k=i; k<i+r; k++)                        for(int l=j; l<j+c; l++)                        {                            if(a[k][l]=='1')                                a[k][l]='0';                            else                                a[k][l]='1';                        }                    /*for(int k=0; k<n; k++)                    {                        for(int l=0; l<m; l++)                            printf("%c",a[k][l]);                        printf("\n");                    }                    printf("\n");*/                }            }    if(panduan())        return 1;    else        return 0;}int main(){    while(~scanf("%d %d %d %d",&n,&m,&r,&c))    {        if(n==0&&m==0&&r==0&&c==0)            break;        for(int i=0; i<n; i++)            scanf("%s",&map[i]);        int e=0;        for(int i=0; i<m; i++)        {            step=0;            memset(a,'0',sizeof(a));            int x=dfs(0,i);            if(x)            {                e=1;                printf("%d\n",step);                break;            }        }        if(!e)            printf("-1\n");    }    return 0;} 


0 0
原创粉丝点击