棋盘覆盖问题 |
Time Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:32768KB |
Total submit users: 65, Accepted users: 26 |
Problem 10432 : No special judgement |
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 |
要求遍历顺序按从左到右,从上到下。 package acm.icpc; import java.util.Scanner; /** * @功能Function Description: 棋盘覆盖 * @开发环境Environment: eclipse * @技术特点Technique: * @算法 * @版本Version: Scanner * @作者Author: follow your dreams * @日期Date: 20120809 * @备注Notes: 测试数据都对了,可是提交一直wrong error * @链接: http://acm.hunnu.edu.cn/online/?action=problem&type=show */ public class Icpc10432_1_20120820 { public static int[][] arr; private static int num = 1;//静态变量 public static void main(String[] args) { Scanner sc = new Scanner(System.in); int count = sc.nextInt(); int countCase = 0; while(count-- != 0) { countCase ++; int n = sc.nextInt(); int x = sc.nextInt(); int y = sc.nextInt(); arr = new int[n+1][n+1]; num = 1; if(n==1) { System.out.println("0"); } else { solve(1, 1, x+1, y+1, n); show(n, countCase); } } } /** * 将棋盘分割成4个部分 * @param i 棋盘的左上角的坐标(i,j) * @param j * @param x 特殊的那个位置 * @param y * @param n 棋盘的宽度(由于棋盘是正方形的,有宽度和左上角的坐标就可以确定棋盘了) */ public static void solve(int i, int j, int x, int y, int n) { int temp = n/2; int flag = 0; int xtemp1 = i+temp-1; int ytemp1 = j+temp-1; if(!(x<=xtemp1 && y<=ytemp1)) { arr[i+temp-1][j+temp-1] = num; } else { flag = 1; } int xtemp2 = i+temp-1; int ytemp2 = j+temp; if(!(x<=xtemp2 && y>=ytemp2)) { arr[xtemp2][ytemp2] = num; } else { flag = 2; } int xtemp3 = i+temp; int ytemp3 = j+temp-1; if(!(x>=xtemp3 && y<=ytemp3)) { arr[xtemp3][ytemp3] = num; } else { flag = 3; } int xtemp4 = i+temp; int ytemp4 = j+temp; if(!(x>=xtemp4 && y>=ytemp4)) { arr[xtemp4][ytemp4] = num; } else { flag = 4; } num ++; if(n/2 != 1) { if(flag != 1) { solve(i, j, xtemp1, ytemp1, temp); } else { solve(i, j, x, y, temp); } if(flag != 2) { solve(i, temp+j, xtemp2, ytemp2, temp); } else { solve(i, temp+j, x, y, temp); } if(flag != 3) { solve(temp+i, j, xtemp3, ytemp3, temp); } else { solve(temp+i, j, x, y, temp); } if(flag != 4) { solve(temp+i, temp+j, xtemp4, ytemp4, temp); } else { solve(temp+i, temp+j, x, y, temp); } } } public static void show(int n, int countCase) {//输出 int i, j; System.out.println("CASE:" + countCase); for(i=1; i<=n; i++) { for(j=1; j<n; j++) { System.out.printf("%d\t", arr[i][j]); } System.out.println(arr[i][j]); } } } |
转载于:https://www.cnblogs.com/followYourDreams/archive/2012/08/21/2648569.html