TOJ 4267 An Easy Puz / 深搜

2024-06-15 12:18
文章标签 toj 4267 easy puz

本文主要是介绍TOJ 4267 An Easy Puz / 深搜,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

An Easy Puz

时间限制(普通/Java):1000MS/3000MS     运行内存限制:65536KByte

描述

Wddpdh find an interesting mini-game in the BBS of WHU, called “An easy PUZ”. It’s a 6 * 6 chess board and each cell has a number in the range of 0 and 3(it can be 0, 1, 2 or 3). Each time you can choose a number A(i, j) in i-th row and j-th column, then the number A(i, j) and the numbers around it (A(i-1, j), A(i+1, j),A(i, j-1),A(i, j+1), sometimes there may just be 2 or 3 numbers.) will minus 1 (3 to 2, 2 to 1, 1 to 0, 0 to 3). You can do it finite times. The goal is to make all numbers become 0. Wddpdh now come up with an extended problem about it. He will give you a number N (3 <= N <= 6) indicate the size of the board. You should tell him the minimum steps to reach the goal.

输入

The input consists of multiple test cases. For each test case, it contains a positive integer N(3 <= n <= 6). N lines follow, each line contains N columns indicating the each number in the chess board.

输出

For each test case, output minimum steps to reach the goal. If you can’t reach the goal, output -1 instead.

样例输入

3
1 1 0
1 0 1
0 1 1
3
2 3 1
2 2 1
0 1 0

样例输出

2
3

和上面一题一样 暴力枚举第一行的状态 下一行可以根据上一行推出来 推完看最后一行是否吻合就行

求最小次数全部变成0

可以反向考虑 从全部是0的矩阵变成题目输入的矩阵

深搜时第一行的含义是这个位置需要操作几次

#include <stdio.h>
#include <string.h>
const int MAX = 10;
int n;
int min;
int a[MAX][MAX];
int b[MAX][MAX];int check()
{int i,j,sum,cnt = 0;for(i = 0;i < n; i++){cnt += b[0][i];}for(i = 1; i < n; i++){for(j = 0; j < n; j++){sum = b[i-1][j];if(i > 1)sum += b[i-2][j];if(j > 0)sum += b[i-1][j-1];if(j < n - 1)sum += b[i-1][j+1];b[i][j] = (a[i-1][j] + 12 - sum ) % 4;cnt += b[i][j];}}for(j = 0; j < n; j++){sum = b[n-1][j];if(n > 1)sum += b[n-2][j];if(j > 0)sum += b[n-1][j-1];if(j < n - 1)sum += b[n-1][j+1];if((a[n-1][j] + 12 - sum) % 4)return 0x7ffffff;}return cnt;
}
void dfs(int k)
{if(k == n){int res = check();if(min > res)min = res;return;}int i;for(i = 0;i < 4; i++){b[0][k] = i;dfs(k+1);}
}
int main()
{int t,i,j;while(scanf("%d",&n)!=EOF){	for(i = 0; i < n; i++)for(j = 0; j < n; j++)scanf("%d",&a[i][j]);min = 0x7ffffff;dfs(0);if(min == 0x7ffffff)puts("-1");elseprintf("%d\n",min);}return 0;
}


 

这篇关于TOJ 4267 An Easy Puz / 深搜的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1063429

相关文章

玩转Web之Json(三)-----easy ui怎么把前台显示的dataGird中的所有数据序列化为json,返回到后台并解析

最近做一个项目时,需要在dataGird中插入<input>,即文本输入框,当点击提交时,需要把文本框里填的数据返以及其他列的一些信息以json数组的格式返回到后台,虽然我实现了该功能,但一直没找到简便的方法,今天终于在一位版主的点拨下找到了非常简单的方法。   var all = $("#dg").datagrid("getData");var json =JSON.

玩转Web之easyui(三)-----easy ui dataGird 重新指定url以获取不同数据源信息

如果已经写了一个dataGird并且已经通过url绑定数据源,能不能在其他地方改变url使其从不同数据源获取信息,从而实现查询等操作?答案当然是肯定的,而且仅需要几行代码 $('#btnq').bind('click', function(){ $('#dg').datagrid({ url: '../servlet/Student_search' });//重新指定url$('#dg'

玩转Web之easyui(二)-----easy ui 异步加载生成树节点(Tree),点击树生成tab(选项卡)

关于easy ui 异步加载生成树及点击树生成选项卡,这里直接给出代码,重点部分代码中均有注释 前台: $('#tree').tree({ url: '../servlet/School_Tree?id=-1', //向后台传送id,获取根节点lines:true,onBeforeExpand:function(node,param){ $('#tree').tree('options'

玩转Web之easyui(一)-----easy ui datagird 分页

无意中发现了一个巨牛的人工智能教程,忍不住分享一下给大家。教程不仅是零基础,通俗易懂,而且非常风趣幽默,像看小说一样!觉得太牛了,所以分享给大家。点这里可以跳转到教程。   easy ui 中数据表格的分页其实是很简单的,分页是在数据表格可以正常显示数据的基础上进行的,在这里给出servlet的代码,其中selectAll()方法是从数据库中提取所有数据, 分页的一种思路是:从数据表中取出所

[HDU 5572] An Easy Physics Problem (点在线上判定+对称)

HDU - 5572 给定一个圆和圆外两个点 A和 B 现在有一个质点在 A处,有速度方向 V 其与圆的碰撞是弹性碰撞,问质点是否能经过 B 分情况讨论 如果射线不与圆相交,直接判定点是否在射线上如果射线与圆相交,那么列方程解出与原交点 并得出反弹的法线方程,然后以法线方程作对称 最后判断点是否在一条线段和一条射线上 作对称的话可以将点 A以法线作对称,然后再用撞击点和对

【经典算法】LeetCode 20:有效的括号(Java/C/Python3实现含注释说明,Easy)

作者主页: 🔗进朱者赤的博客 精选专栏:🔗经典算法 作者简介:阿里非典型程序员一枚 ,记录在大厂的打怪升级之路。 一起学习Java、大数据、数据结构算法(公众号同名) ❤️觉得文章还不错的话欢迎大家点赞👍➕收藏⭐️➕评论,💬支持博主,记得点个大大的关注,持续更新🤞 ————————————————- 罗马数字转整数 标签(题目类型):字符串处理、数学计算 题目描述

Sounds Good - Easy Optimized Audio Manager

Sounds Good可以简化您的工作。这个音频管理器将帮助您在游戏中实现声音,音乐,播放列表,动态音乐和多个音频通道。它的设计是用户友好且易于使用的。您可以使用一行代码实现音频,并且可以在编辑器窗口的帮助下轻松直观地管理游戏的音频。 使用Sounds Good有什么好处?⭐ 易用性:它提供了一个用户友好且简单的系统,用于在游戏中实现音频,使所有技能水平的人都可以使用。 性能优化:Sounds

Easy 15 Add Binary(67)

Description Given two binary strings, return their sum (also a binary string). For example, a = “11” b = “1” Return “100”. Solution class Solution {public:string addBinary(string a, string b)

Easy 11 Count and Say(38)

Description The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 1211, 111221, … 1 is read off as “one 1” or 11. 11 is read off as “two 1s” or 21. 21 is read of

Easy 9 Implement strStr()(28)

Description Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. Solution 暴力法。 class Solution {public:int strStr(string haystack, string needl