Blue Bridge Cup 2016 7th C language group B provincial exercise problem solution-exercise F. Fill in the squares**

Daily Questions (Ninety-eight)

Lanqiao Cup 7th C Language Group B Provincial Competition Exercises

Exercise F: Fill in the squares

Insert picture description here
Map 1.jpg
Insert picture description here

Ideas:

First, set the full array template, and then set the condition in the check() function so that consecutive numbers are not adjacent, so we can specify as follows:
Insert picture description here

C++ code:

#include<bits/stdc++.h>
using namespace std;

int a[10] = {
    
    0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
int ans = 0;

bool check()
{
    
    
	if(abs(a[0] - a[1]) == 1 || 
	   abs(a[0] - a[3]) == 1 || 
	   abs(a[0] - a[4]) == 1 || 
	   abs(a[0] - a[5]) == 1 ||
	
	   abs(a[1] - a[2]) == 1 ||
	   abs(a[1] - a[4]) == 1 ||
	   abs(a[1] - a[5]) == 1 ||
	   abs(a[1] - a[6]) == 1 ||
	   
	   abs(a[2] - a[5]) == 1 ||
	   abs(a[2] - a[6]) == 1 ||
	   
	   abs(a[3] - a[4]) == 1 ||
	   abs(a[3] - a[7]) == 1 ||
	   abs(a[3] - a[8]) == 1 ||
	   
	   abs(a[4] - a[5]) == 1 ||
	   abs(a[4] - a[7]) == 1 ||
	   abs(a[4] - a[8]) == 1 ||
	   abs(a[4] - a[9]) == 1 ||
	   
	   abs(a[5] - a[6]) == 1 ||
	   abs(a[5] - a[8]) == 1 ||
	   abs(a[5] - a[9]) == 1 ||
	   
	   abs(a[6] - a[9]) == 1 ||
	   
	   abs(a[7] - a[8]) == 1 ||
	   
	   abs(a[8] - a[9]) == 1)
		return false;
	return true;
}

void f(int k)
{
    
    
	if(k == 10)
	{
    
    
		bool b = check();
		if(b)
			ans++;
		return;
	}
	
	for(int i = k; i < 10; i++)
	{
    
    
		{
    
    
			int t = a[i];
			a[i] = a[k];
			a[k] = t;
		}
		f(k + 1);
		{
    
    
			int t = a[i];
			a[i] = a[k];
			a[k] = t;
		}
	}
}

int main()
{
    
    
	f(0);
	cout << ans << endl;
	return 0;
}

Operation result:
Insert picture description here
So the answer is 1580

Method Two:

The idea is shown in the figure:
Insert picture description here
C++ code:

#include<bits/stdc++.h>
using namespace std;

int a[5][6];
int vis[10];
int ans;

bool check(int i, int j)
{
    
    
	for(int x = i - 1; x <= i + 1; x++)
	{
    
    
		for(int y = j - 1; y <= j + 1; y++)
		{
    
    
			if(abs(a[x][y] - a[i][j]) == 1)
				return false;			
		}
	}
	return true;
}

void f(int x, int y)
{
    
    
	if(x == 3 && y == 4)
	{
    
    
		ans++;
		return;
	}
	for(int i = 0; i < 10; i++)
	{
    
    
		if(vis[i] == 0)
		{
    
    
			a[x][y] = i;
			if(!check(x, y))
			{
    
    
				a[x][y] = -10;
				continue;
			}
			vis[i] = 1;		//标记为已访问 
			if(y == 4)
				f(x + 1, 1);
			else
				f(x, y + 1);
			{
    
    			//回溯 
				vis[i] = 0;
				a[x][y] = -10;
			}
		}
	}
}

void init()
{
    
    
	for(int i = 0; i < 5; i++)
	{
    
    
		for(int j = 0; j < 6; j++)
		{
    
    
			a[i][j] = -10;
		}
	}
}

int main()
{
    
    
	init();
	f(1, 2);
	cout << ans << endl;
	return 0;
}

If you like my article, please remember three consecutive times, like and follow the collection, every like and every one of your attention and every collection will be my infinite motivation on the way forward! ! ! ↖(▔▽▔)↗Thank you for your support, the next issue will be more exciting! ! !

Guess you like

Origin blog.csdn.net/qq_44631615/article/details/104989087