九宫格游戏

有一个类九宫格(如下),灰色三角形一次编号0-9,选中其中一个作为空格,其余各编号三角形中可能被填充红、蓝、绿、黄四种颜色,

求从任意一个起始布局(init)开始,变换至任意的目标布局(goal),如果成功,返回0;如果失败,则求至少需要替换多少个格子的填充色,然后方能成功,返回需替换的格子数。

 

举例:init="BG*YRGRRYR",goal="BGGY*YRRRR",布局字符串第i位表示第i个格中颜色(RGBY依次是颜色,*为空格),经由如下变换序列,由init可以成功变换为goal,所以无需改变任何一格的颜色,返回0

对此题的第一反应是觉得题型像深度遍历求可能解一类,然后对比所有可能序列与目标序列的差异并取最小值,但是实际深度遍历并不可行,因为无法确定遍历的结束边界(即使空格到了目标格也还可以继续,因为可此导出其他序列),所以否定深度遍历。

 

再回头看看宫格,想想其中奥妙,发现有一些规律:

1)任意连着空格的两个格子,颜色可一个互换,其他格子颜色保持不变。比如假设空格是7,则6和8可以在保持3和4不变情况下互换位置,变换过程:7沿着6->3->4->8走一圈再回归原位位,于是6到达8的位置,好比除7外3486 时针移动了一格,然后固定6,再类似的 时针转动348,于是8到达6的原来位置,3、4和7都不动,6和8互换完成。

2)基于第一个规律,空格可以移动至某个位置,互换某两个相邻的格子,然后再沿原路返回,于是可以达到互换任意两个空格的效果,与空格的实际位置并无关系。

 

有了如上规律,可以得出思路,首先将空格移到目标格,然后按需要互换任意空格的颜色,使其与目标布局尽量匹配,如果不行,则替换颜色,所以实际解很简单,就等价于求init和goal相异颜色的个数除2。

 

 

 

 

java关键代码: mText = (TextView) findViewById(R.id.textView1); mImageView01 = (ImageView) findViewById(R.id.m1); mButton = (Button) findViewById(R.id.button1); randon(); mImageView01.setOnClickListener(new View.OnClickListener() { @SuppressWarnings("deprecation") public void onClick(View v) { if(check1 == true && num > 0 && num1 == false) { num--; num1 = true; choose1 = true; if(turn1 == false) { turnAnim(mImageView01,s1[0]); if(s1[0] == R.drawable.b) { num = 0 ; friendState = true; } turn1 = true; } } }); mButton.setOnClickListener(new Button.OnClickListener() { @SuppressWarnings("deprecation") public void onClick(View v) { friendState = false; xianshi = 3; mText.setText("还剩"+xianshi+"次机会。"); num = 3; }); } /*往回洗牌的动画*/ public void backAnim(final ImageView a) { Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.back_scale); animation.setAnimationListener(new Animation.AnimationListener() { @Override public void onAnimationStart(Animation animation) { } @Override public void onAnimationRepeat(Animation animation) { } @Override public void onAnimationEnd(Animation animation) { a.setImageDrawable(getResources().getDrawable( R.drawable.pbg)); // bool = true; //通过AnimationUtils得到动画配置文件(/res/anim/front_scale.xml),然后在把动画交给ImageView a.startAnimation(AnimationUtils.loadAnimation(MainActivity.this, R.anim.front)); } }); a.startAnimation(animation); } /*翻牌的动画*/ public void turnAnim(final ImageView a,final int b) { Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.back_scale); animation.setAnimationListener(new Animation.AnimationListener() { @Override public void onAnimationStart(Animation animation) { } @Override public void onAnimationRepeat(Animation animation) { } @Override public void onAnimationEnd(Animation animation) { a.setImageDrawable(getResources().getDrawable(b)); // bool = true; //通过AnimationUtils得到动画配置文件(/res/anim/front_scale.xml),然后在把动画交给ImageView a.startAnimation(AnimationUtils.loadAnimation(MainActivity.this, R.anim.front)); } }); a.startAnimation(animation); if (b == R.drawable.b) { mText.setText("哇!你猜对了噢!!拍拍手!"); } else { if(xianshi >= 1) { mText.setText("还剩"+xianshi+"次机会。"); } else{ mText.setText("机会已用完!"); } } } private void randon() { // TODO Auto-generated method stub for (int i = 0; i < 9; i++) { int tmp = s1[i]; int s = (int) (Math.random() * 9); s1[i] = s1[s]; s1[s] = tmp; } } } res/layout/activity_main.xml: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#000000" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".EX04_06" > <LinearLayout android:id="@+id/LayoutButtons3" android:layout_marginTop="5dp" android:layout_width="230dp" android:layout_height="95dp" android:layout_centerHorizontal="true" android:layout_weight="1" android:orientation="horizontal" > <ImageView android:id="@+id/m1" android:layout_margin= "2dp" android:layout_width="60dp" android:layout_height="90dp" android:layout_weight="1" android:src="@drawable/pbg" android:scaleType="centerCrop"/> <ImageView android:id="@+id/m2" android:layout_margin= "2dp" android:layout_width="60dp" android:layout_height="90dp" android:layout_weight="1" android:src="@drawable/pbg" android:scaleType="centerCrop"/> <ImageView android:id="@+id/m3" android:layout_margin= "2dp" android:layout_width="60dp" android:layout_height="90dp" android:layout_weight="1" android:src="@drawable/pbg" android:scaleType="centerCrop" /> </LinearLayout> <LinearLayout android:id="@+id/LayoutButtons4" android:layout_below="@+id/LayoutButtons3" android:layout_width="230dp" android:layout_height="95dp" android:layout_centerHorizontal="true" android:layout_weight="1" android:orientation="horizontal" > <ImageView android:id="@+id/m4" android:layout_below="@+id/LayoutButtons3" android:layout_margin= "2dp" android:layout_width="60dp" android:layout_height="90dp" android:layout_weight="1" android:src="@drawable/pbg" android:scaleType="centerCrop" /> <ImageView android:id="@+id/m5" android:layout_margin= "2dp" android:layout_width="60dp" android:layout_height="90dp" android:layout_weight="1" android:src="@drawable/pbg" android:scaleType="centerCrop" /> <ImageView android:id="@+id/m6" android:layout_margin= "2dp" android:layout_width="60dp" android:layout_height="90dp" android:layout_weight="1" android:src="@drawable/pbg" android:scaleType="centerCrop" /> </LinearLayout> <LinearLayout android:id="@+id/LayoutButtons5" android:layout_below="@+id/LayoutButtons4" android:layout_width="230dp" android:layout_height="95dp" android:layout_centerHorizontal="true" android:layout_weight="1" android:orientation="horizontal" > <ImageView android:id="@+id/m7" android:layout_margin= "2dp" android:layout_width="60dp" android:layout_height="90dp" android:layout_weight="1" android:src="@drawable/pbg" android:scaleType="centerCrop" /> <ImageView android:id="@+id/m8" android:layout_margin= "2dp" android:layout_width="60dp" android:layout_height="90dp" android:layout_weight="1" android:src="@drawable/pbg" android:scaleType="centerCrop" /> <ImageView android:id="@+id/m9" android:layout_margin= "2dp" android:layout_width="60dp" android:layout_height="90dp" android:layout_weight="1" android:src="@drawable/pbg" android:scaleType="centerCrop" /> </LinearLayout> <TextView android:id="@+id/textView1" android:layout_below="@+id/LayoutButtons5" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_marginTop="2dp" android:text="猜猜是哪张" android:textSize="20sp" /> <Button android:id="@+id/button1" android:layout_width="120dp" android:layout_height="60dp" android:layout_alignTop="@+id/textView1" android:layout_centerHorizontal="true" android:text="再玩一次!" /> </RelativeLayout> res/anim/back_ainm.xml: <?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_interpolator"> <scale android:fromXScale="1.0" android:toXScale="0.0" android:fromYScale="1.0" android:toYScale="1.0" android:pivotX="50%" android:pivotY="50%" android:duration="150"/> </set> res/anim/front.xml: <?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_interpolator"> <scale android:fromXScale="0.0" android:toXScale="1.0" android:fromYScale="1.0" android:toYScale="1.0" android:pivotX="50%" android:pivotY="50%" android:duration="150"/> </set>
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值