Maze box four color theorem digital grouping problem solution

Posted by ryanhowdy on Fri, 04 Mar 2022 21:02:52 +0100

1. Maze

Title Description

Recently, little Y is playing a maze game. The game is played on an n * m grid. Each grid may be an open space or an obstacle. At the beginning of the game, the character controlled by the player is located in an open space in the figure. During the game, players can use the up, down, left and right keys to control the character to move to the adjacent grid without obstacles (of course, the character cannot move outside the map or diagonally). The goal of the game is to collect the stars on the map (each star can only be collected once). The more you collect, the higher the score. Suppose Y doesn't know how many stars he can collect in a small game.

input

The first row contains two positive integers n and m, indicating that the map of the game contains n rows and m columns.
Next, we give an n × m character matrix, each character may be the following:
● #: indicates that there are obstacles at this position
● . (English full stop): indicates that the location is an open space
● *: indicates that the location is empty and a star is generated
● S: indicates that the position is an open space, and the player is initially located in this position. Ensure that there is and only one S in the figure

output

A total of one line, including an integer, indicating the maximum number of stars that can be collected

sample input

4 8
..#...*.
*.#.S#..
######..
.*..#.*.

sample output

2

Data range limitation

For 50% data, n, m ≤ 40;
For 100% data, 1 ≤ n, m ≤ 200.

#include<cstdio>
#include<iostream>
using namespace std;
int n,m,ans=0,nx,ny;
char a[500][500];
int xx[4]= {-1,0,1,0},yy[4] {0,1,0,-1}; //Up, down, left and right
int dg(int x,int y);//Declarative function
int main() {
	freopen("maze.in","r",stdin);
	freopen("maze.out","w",stdout);
	int i,j,i1,j1;
	scanf("%d%d",&n,&m);
	for(i=1; i<=n; i++) {
		for(j=1; j<=m; j++) {
			cin>>a[i][j];//input
		}
	}
	for(i=1; i<=n; i++) {
		for(j=1; j<=m; j++) {
			if(a[i][j]=='S') { //Judging the point of birth
				dg(i,j);//Start recursion directly
				break;
			}
		}
	}
	printf("%d",ans);//Output final answer
}
int dg(int x,int y) { //Recursive function
	int i;
	for(i=0; i<=3; i++) {
		a[x][y]='#';// You can't go any further, mark
		nx=x+xx[i];//Four directions
		ny=y+yy[i];//Four directions
		if(a[nx][ny]!='#'& & NX > = 1 & & NX < = n & & NY > = 1 & & NY < = m) {/ / judge whether it is out of bounds
			if(a[nx][ny]=='*')//Add the score point
				ans++;
			dg(nx,ny);//Then continue the wide search
		}
	}
}

2. Box

Title Description

Little D is playing the game of stacking boxes. Each box has an intensity, which represents the maximum number of boxes that can be stacked on it. Since the boxes are all the same size, you can't put more than one box side by side on a box. Now small D has n boxes, and the strength of the i-th box is xi. Little D wants to know how many piles he needs to pile up if he wants to pile all these boxes.

input

The first line reads in an integer n, which represents the number of boxes small D has.
The second line reads n integers, and the ith integer xi represents the strength of the ith box.

output

There is one row in total, and an integer indicates at least how many heaps small D needs to stack.

sample input

5
0 2 1 1 2

sample output

2

Data range limitation

For 20% data, n ≤ 10;
For 50% data, n ≤ 1000;
For 100% data, n ≤ 500000, xi ≤ 1000000000.

#include<iostream>
#include<iomanip>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdio>
using namespace std;
const int maxn=10001;
int n,i,ans,t;
struct cd { //Define queue
	int qd;
	bool zt;
};
cd a[500010];
bool cmp(cd a1,cd b1) {
	return a1.qd<b1.qd;//Here is the ratio size
}
void input() { //Input and output
	cin>>n;
	for(i=1; i<=n; i++) {
		cin>>a[i].qd;
		a[i].zt=1;
	}
}
bool comp(cd c[],int p) { //Judgment function
	int ok1=0;
	int o;
	for(o=1; o<=p; o++)
		if(c[o].zt!=0) { //Judge whether it has been used
			ok1=1;//Mark when there is
			break;
		}
	if(ok1==0) return 1;//The marker works
	else return 0;
}
void work() {
	sort(a+1,a+n+1,cmp);
	while(comp(a,n)==0) { //When not used
		ans=0;
		for(int i=1; i<=n; i++) {
			if(a[i].zt==0) continue;//Judge whether the current book is available
			if(a[i].qd>=ans) { //Is it full
				ans++;//It's almost full
				a[i].zt=0;
			}
		}
		if(ans!=0) t++;//Record the number of schemes without marking
	}
}
void output() {
	cout<<t;//output
}
int main() {
	freopen("box.in","r",stdin);
	freopen("box.out","w",stdout);
	input();
	work();
	output(); //What does function mean? Needless to say, the first input, the second work and the third output
}

3. Four color theorem

Title Description

You must have heard of the famous four-color theorem? This is one of the three major mathematical problems in the modern world (by the way, the other two are Fermat theorem and goldmach conjecture).
The four-color theorem came from England. In 1852, Francis Guthrie, who graduated from the University of London, found an interesting phenomenon when he was engaged in map coloring in a scientific research unit: "it seems that each map can be colored with four colors, so that countries with common borders have different colors." (Note: only the areas with common edges are required to have different colors, and it doesn't matter if only the common vertices have the same color)
Can this conclusion be strictly proved mathematically? He and his brother Grice, who was studying in college, decided to have a try. The two brothers have piled up a large pile of manuscript paper to prove this problem, but the research work has not made progress.
On October 23, 1852, his brother asked his teacher and famous mathematician De Morgan about the proof of this problem. Morgan couldn't find a way to solve this problem, so he wrote to his friend and famous mathematician Sir Hamilton for advice. After receiving Morgan's letter, Hamilton demonstrated the four-color problem. But until Hamilton died in 1865, the problem could not be solved.
In 1976, with the support of J. Koch's algorithm, American mathematicians Kenneth Appel and Wolfgang Haken made 10 billion judgments in 1200 hours on two different electronic computers at the University of Illinois, and finally completed the proof of the four-color theorem.
Your task is certainly much easier than those mathematicians: you just need to write a program to calculate the number of schemes to fill different areas with four colors on a given map with five areas, and ensure that the areas with common edges have different colors.

input

The first line of the file is an integer n (0 < = n < = 10), which respectively represents the amount of information of areas with common edges in the map.
The following n lines, each with a pair of integers, indicate that after numbering all areas, the two numbered areas have common edges.

output

There is only one integer in the file, which represents the total number of schemes to fill the map with four colors. Note that in some scenarios, all four colors do not have to be used.

sample input

 4
1 2
1 3
1 4
1 5

sample output

324

Data range limitation

Tips

#include<cstdio>
int s,n,x,y,a[10][10],t[10],p;
void bfs(int x) {
	if(x==6) { //Determine whether the list of possible
		s++;//Yes, that's the answer++
		return;
	}
	for(int i=1; i<=4; ++i) { //Enumerate all possible
		for(int j=1; j<=x-1; ++j) { //Enumerate all possible
			if(a[x][j]==1&&t[j]==i) { //Judge whether it is connected and the same color
				p=1;//Just mark it
				break;//immediate withdrawal
			}
		}
		if(p==1) { //Here is the function of clearing
			p=0;//Here is the function of clearing
			continue;//Here is the function of clearing
		}
		t[x]=i;//Here is the function of memory array
		bfs(x+1);//search
	}
}
int main() {
	freopen("c.in","r",stdin);
	freopen("c.out","w",stdout);
	scanf("%d",&n);//Enter n
	for(int i=1; i<=n; ++i) {
		scanf("%d%d",&x,&y);//Enter each number
		a[x][y]=1;//Mark relation
		a[y][x]=1;//Mark relation
	}
	bfs(1);//Start search
	printf("%d",s);//Output after searching
}

4. Digital grouping

Title Description

Xiao Ming's mathematical calculation ability is very strong, and he often appears very proud in front of his classmates. The representative of mathematics department really couldn't see it anymore. He decided to solve a very troublesome problem and "torture" him.
The math representative decided to give him some numbers and ask him to group. Start grouping from the first number, and each group must be a continuous number. The sum of each group is required to be equal. Ask what the minimum sum of each group can be. (of course, these numbers can be grouped, but they can be directly divided into one group.)

input

The first line is a number N
The second line is N integers (each number is less than or equal to 1000), and the two numbers are separated by spaces.

output

One line, minimum sum

sample input

6
2 5 1 3 3 7 

sample output

7

Data range limitation

1 n = 10
2 n = 100
3 n = 1000
4 n = 200000
5 n = 200000
6 n = 1000000
7 n = 1000000
8 n = 1000000
9 n = 1000000
10 n = 1000000

Tips

Divided into three groups (2, 5) (1, 3, 3) (7) and 7, there is no smaller sum than 7.

#include<bits/stdc++.h>
using namespace std;
int a[1000005],b[1000005],n;
long long int k=0;
bool rechecking/*Double check function*/(int da) {
	int sum=0;
	for(int j=1; j<=n; j++) {
		sum+=a[j];//Current number accumulation
		if(sum>da) { //It must not be greater than
			return false;//No direct return
		}
		if(sum==da) { //Equal to let him continue to accumulate until n
			sum=0;//Clear
		}
	}
	return true;//It must be feasible in the end
}
int uu=0;
int main() {
	freopen("d.in","r",stdin);
	freopen("d.out","w",stdout);
	cin>>n;//Enter n
	for(register int i=1; i<=n; i++) {
		scanf("%d",&a[i]);//Enter each number
		k+=a[i];//Sum
		if(uu<a[i])uu=a[i];//Find the maximum value
	}
	for(long long int i=uu; i<=k/2/*The approximate number of times is K divided by two*/; i++) { //Because the number of minimum energy composition sum is the maximum
//For example, 2 5 6 1 2 5, the minimum is 7, 7 > 6
//Or 2 3 5 1 4, minimum 5,5 = 5
//Therefore, the minimum value > = maximum value is obtained
		if(k%i==0) { //Judge whether it is a divisor
			if(rechecking(i)==true) { //Judge whether it can be constituted
				cout<<i;//Direct output
				return 0;//Save time complexity:)
			}
		}
	}
	cout<<k;//If it can't be formed, there is only K
}

be without

Topics: C++ Algorithm Dynamic Programming Graph Theory