棋盘覆盖问题
来源:互联网 发布:vb语言基本代码 编辑:程序博客网 时间:2024/06/14 08:26
Problem description
在一个2k x 2k ( 即:2^k x 2^k )个方格组成的棋盘中,恰有一个方格与其他方格不同,称该方格为一特殊方格,且称该棋盘为一特殊棋盘。在棋盘覆盖问题中,要用图示的4种不同形态的L型骨牌覆盖给定的特殊棋盘上除特殊方格以外的所有方格,且任何2个L型骨牌不得重叠覆盖。
Input
输入文件第一行是一个整数T,表示有多少组测试数据,接下来是T组测试数据,共2T行,每组第一行为整数n,是2的n次幂(1<=n<=64),表示棋盘的大小为n*n,第二行是两个整数,代表特殊方格所在行号和列号。
Output
先输出“CASE:i,然后按样例输出。数据间用制表符隔开(‘t’),每行最后一个数据后无制表符。
Sample Input
2
2
0 0
8
2 2
Sample Output
CASE:1
0 1
1 1
CASE:2
3 3 4 4 8 8 9 9
3 2 2 4 8 7 7 9
5 2 0 6 10 10 7 11
5 5 6 6 1 10 11 11
13 13 14 1 1 18 19 19
13 12 14 14 18 18 17 19
15 12 12 16 20 17 17 21
15 15 16 16 20 20 21 21
Judge Tips
要求遍历顺序按从左到右,从上到下。
思路:
虽然这个问题已经在网上被讨论遍了,但是最近从新拾起算法,感觉有必要夯实一下基础。
棋盘覆盖问题:
首先大致描述一下题目:
在一个2^k×2^k个方格组成的棋盘中,若有一个方格与其他方格不同,则称该方格为一特殊方格,且称该棋盘为一个特殊棋盘.显然特殊方格在棋盘上出现的位置有4^k种情形.因而对任何
k≥0,有4^k种不同的特殊棋盘.
下图–图(1)中的特殊棋盘是当k=2时16个特殊棋盘中的一个:
图(1)
题目要求在棋盘覆盖问题中,要用下图—图(2)所示的4种不同形态的L型骨牌覆盖一个给定的特殊棋盘上除特殊方格以外的所有方格,且任何2个L型骨牌不得重叠覆盖.
图(2)
思路分析:
当k>0时,将2^k×2^k棋盘分割为4个2^k-1×2^k-1子棋盘,如下图–图(3)所示:
图(3)
特殊方格必位于4个较小子棋盘之一中,其余3个子棋盘中无特殊方格.为了将这3个无特殊方格的子棋盘转化为特殊棋盘,可以用一个L型骨牌覆盖这3个较小棋盘的会合处。
如下图–图(4)所示,这3个子棋盘上被L型骨牌覆盖的方格就成为该棋盘上的特殊方格,从而原问题转化为4个较小规模的棋盘覆盖问题.递归地使用这种分割,直至棋盘简化为1×1棋盘。
#include<iostream>using namespace std;int tile=1; //L型骨牌的编号(递增)int board[100][100]; //棋盘/****************************************************** 递归方式实现棋盘覆盖算法* 输入参数:* tr--当前棋盘左上角的行号* tc--当前棋盘左上角的列号* dr--当前特殊方格所在的行号* dc--当前特殊方格所在的列号* size:当前棋盘的:2^k*****************************************************/void chessBoard ( int tr, int tc, int dr, int dc, int size ){ if ( size==1 ) //棋盘方格大小为1,说明递归到最里层 return; int t=tile++; //每次递增1 int s=size/2; //棋盘中间的行、列号(相等的) //检查特殊方块是否在左上角子棋盘中 if ( dr<tr+s && dc<tc+s ) //在 chessBoard ( tr, tc, dr, dc, s ); else //不在,将该子棋盘右下角的方块视为特殊方块 { board[tr+s-1][tc+s-1]=t; chessBoard ( tr, tc, tr+s-1, tc+s-1, s ); } //检查特殊方块是否在右上角子棋盘中 if ( dr<tr+s && dc>=tc+s ) //在 chessBoard ( tr, tc+s, dr, dc, s ); else //不在,将该子棋盘左下角的方块视为特殊方块 { board[tr+s-1][tc+s]=t; chessBoard ( tr, tc+s, tr+s-1, tc+s, s ); } //检查特殊方块是否在左下角子棋盘中 if ( dr>=tr+s && dc<tc+s ) //在 chessBoard ( tr+s, tc, dr, dc, s ); else //不在,将该子棋盘右上角的方块视为特殊方块 { board[tr+s][tc+s-1]=t; chessBoard ( tr+s, tc, tr+s, tc+s-1, s ); } //检查特殊方块是否在右下角子棋盘中 if ( dr>=tr+s && dc>=tc+s ) //在 chessBoard ( tr+s, tc+s, dr, dc, s ); else //不在,将该子棋盘左上角的方块视为特殊方块 { board[tr+s][tc+s]=t; chessBoard ( tr+s, tc+s, tr+s, tc+s, s ); }}void main(){ int size; cout<<"输入棋盘的size(大小必须是2的n次幂): "; cin>>size; int index_x,index_y; cout<<"输入特殊方格位置的坐标: "; cin>>index_x>>index_y; chessBoard ( 0,0,index_x,index_y,size ); for ( int i=0; i<size; i++ ) { for ( int j=0; j<size; j++ ) cout<<board[i][j]<<"/t"; cout<<endl; }}
Java版
import java.util.Scanner;public class ChessBoard { static int x,y;//分别表示特殊点的层数和列数 static int title=1; static int chess[][]; public static void main(String[] args) { Scanner scanner=new Scanner(System.in); int size=scanner.nextInt();//size行size列 x=scanner.nextInt(); y=scanner.nextInt(); chess=new int[size][size]; chessBoard(0,0,x,y,size); for(int i=0;i<size;i++) { for(int j=0;j<size;j++) { System.out.print(chess[i][j]+" "); } System.out.println(); } } /***************************************************** * 递归方式实现棋盘覆盖算法 * 输入参数: * tr--当前棋盘左上角的行号 * tc--当前棋盘左上角的列号 * dr--当前特殊方格所在的行号 * dc--当前特殊方格所在的列号 *****************************************************/ public static void chessBoard ( int tr, int tc, int dr, int dc,int size){ if(size==1) return ; int t=title++;//骨牌数 int s=size/2;//中间位置 if(dr<tr+s && dc<tc+s)//特殊点在左上角 { chessBoard(tr, tc, dr, dc, s); }else { chess[tr+s-1][tc+s-1]=t; chessBoard(tr, tc, tr+s-1, tc+s-1, s); } if(dr<tr+s && dc>=tc+s)//特殊点在右上角 { chessBoard(tr, tc+s, dr, dc, size); } else { chess[tr+s-1][tc+s]=t; chessBoard(tr, tc+s, tr+s-1, tc+s, s); } if(dr>=tr+s && dc<tc+s)//左下角 { chessBoard(tr+s, tc, dr, dc, s); }else{ chess[tr+s][tc+s-1]=t; chessBoard(tr+s, tc, tr+s, tc+s-1, s); } if( dr>=tr+s && dc>=tc+s )//右下角 { chessBoard(tr+s, tc+s, dr, dc, s); } else{ chess[tr+s][tc+s]=t; chessBoard(tr+s, tc+s, tr+s, tc+s, s); } }}
输出:
8
0 0
0 3 4 4 9 9 10 10
3 3 2 4 9 8 8 10
5 2 2 7 11 11 8 12
5 5 7 7 1 11 12 12
14 14 16 1 1 26 27 27
14 13 16 16 26 26 25 27
22 13 13 24 28 25 25 30
22 22 24 24 28 28 30 30
- 棋盘覆盖问题
- 棋盘覆盖问题
- 棋盘覆盖问题
- 棋盘覆盖问题
- 棋盘覆盖问题
- 棋盘覆盖问题
- 棋盘覆盖问题
- 棋盘覆盖问题
- 棋盘覆盖问题
- ChessBoard棋盘覆盖问题
- 棋盘覆盖问题
- 棋盘覆盖问题
- 棋盘覆盖问题
- 棋盘覆盖问题
- 《ChessboardCoverage_棋盘覆盖问题》
- 棋盘覆盖问题
- 棋盘覆盖问题
- 棋盘覆盖问题
- Extjs3.2第一篇:formPanel的getForm().getValues()提交与处理
- 用java做导出excel的万能方法
- spark常用的transformation
- Java安全之消息摘要
- 学习OpenCV——KeyPoint Matching 优化方式
- 棋盘覆盖问题
- Servlet API中包装类装饰模式的应用
- rediskey的辅助类备忘
- LeetCode 257. Binary Tree Paths
- solvepnp详解
- Python实现删除当前目录下除当前脚本以外的文件和文件夹实例
- java中“或”(||)条件的执行次序
- com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
- spring boot 单元测试