• 放棋子蓝桥杯


    欢迎访问我的新博客:http://www.milkcu.com/blog/

    原文地址:http://www.milkcu.com/blog/archives/1366864680.html

    简述

    这是2012年第三届蓝桥杯全国软件大赛预赛(C++本科组)第7题,要求将代码补全,实现“每行每列都正好有3颗棋子”的功能。

    推荐链接:《2012蓝桥杯软件大赛预赛题目汇总》

    题目描述

        今有 6 x 6 的棋盘格。其中某些格子已经预先放好了棋子。现在要再放上去一些,使得:每行每列都正好有3颗棋子。我们希望推算出所有可能的放法。下面的代码就实现了这个功能。
    棋盘放棋子
        初始数组中,“1”表示放有棋子,“0”表示空白。

    int N = 0;
    
    bool CheckStoneNum(int x[][6])
    {
    	for(int k=0; k<6; k++)
    	{
    		int NumRow = 0;
    		int NumCol = 0;
    		for(int i=0; i<6; i++)
    		{
    			if(x[k][i]) NumRow++;
    			if(x[i][k]) NumCol++;
    		}
    		if(_____________________) return false;  // 填空
    	}
    	return true;
    }
    
    int GetRowStoneNum(int x[][6], int r)
    {
    	int sum = 0;
    	for(int i=0; i<6; i++) 	if(x[r][i]) sum++;
    	return sum;
    }
    
    int GetColStoneNum(int x[][6], int c)
    {
    	int sum = 0;
    	for(int i=0; i<6; i++) 	if(x[i][c]) sum++;
    	return sum;
    }
    
    void show(int x[][6])
    {
    	for(int i=0; i<6; i++)
    	{
    		for(int j=0; j<6; j++) printf("%2d", x[i][j]);
    		printf("\n");
    	}
    	printf("\n");
    }
    
    void f(int x[][6], int r, int c);
    
    void GoNext(int x[][6],  int r,  int c)
    {
    	if(c<6)
    		_______________________;   // 填空
    	else
    		f(x, r+1, 0);
    }
    
    void f(int x[][6], int r, int c)
    {
    	if(r==6)
    	{
    		if(CheckStoneNum(x))
    		{
    			N++;
    			show(x);
    		}
    		return;
    	}
    
    	if(______________)  // 已经放有了棋子
    	{
    		GoNext(x,r,c);
    		return;
    	}
    	
    	int rr = GetRowStoneNum(x,r);
    	int cc = GetColStoneNum(x,c);
    
    	if(cc>=3)  // 本列已满
    		GoNext(x,r,c);  
    	else if(rr>=3)  // 本行已满
    		f(x, r+1, 0);   
    	else
    	{
    		x[r][c] = 1;
    		GoNex。t(x,r,c);
    		x[r][c] = 0;
    		
    		if(!(3-rr >= 6-c || 3-cc >= 6-r))  // 本行或本列严重缺子,则本格不能空着!
    			GoNext(x,r,c);  
    	}
    }
    
    int main(int argc, char* argv[])
    {
    	int x[6][6] = {
    		{1,0,0,0,0,0},
    		{0,0,1,0,1,0},
    		{0,0,1,1,0,1},
    		{0,1,0,0,1,0},
    		{0,0,0,1,0,0},
    		{1,0,1,0,0,1}
    	};
    
    	f(x, 0, 0);
    	
    	printf("%d\n", N);
    
    	return 0;
    }
    

    请分析代码逻辑,并推测划线处的代码。
    答案写在 “解答.txt” 文件中
    注意:只写划线处应该填的内容,划线前后的内容不要抄写。

    分析

    该题的难点在读懂每个函数的作用:

    • bool CheckStoneNum(int x[][6]);                  判断每行每列是否都正好有3颗棋子;
    • int GetRowStoneNum(int x[][6], int r);          返回r行存在的棋子数;
    • int GetColStoneNum(int x[][6], int c);           返回c列的棋子数;
    • void show(int x[][6]);                                        将符合要求的方案打印出来;
    • void GoNext(int x[][6],  int r,  int c);                 从(r, c)开始往后遍历二维数组;

    源代码

    # include <stdio.h>
    
    int N = 0;
    
    bool CheckStoneNum(int x[][6])
    {
    	for(int k=0; k<6; k++)
    	{
    		int NumRow = 0;
    		int NumCol = 0;
    		for(int i=0; i<6; i++)
    		{
    			if(x[k][i]) NumRow++;
    			if(x[i][k]) NumCol++;
    		}
    		if(NumRow != 3 || NumCol != 3) return false;  // 填空
    	}
    	return true;
    }
    
    int GetRowStoneNum(int x[][6], int r)
    {
    	int sum = 0;
    	for(int i=0; i<6; i++) 	if(x[r][i]) sum++;
    	return sum;
    }
    
    int GetColStoneNum(int x[][6], int c)
    {
    	int sum = 0;
    	for(int i=0; i<6; i++) 	if(x[i][c]) sum++;
    	return sum;
    }
    
    void show(int x[][6])
    {
    	for(int i=0; i<6; i++)
    	{
    		for(int j=0; j<6; j++) printf("%2d", x[i][j]);
    		printf("\n");
    	}
    	printf("\n");
    }
    
    void f(int x[][6], int r, int c);
    
    void GoNext(int x[][6],  int r,  int c)
    {
    	if(c<6)
    		f(x, r, c + 1);   // 填空
    	else
    		f(x, r+1, 0);
    }
    
    void f(int x[][6], int r, int c)
    {
    	if(r==6)
    	{
    		if(CheckStoneNum(x))
    		{
    			N++;
    			show(x);
    		}
    		return;
    	}
    
    	if(x[r][c] == 1)  // 已经放有了棋子
    	{
    		GoNext(x,r,c);
    		return;
    	}
    	
    	int rr = GetRowStoneNum(x,r);
    	int cc = GetColStoneNum(x,c);
    
    	if(cc>=3)  // 本列已满
    		GoNext(x,r,c);  
    	else if(rr>=3)  // 本行已满
    		f(x, r+1, 0);   
    	else
    	{
    		x[r][c] = 1;
    		GoNext(x,r,c);
    		x[r][c] = 0;
    		
    		if(!(3-rr >= 6-c || 3-cc >= 6-r))  // 本行或本列严重缺子,则本格不能空着!
    			GoNext(x,r,c);  
    	}
    }
    
    int main(int argc, char* argv[])
    {
    	int x[6][6] = {
    		{1,0,0,0,0,0},
    		{0,0,1,0,1,0},
    		{0,0,1,1,0,1},
    		{0,1,0,0,1,0},
    		{0,0,0,1,0,0},
    		{1,0,1,0,0,1}
    	};
    
    	f(x, 0, 0);
    	
    	printf("%d\n", N);
    
    	return 0;
    }

    最后答案

    NumRow != 3 || NumCol != 3
    f(x, r, c + 1)
    x[r][c] == 1
  • 相关阅读:
    【小白建站笔记】从购买域名及服务器到备案成功:以阿里云为例
    jarvisoj-Evil Exe
    jarvisoj-软件密码破解-2(CFF_100_1)
    Jarvis OJ-superexpress
    JarvisOJ-[xman2019]xfz
    csictf-Esrever
    分解质因数
    矩阵乘法
    python获取一组数个位数之和
    python计算字母出现次数
  • 原文地址:https://www.cnblogs.com/milkcu/p/3808910.html
Copyright © 2020-2023  润新知