题目:有一个正方形的墙,由N*N个正方形的砖组成,其中一些砖是白色的,另外一些砖是黄色的。Bob是个画家,想把全部的砖都涂成黄色。但他的画笔不好使。当他用画笔涂画第(i, j)个位置的砖时, 位置(i-1, j)、 (i+1, j)、 (i, j-1)、 (i, j+1)上的砖都会改变颜色。请你帮助Bob计算出最少需要涂画多少块砖,才能使所有砖的颜色都变成黄色。
输入
第一行是一个整数n (1≤n ≤15),表示墙的大小。接下来的n行表示墙的初始状态。每一行包含n个字符。第i行的第j个字符表示位于位置(i,j)上的砖的颜色。“w”表示白砖,“y”表示黄砖。
输出
一行,如果Bob能够将所有的砖都涂成黄色,则输出最少需要涂画的砖数,否则输出“inf”。
样例输入
样例输出
5 wwwww wwwww wwwww wwwww wwwww
15
跟关灯问题类似:
如果0——黄色,1——白色
wall[n][j] == (paint[n][j-1] + paint[n][j] + paint[n][j+1] + paint[n-1][j]) % 2 -->wall[n][j] = 0
若 求涂成白色:paint[i+1][j] = (wall[i][j] + paint[i][j] + paint[i][j+1] + paint[i][j-1] + paint[i-1][j]) % 2;
------> paint[i+1][j] = (wall[i][j] + paint[i][j] + paint[i][j+1] + paint[i][j-1] + paint[i-1][j] + 1) % 2;
判断也由wall[n][j] == (paint[n][j-1] + paint[n][j] + paint[n][j+1] + paint[n-1][j]) % 2 -->wall[n][j] = 0
------> wall[n][j] != (paint[n][j-1] + paint[n][j] + paint[n][j+1] + paint[n-1][j]) % 2 -->wall[n][j] = 1
#include<stdio.h>
#include<string.h>
int wall[17][17];
int paint[17][17]; //1——涂色
int min_num;
#include<string.h>
int wall[17][17];
int paint[17][17]; //1——涂色
int min_num;
bool guess(int n)
{
for(int i = 1;i < n;i ++)
{
for(int j = 1;j < n+1;j ++)
{
paint[i+1][j] = (wall[i][j] + paint[i][j] + paint[i][j+1]
+ paint[i][j-1] + paint[i-1][j]) % 2;
}
}
for(int j = 1;j <= n;j ++)
{
if(wall[n][j] != (paint[n][j-1] + paint[n][j] + paint[n][j+1]
+ paint[n-1][j]) % 2)
return false;
}
return true;
}
{
for(int i = 1;i < n;i ++)
{
for(int j = 1;j < n+1;j ++)
{
paint[i+1][j] = (wall[i][j] + paint[i][j] + paint[i][j+1]
+ paint[i][j-1] + paint[i-1][j]) % 2;
}
}
for(int j = 1;j <= n;j ++)
{
if(wall[n][j] != (paint[n][j-1] + paint[n][j] + paint[n][j+1]
+ paint[n-1][j]) % 2)
return false;
}
return true;
}
void enumertate(int n)
{
int i,j;
for(i = 1;i < n;i ++)
paint[1][i] = 0;
while(guess(n) == false)
{
paint[1][1] ++;
i = 1;
while(paint[1][i] > 1)
{
paint[1][i] = 0;
i ++;
paint[1][i] ++;
}
}
{
int i,j;
for(i = 1;i < n;i ++)
paint[1][i] = 0;
while(guess(n) == false)
{
paint[1][1] ++;
i = 1;
while(paint[1][i] > 1)
{
paint[1][i] = 0;
i ++;
paint[1][i] ++;
}
}
}
int main()
{
int t,n;int sum = 0;
char c;
int i,j;
scanf("%d",&t);
while(t --)
{
min_num = 400;
scanf("%d",&n);
memset(wall,0,sizeof(wall)); 不能是memset(wall,0,sizeof(int)*n);
for(i = 1;i <= n;i ++)
{
getchar();
for(j = 1;j <= n;j ++)
{
c = getchar();
if(c == 'w')
{
wall[i][j] = 1;
}
}
}
enumertate(n);
for(i = 1;i <= n;i ++)
{
for(j = 1;j <= n;j ++)
{
sum += paint[i][j];
}
}
if(sum < min_num)
min_num = sum;
if(min_num != 400)
printf("%d\n",min_num);
else
printf("inf\n");
}
}
{
int t,n;int sum = 0;
char c;
int i,j;
scanf("%d",&t);
while(t --)
{
min_num = 400;
scanf("%d",&n);
memset(wall,0,sizeof(wall)); 不能是memset(wall,0,sizeof(int)*n);
for(i = 1;i <= n;i ++)
{
getchar();
for(j = 1;j <= n;j ++)
{
c = getchar();
if(c == 'w')
{
wall[i][j] = 1;
}
}
}
enumertate(n);
for(i = 1;i <= n;i ++)
{
for(j = 1;j <= n;j ++)
{
sum += paint[i][j];
}
}
if(sum < min_num)
min_num = sum;
if(min_num != 400)
printf("%d\n",min_num);
else
printf("inf\n");
}
}