1 solutions

  • 0
    @ 2024-12-11 0:49:57

    C :

    #include<stdio.h>
    int k,x,y,t=1;
    short int p[11]={1,2,4,8,16,31,64,128,256,512,1024};
    short int map[1025][1025]={0};
    void print()
    {
    	for(int i=1;i<=p[k];i++)
    	{
    		for(int j=1;j<=p[k];j++) printf("%d\t",map[i][j]);
    		printf("\n");
    	}
    }
    void work(int a1,int b1,int a2,int b2,int x0,int y0)
    {
    	if(a1+1==a2) 
    	{
    		if(!map[a1][b1]) map[a1][b1]=t;
    		if(!map[a1][b2]) map[a1][b2]=t;
    		if(!map[a2][b1]) map[a2][b1]=t;
    		if(!map[a2][b2]) map[a2][b2]=t;
    		t++;
    		return;
    	}
    	int now1=(a1+a2)>>1;
    	int now2=(b1+b2)>>1;
    	if(x0<=now1)
    	{
    		if(y0<=now2) 
    		{
    			map[now1][now2+1]=t;
    			map[now1+1][now2]=t;
    			map[now1+1][now2+1]=t;
    			t++;
    			work(a1,b1,now1,now2,x0,y0);
    	        work(a1,now2+1,now1,b2,now1,now2+1);
    	        work(now1+1,b1,a2,now2,now1+1,now2);
    	        work(now1+1,now2+1,a2,b2,now1+1,now2+1);
    		}
    		else
    		{
    			map[now1][now2]=t;
    			map[now1+1][now2]=t;
    			map[now1+1][now2+1]=t;
    			t++;	
    			work(a1,b1,now1,now2,now1,now2);
    	        work(a1,now2+1,now1,b2,x0,y0);
    	        work(now1+1,b1,a2,now2,now1+1,now2);
    	        work(now1+1,now2+1,a2,b2,now1+1,now2+1);	
    		}
    	}
    	else
    	{
    		if(y0<=now2) 
    		{
    			map[now1][now2]=t;
    			map[now1][now2+1]=t;
    			map[now1+1][now2+1]=t;
    			t++;
    			work(a1,b1,now1,now2,now1,now2);
    	        work(a1,now2+1,now1,b2,now1,now2+1);
    	        work(now1+1,b1,a2,now2,x0,y0);
    	        work(now1+1,now2+1,a2,b2,now1+1,now2+1);
    		}
    		else
    		{		
    			map[now1][now2]=t;
    			map[now1][now2+1]=t;
    			map[now1+1][now2]=t;
    			t++;
    			work(a1,b1,now1,now2,now1,now2);
    	        work(a1,now2+1,now1,b2,now1,now2+1);
    	        work(now1+1,b1,a2,now2,now1+1,now2);
    	        work(now1+1,now2+1,a2,b2,x0,y0);
    		}
    	}
    }
    
    main()
    {
    	scanf("%d%d%d",&k,&x,&y);
    	work(1,1,p[k],p[k],x,y);
    	map[x][y]=0;
    	print();
    }
    

    C++ :

    #include<iostream>
    #include<cstring>
    #include<cstdio>
    using namespace std;
    int x,y,k,num,a[1030][1030]={0};
    void chess(int,int,int,int,int);
    int main()
    {
    	//freopen("qp5.in","r",stdin);
    	//freopen("qp5.out","w",stdout);
    	num=1;
    	cin>>k;
    	k=2<<k-1;	
    	cin>>x>>y;
    	chess(1,1,x,y,k);
    	//cout<<"CASE:"<<q<<endl;
    	for(int i=1;i<=k;i++)
    	{
    		for(int j=1;j<=k;j++) cout<<a[i][j]<<'\t';
    		cout<<endl;
    	}		
    	return 0;
    }
    void chess(int zsx,int zsy,int cqx,int cqy,int size)
    {
    	if(size==1) return;
    	int s=size/2,t=num++;
    	if((cqx<zsx+s) && (cqy<zsy+s))//表明残缺在左上部
    		chess(zsx,zsy,cqx,cqy,s);
    	else
    	{
    		a[zsx+s-1][zsy+s-1]=t;//用t覆盖右下角
    		chess(zsx,zsy,zsx+s-1,zsy+s-1,s); 
    	} 
    	//*****************
    	if(cqx<zsx+s && cqy>=zsy+s)//表明残缺在左下角
    		chess(zsx,zsy+s,cqx,cqy,s);
    	else
    	{
    		a[zsx+s-1][zsy+s]=t;////用t覆盖右上角
    		chess(zsx,zsy+s,zsx+s-1,zsy+s,s);
    	}
    	//******************
    	if(cqx>=zsx+s && cqy<zsy+s)//说明残缺在右上区
    		chess(zsx+s,zsy,cqx,cqy,s);
    	else
    	{
    		a[zsx+s][zsy+s-1]=t;
    		chess(zsx+s,zsy,zsx+s,zsy+s-1,s);
    	}
    	//***************** 
    	if(cqx>=zsx+s && cqy>=zsy+s)//说明残缺在右下区域
    		chess(zsx+s,zsy+s,cqx,cqy,s);
    	else
    	{
    		a[zsx+s][zsy+s]=t;
    		chess(zsx+s,zsy+s,zsx+s,zsy+s,s);
    	} 	 
    }
    

    Java :

    import java.util.Scanner;
    
    public class Main {
    	static int tile = 1;
    	static int[][] board;
    
    	public static void main(String[] args) {
    		Scanner in = new Scanner(System.in);
    		while (in.hasNextInt()) {
    			int a = in.nextInt();
    			int size = (int) Math.pow(2, a);
    			int dr = in.nextInt();
    			int dc = in.nextInt();
    			board = new int[size+1][size+1];
    			chessBoard(1, 1, dr, dc, size);
    			for(int i=1;i<=size;i++){
    				for(int j=1;j<=size;j++){
    					System.out.print(board[i][j]+"\t");
    				}
    				System.out.println();
    			}
    		}
    	}
    
    	public static void chessBoard(int tr, int tc, int dr, int dc, int size) {
    		// tr:左上角行号,tc:左上角列号,dr:黑点行号,dc:黑点列号
    		if (size == 1)
    			return;
    		int t = tile++;// 骨牌号
    		int s = size / 2;// 分割棋盘
    		// 覆盖左上角子棋盘
    		if (dr < tr + s && dc < tc + s) {
    			// 黑点在此棋盘中
    			chessBoard(tr, tc, dr, dc, s);
    		} else {// 黑点不在此棋盘中
    				// 用t号骨牌覆盖右下角
    			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 {// 黑点不在此棋盘中
    				// 用t号骨牌覆盖左下角
    			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 {// 黑点不在此棋盘中
    				// 用t号骨牌覆盖右上角
    			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 {// 黑点在此棋盘中
    				// 用t号骨牌覆盖左上角
    			board[tr + s][tc + s] = t;
    			// 覆盖其余方格
    			chessBoard(tr + s, tc + s, tr + s, tc + s, s);
    		}
    	}
    }
    
    
    • 1

    Information

    ID
    781
    Time
    1000ms
    Memory
    128MiB
    Difficulty
    (None)
    Tags
    # Submissions
    0
    Accepted
    0
    Uploaded By