POJ-3279 C语言写法

POJ-3279-Fliptile

Farmer John knows that an intellectually satisfied cow is a happy cow who will give more milk. He has arranged a brainy activity for cows in which they manipulate an M × N grid (1 ≤ M ≤ 15; 1 ≤ N ≤ 15) of square tiles, each of which is colored black on one side and white on the other side.

As one would guess, when a single white tile is flipped, it changes to black; when a single black tile is flipped, it changes to white. The cows are rewarded when they flip the tiles so that each tile has the white side face up. However, the cows have rather large hooves and when they try to flip a certain tile, they also flip all the adjacent tiles (tiles that share a full edge with the flipped tile). Since the flips are tiring, the cows want to minimize the number of flips they have to make.

Help the cows determine the minimum number of flips required, and the locations to flip to achieve that minimum. If there are multiple ways to achieve the task with the minimum amount of flips, return the one with the least lexicographical ordering in the output when considered as a string. If the task is impossible, print one line with the word “IMPOSSIBLE”.
INPUT
Line 1: Two space-separated integers: M and N
Lines 2… M+1: Line i+1 describes the colors (left to right) of row i of the grid with N space-separated integers which are 1 for black and 0 for white
OUTPUT
Lines 1… M: Each line contains N space-separated integers, each specifying how many times to flip that particular location.
Sample Input
4 4
1 0 0 1
0 1 1 0
0 1 1 0
1 0 0 1
Sample output
0 0 0 0
1 0 0 1
1 0 0 1
0 0 0 0

题面翻译:

农场主约翰知道,一头知足的母牛是一头快乐的母牛,它会提供更多的牛奶。他为奶牛安排了一项脑力活动,让它们操纵一个M×N的方砖网格(1≤M≤15;1≤N≤15),每个方砖的一面是黑色,另一面是白色。
正如人们所猜测的,当一块白色的瓷砖被翻转时,它会变成黑色;当一块黑色的瓷砖被翻转时,它会变成白色。奶牛在翻转瓷砖时会得到奖励,使每一块瓷砖的白色面朝上。然而,奶牛有相当大的蹄子,当它们试图翻转某个瓷砖时,也会翻转所有相邻的瓷砖(与翻转的瓷砖共享一个完整边缘的瓷砖)。因为翻筋斗很累,奶牛想尽量减少翻筋斗的次数。
帮助奶牛确定所需翻转的最小次数,以及翻转的位置以达到最小值。如果有多个方法以最小的翻转量来完成任务,
== 则将被认为是字符串的输出中返回的字典顺序最少的一个返回 。==
如果任务不可能完成,请打印一行“不可能”字样。
输入
第1行:两个空格分隔的整数:M和N
第2行。。M+1:i+1行表示网格第一行的颜色(从左到右),用N个空格分隔的整数表示,1表示黑色,0表示白色
输出
第1行。。M: 每行包含N个空格分隔的整数,每个整数指定翻转特定位置的次数。
样本输入
4 4
1 0 0 1
0 1 1 0
0 1 1 0
1 0 0 1
样本输出
0 0 0 0
1 0 0 1
1 0 0 1
0 0 0 0
困扰了俺10个小时的题,最后才发现如果翻转次数相同还要字典序输出~~( 淦) ~~
说一下思路吧

1.上一行的地砖都可以通过翻转下一行来翻转它
2.翻转所有奇数次结果都相同,偶数次同理

首先遍历第一行所有的翻转结果,每次遍历后->将第二行及以后的每一行都用来翻转上一行还是1的砖块,结束之后判断最后一行黑白,只要有一个黑的这次的结果就是错的,如果全白则将与最小步数比较,如果更小就存起来,相等的话就从00开始比较两个答案的字典序,如果新的结果字典序小就把答案重置;
**全局变量 **

#include<stdio.h>
#include<string.h>		//用到了memset
#include<math.h>    //会用到pow
int m,n;	//地图行列
int map[20][20],a[20][20],b[20][20],ans[20][20],ann=0x3f3f3f3f; //map是存地图,a是零时地图,每次遍历结束都重置,b是存本次遍历翻转的砖块,ans存当前所有答案中字典数和步数最小的答案
int xyz[20]; 	//将2进制数用数组一存,,,就是列长的01序列从0000-1111

** 用到的函数 **

int baoli(void); //主程序
void res(void); //用来在每次遍历且翻转结束后重置地图
void two(int);	//因为要遍历第一行的所有翻转结果,最多也就是2^16次方,遍历0-2^16然后转为二进制就是16个01序列
void fan(int,int);	//翻转函数,用来翻转上下左右和他自己
int panduan(void);	//判断最后一行是否全白
void zuihou(void);	//用来保存答案
int bijiao(void);	//用来比较两个答案的字典序

** main函数**

int main()
{
	int i,j;
 	scanf("%d %d",&n,&m);
 	for(i=1;i<=n;i++)
 	{
 		for(j=1;j<=m;j++)
 		{
 			scanf("%d",&map[i][j]); //读地图,从1开始解决翻转中越界问题
		 }
	 }
	 res();     //将a重置为与map相同
	int p=baoli();  //保存主函数返回值

	if(p!=-1) //如果返回值不为-1就直接输出答案
	{
			for(i=1;i<=n;i++)
		{
			for(j=1;j<=m;j++)
			{
				printf("%d ",ans[i][j]);
			}
			printf("\n");
		}
	}
	else  //否则就emmm
	{
		printf("IMPOSSIBLE\n");
	}
	return 0;
}

具体函数实现
** void res(void)**

void res(void)
{
	int i,j;
	for(i=1;i<=n;i++)
	{
		for(j=1;j<=m;j++)
		{
			a[i][j]=map[i][j];   //将a重置为map
		}
	}
	return;
}

void fan(int,int)

void fan(int x,int y)	//翻转函数
{
	a[x][y]=(a[x][y]+1)%2;     //+1再取余相当于反转01
	a[x-1][y]=(a[x-1][y]+1)%2;
	a[x][y-1]=(a[x][y-1]+1)%2;
	a[x+1][y]=(a[x+1][y]+1)%2;
	a[x][y+1]=(a[x][y+1]+1)%2;	
	return;
}

int panduan(void)

int panduan(void)  //用来判断最后一行是否还有黑砖,有就返回0否则返回1
{
	int i,t=1;
	for(i=1;i<=m;i++)
	{
		if(a[n][i]==1)
		{
			t=0;
			break;
		}
	}
	return t;
}

void two(int)

void two(int step)   //将step转换为二进制并用数组从xyz[1]开始存起来方便用
{
   int i,j=1;
   memset(xyz,0,sizeof(xyz));   //清零xyz
   i=step;
   while(i)
   {
    xyz[j]=i%2;
    i/=2;
    j++;
   }
   return;
}

int bijiao(void)

int bijiao()   //当步数相同,比较ans和b的字典序零时数组字典序小返回1
{
	int i,j,t=1;
	for(i=1;i<=n;i++)
	{
		for(j=1;j<=m;j++)
		{
			if(b[i][j]>ans[i][j])
			{
				t=0;
				break;
			}
		}
	}
	return t;
}

void zuihou(void)

void zuihou()   //显然,这是b的步数或者字典序小的时候覆盖ans的函数
{
	int i,j;
	for(i=1;i<=n;i++)
	{
		for(j=1;j<=m;j++)
		{
			ans[i][j]=b[i][j];
		}
	}
	return ;
}
int baoli(void)
int baoli(void)   //取名为暴力代表着本羸弱的暴力倾向
{
	int sum=0;			//存本次遍历的步数
	int i,j,k;	
	for(i=0;i<=pow(2,m)-1;i++)			//从0到pow(2,m)-1转换为二进制就是从00000-111111
	{
		two(i);				//转换为二进制
		res();				//重置a数组
		sum=0;
		memset(b,0,sizeof(b));		//重置零时翻转数组
		for(j=1;j<=m;j++)		//当二进制数组某一位为1时翻转对应第一行方块
		{
			if(xyz[j]==1)
			{
				fan(1,j);			//翻转emm
				b[1][j]=1;			//存翻转的方块
				sum++;				//步数++
			}
		}
		for(j=2;j<=n;j++)		//从第二行开始遍历,上一行对应方块为1时翻转本行方块
		{	
			for(k=1;k<=m;k++)
			{
				if(a[j-1][k]==1)
				{
					fan(j,k);
					b[j][k]=1;
					sum++;
				}
			}
		}
		 	 	if(panduan()==1)  //判断最后一行是否全白
		 	 	{
		 	 		if(sum<ann)
		 	 		{
		 	 			ann=sum;
		 	 			zuihou();		
		 	 			
					  }
					  else if(ann==sum)//步数与全局最小步数相等时判断字典序
					  {
					  	if(bijiao())
					  	{
					  		zuihou();
						  }
					  }
				  }
		
	}
	if(ann==0x3f3f3f3f)			//如果没有符合条件的,返回-1
	return -1;
	else
	{
		return 0;			
	}	
}

最后放ac代码

#include<stdio.h>
#include<string.h>
#include<math.h>
int m,n;
int map[20][20],a[20][20],b[20][20],ans[20][20],ann=0x3f3f3f3f;
int xyz[20];
int baoli(void);
void res(void);
void two(int);
void fan(int,int);
int panduan(void);
void zuihou(void);
int bijiao(void);
int main()
{
	int i,j;
 	scanf("%d %d",&n,&m);
 	for(i=1;i<=n;i++)
 	{
 		for(j=1;j<=m;j++)
 		{
 			scanf("%d",&map[i][j]);
 			
		 }
	 }
	 res();
	int p=baoli();

	if(p!=-1)
	{
			for(i=1;i<=n;i++)
		{
			for(j=1;j<=m;j++)
			{
				printf("%d ",ans[i][j]);
			}
			printf("\n");
		}
	}
	else
	{
		printf("IMPOSSIBLE\n");
	}
	
	
	return 0;
}
int baoli(void)
{
	int sum=0;
	int i,j,k;
	for(i=0;i<=pow(2,m)-1;i++)
	{
		two(i);
		res();
		sum=0;
		memset(b,0,sizeof(b));
		for(j=1;j<=m;j++)
		{
			if(xyz[j]==1)
			{
				fan(1,j);
				b[1][j]=1;
				sum++;
			}
		}
		for(j=2;j<=n;j++)
		{
			for(k=1;k<=m;k++)
			{
				if(a[j-1][k]==1)
				{
					fan(j,k);
					b[j][k]=1;
					sum++;
				}
			}
		}
		 	 	if(panduan()==1)
		 	 	{
		 	 		if(sum<ann)
		 	 		{
		 	 			ann=sum;
		 	 			zuihou();
		 	 			
					  }
					  else if(ann==sum)
					  {
					  	if(bijiao())
					  	{
					  		zuihou();
						  }
					  }
				  }
		
	}
	if(ann==0x3f3f3f3f)
	return -1;
	else
	{
		return 0;
	}	
}
int bijiao()
{
	int i,j,t=1;
	for(i=1;i<=n;i++)
	{
		for(j=1;j<=m;j++)
		{
			if(b[i][j]>ans[i][j])
			{
				t=0;
				break;
			}
		}
	}
	return t;
}
void zuihou()
{
	int i,j;
	for(i=1;i<=n;i++)
	{
		for(j=1;j<=m;j++)
		{
			ans[i][j]=b[i][j];
		}
	}
	
	
	
	return ;
}
int panduan(void)
{
	int i,t=1;
	for(i=1;i<=m;i++)
	{
		if(a[n][i]==1)
		{
			t=0;
			break;
		}
	}
	return t;
}
void two(int step)
{
   int i,j=1;
   memset(xyz,0,sizeof(xyz));
   i=step;
   while(i)
   {
    xyz[j]=i%2;
    i/=2;
    j++;
    
   }
   return;
}
void res(void)
{
	int i,j;
	for(i=1;i<=n;i++)
	{
		for(j=1;j<=m;j++)
		{
			a[i][j]=map[i][j];
		}
	}
	
	return;
		
}
void fan(int x,int y)
{
	a[x][y]=(a[x][y]+1)%2;
	a[x-1][y]=(a[x-1][y]+1)%2;
	a[x][y-1]=(a[x][y-1]+1)%2;
	a[x+1][y]=(a[x+1][y]+1)%2;
	a[x][y+1]=(a[x][y+1]+1)%2;	
	return;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值