Description
- Choose any one of the 16 pieces.
- 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).
![](https://i-blog.csdnimg.cn/blog_migrate/14b4b3ec0b5261bea3a5ad9f1313252c.jpeg)
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
Output
Sample Input
bwwb bbwb bwwb bwww
Sample Output
4
#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 1 010 101 010 4 3 2 1 011 110 011 110 3 4 2 2 0110 0111 0000 0 0 0 0
示例输出
4 6 -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;
}