poJ 1753(dfs) Flip Game

Flip Game
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 46061 Accepted: 19735

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

这个题目是一个比较不错的DFS的题目,作为萌新,刚开始学习DFS,慢慢不断了解DFS,其实感觉它没有自己想想的那么难,更好的理解,它就是一棵树,利用前序遍历这样来做,不懂树的遍历方法,可以手动百度下。

#include<stdio.h>
#include<string.h>
int step =0x7fffffff;
void dfs(int a,int b,char c[10][10])
{
	int i,j,sum=0;
	char ch[10][10];
	for(i=0;i<4;i++)//每次DFs后都扫描一边是否满足题意
	{
		for(j=0;j<4;j++)
		{
			if(c[i][j] == 'b')
				sum++;
		}
	}
	if(sum==0 || sum==16)
	{
		if(b < step)
			step=b;
		return ;
	}
	if(a==16)//a可以看过是扫描的棋子的数
		return ;
	for(i=0;i<4;i++)
		strcpy(ch[i],c[i]);
	dfs(a+1,b,ch);//当前位置不作处理向下递归 
	int r=a/4; //行数
	int w=a%4;//列数 
	if(ch[r][w]=='b')//翻转当前的状态 
		ch[r][w]='w';
	else
		ch[r][w]='b';
	if(r>=1)//行数大于等于一 代表有上界 
	{
		if(ch[r-1][w]=='b')
			ch[r-1][w]='w';
		else
			ch[r-1][w]='b';
	}
	if(r<=2)//说明拥有下界
	{
		if(ch[r+1][w]=='b')
			ch[r+1][w]='w';
		else
			ch[r+1][w]='b';
	} 
	if(w>=1)//拥有左边的界限 
	{
		if(ch[r][w-1]=='b')
			ch[r][w-1]='w';
		else
			ch[r][w-1]='b';
	}
	if(w<=2)//拥有右边的界限
	{
		if(ch[r][w+1]=='b')
			ch[r][w+1]='w';
		else
			ch[r][w+1]='b';
	 } 
	dfs(a+1,b+1,ch); 
}
int main()
{
	int i;
	char a[10][10];
	for(i=0;i<4;i++)	
		scanf("%s",a[i]);
	dfs(0,0,a);
	if(step == 0x7fffffff)
		printf("Impossible\n");
	else 
		printf("%d\n",step);
	return 0;
} 

就是查询了每种情况,然后找到最短的那种,记录下来~数据量大的话DFS肯定超时,因为它就和暴力差不多了,每种情况都扫描到了。


源码来自:https://pan.quark.cn/s/41b9d28f0d6d 在信息技术领域中,jQuery作为一个广受欢迎的JavaScript框架,显著简化了诸多操作,包括对HTML文档的遍历、事件的管理、动画的设计以及Ajax通信等。 本篇文档将深入阐释如何运用jQuery达成一个图片自动播放的功能,这种效果常用于网站的轮播展示或幻灯片演示,有助于优化用户与页面的互动,使网页呈现更加动态的视觉体验。 为了有效实施这一功能,首先需掌握jQuery的核心操作。 通过$符号作为接口,jQuery能够迅速选取DOM组件,例如$("#id")用于选取具有特定ID的元素,而$(".class")则能选取所有应用了某类class的元素。 在选定元素之后,可以执行多种行为,诸如事件监听、样式的变更、内容的更新以及动画的制作等。 关于“一个基于jQuery的图片自动播放功能”,首要任务是准备一组图片素材,这些素材将被整合至一个容器元素之中。 例如,可以构建一个div元素,将其宽度设定为单张图片的尺寸,再借助CSS实现溢出内容的隐藏,从而构建出水平滚动的初始框架。 ```html<div id="slider"> <img src="image1.jpg" alt="Image 1"> <img src="image2.jpg" alt="Image 2"> <!-- 更多图片内容... --></div>```接着,需要编写jQuery脚本以实现图片的自动切换。 这通常涉及到定时器的运用,以设定周期性间隔自动更换当前显示的图片。 通过使用`.fadeOut()`和`.fadeIn()`方法,能够实现图片间的平滑过渡,增强视觉效果。 ```javascript$(document).re...
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值