First week search

Posted by phpnewby1918 on Wed, 26 Jan 2022 10:42:51 +0100

B-Knight Moves

POJ 1915

General idea of the topic

Given the starting point and ending point of a knight (horse) on the chessboard, output the minimum number of steps to move. If the start and end points are the same, 0 is output.

analysis

BFS, search for 8 kinds of walking positions. Just find it.

code
#include<stdio.h>
#include<queue>
using namespace std;
/*
The first two times wa, the queue was not emptied 
*/
struct point
{
	int x;
	int y;
	int step;
};
queue<point> r;
int cb[500][500],v[500][500];//chessboard and Tags 
int dx[10]={-2,-1,1,2,2,1,-1,-2};
int dy[10]={1,2,2,1,-1,-2,-2,-1};
int main(){
	int n1,n,l;
	int x1,y1,x2,y2;
	scanf("%d",&n);
	for(n1=0;n1<n;n1++){
		scanf("%d",&l);
		scanf("%d %d",&x1,&y1);
		scanf("%d %d",&x2,&y2);	
		for(int i=0;i<l;i++)
			for(int j=0;j<l;j++){
				v[i][j] = 0;	
			}
		point start;
		start.x = x1;
		start.y = y1;
		start.step = 0;
		r.push(start);
		v[start.x][start.y] = 1;
		
		//bfs
		while(!r.empty()){
			int x = r.front().x,y = r.front().y;
			if(x==x2&&y==y2)
			{
				printf("%d\n",r.front().step);
				break;
			}
			for(int k=0;k<8;k++){
				int nx,ny;
				nx = x + dx[k]; ny = y +dy[k];
				if(nx>=0&&nx<l&&ny>=0&&ny<l&&v[nx][ny]==0){
					point temp;
					temp.x = nx;
					temp.y = ny;
					temp.step = r.front().step +1;
					r.push(temp);
					v[nx][ny] = 1;	
				}
			} 
			r.pop();
		}
		while(!r.empty()) r.pop(); //Empty queue
	}
	return 0;
}

C - Computer Game

General idea of the topic

A grid with two rows and N columns, each of which is either 0 or 1 (0 means no obstacle and 1 means no obstacle). Characters can be moved from (\ (x_1,y_1 \)) to \ ((x_2,y_2) \), where \ (|x_1-x_2| < = 1, |y_1-y_2| < = 1 \). Judge whether it can be moved from \ ((1,1) \) to \ ((2,n) \).

analysis

If a column is full of 1, the character cannot break through the array of 1 and output NO. Otherwise, YES is output.

code
#include<stdio.h>
// Write a character array into a number array 
int main(){
	int i,n;
	int flag;
	scanf("%d",&n);
	for(i=0;i<n;i++){
		int j,m;
		char a[200],b[200];
		scanf("%d",&m);
		flag =0;
		getchar(); 
		for(j=0;j<m;j++)
			scanf("%c",&a[j]);
		getchar();
		for(j=0;j<m;j++)
			scanf("%c",&b[j]);
		for(j=0;j<m;j++){
			if(a[j]=='1'&&b[j]=='1'){
				flag = 1;
				printf("NO\n");
				break;
			}
		}
		if(flag == 0)
			printf("YES\n");
	} 
	return 0;
} 

D - drive a go kart

Xiaoq is designing a 2D go kart game. Your task is to help xiaoq realize some of its functions.

In this game, the game map is a grid with n rows and m columns, numbered from top to bottom as row 1 to row n, and from left to right as column 1 to column M. the coordinates of the grid in row i and column j are (i,j). Each grid is either a passable flat ground or an impassable obstacle.

There is a go kart controlled by the player in a flat grid on the map. The traveling speed of the go kart is vv, and there are 8 possible orientations:

  • "Up": move from (x,y) to (x − 1,y) when you go one step forward.
  • "Next": move from (x,y) to (x+1,y) when moving forward.
  • "Left": when moving forward one step, it will move from (x,y) to (x,y − 1).
  • "Right": move from (x,y) to (x,y+1) one step forward.
  • "Top left": when moving forward one step, it will move from (x,y) to (x − 1,y − 1).
  • "Top right": move from (x,y) to (x − 1,y+1) one step forward.
  • "Bottom left": move from (x,y) to (x+1,y − 1) when moving forward one step.
  • "Bottom right": move from (x,y) to (x+1,y+1) when moving forward one step.

At the beginning, the go kart is upward at a flat grid, and its initial movement rate is v=0. Next, the player will input q operation instructions successively, and each operation instruction is one of the following:

  • "L": the go kart turns 45 degrees to the left.
  • "R": the go kart turns 45 degrees to the right.
  • "U": the speed of go kart increases from v to v+1.
  • "D": the speed of go kart decreases from v * to max(v − 1,0).

After each operation instruction is executed, the go kart will move forward v step along its direction, and will continue to respond to subsequent instructions after the movement is completed. In the process of moving forward, if you try to drive into an obstacle grid or out of the map at a certain step, it indicates that the go kart has collided, it will end its movement, and the speed v will be reduced to 0 while maintaining the direction. It should be noted that when the orientation is inclined by 45 degrees, in order to prevent the occurrence of "die piercing", if there are obstacles on both sides of the go kart, the go kart will also be considered to have collided. For example, the go kart faces down to the right and will now move from (x,y) to (x+1,y+1). If (x+1,y) and (x,y+1) are obstacles, the go kart collides.

Please write a program to report the coordinates of the go kart and whether there is a collision during the movement after each operation instruction is executed and the go kart completes the movement.

General idea of the topic

There is an n*m map, and each coordinate may be "#" (obstacle), " (flat land), "*" (go kart) one of them. Next, there are Q (1 < Q < 500) commands to output the position of the vehicle after each command and whether it hit the wall.

analysis

Simulation, set a function of moving and turning, read and execute the command in turn. When the speed is greater than 1, it may hit the wall at any time. I choose to take a step to see if the front is a wall. When moving in the syncline direction, judge whether it passes through the wall, that is, judge whether the other two in the 2 * 2 grid formed by the car and the next position are both walls.

I'm tired of writing. I can make a two-dimensional array and move it in eight directions, and then discuss it up, down, left and right, and the other directions. (

code
#include<stdio.h>
char map[100][100]; //Save map
char ww[550];  //Save command
int v=0,k,k1;  //0 up 1 right up 2 right... 
void rush(int x,int y,int k,int d,int v){   //v velocity in x y k d direction 
	if(k==0)
			return ;
	else{
		if(ww[k1-k]=='U'){
			v++;
		}
		else if(ww[k1-k]=='D'){
			if(v>0)  v--;     
		}
		else if(ww[k1-k]=='R'){
			d++;
			if(d==8) d = 0; 
		}
		else if(ww[k1-k]=='L'){
			d--;
			if(d==-1) d = 7;
		}
		int v2;
		int flag=0;
		if(v){
			if(d==0){
				for(v2=1;v2<=v;v2++){
					if(map[x-v2][y]=='#'){
						printf("Crash! %d %d\n",x-v2+1,y);
						rush(x-v2+1,y,k-1,0,0);
						flag =1;
						break;  //******************
					}
				}
				if(flag==0) {
					printf("%d %d\n",x-v,y);
					rush(x-v,y,k-1,0,v);
				}		
			}
		else if(d==2){
				for(v2=1;v2<=v;v2++){
					if(map[x][y+v2]=='#'){
						printf("Crash! %d %d\n",x,y+v2-1);	
						rush(x,y+v2-1,k-1,2,0);
						flag =1;
						break;  
					}
				}
				if(flag==0) {
					printf("%d %d\n",x,y+v);
					rush(x,y+v,k-1,2,v);
				}		
			}
		else if(d==4){
				for(v2=1;v2<=v;v2++){
				if(map[x+v2][y]=='#'){
					printf("Crash! %d %d\n",x+v2-1,y);
					rush(x+v2-1,y,k-1,4,0);
					flag =1;
						break;  
					}
				}
				if(flag==0) {
					printf("%d %d\n",x+v,y);
					rush(x+v,y,k-1,4,v);
				}		
			}
		else if(d==6){
			for(v2=1;v2<=v;v2++){
				if(map[x][y-v2]=='#'){
					printf("Crash! %d %d\n",x,y-v2+1);
				
					rush(x,y-v2+1,k-1,6,0);
						flag =1;
							break;  
					}
				}
			if(flag==0) {
					printf("%d %d\n",x,y-v);
					rush(x,y-v,k-1,6,v);
				}		
			}
		else if(d==1){
			for(v2=1;v2<=v;v2++){
				if(map[x-v2][y+v2]=='#'||(map[x-v2][y+v2-1]=='#'&&map[x-v2+1][y+v2]=='#')){
					printf("Crash! %d %d\n",x-v2+1,y+v2-1);
					rush(x-v2+1,y+v2-1,k-1,1,0);
						flag =1;
							break;  
					}
				}
				if(flag==0) {
					printf("%d %d\n",x-v,y+v);
					rush(x-v,y+v,k-1,1,v);
				}		
			}
		else if(d==7){
				for(v2=1;v2<=v;v2++){
				if(map[x-v2][y-v2]=='#'||(map[x-v2][y-v2+1]=='#'&&map[x-v2+1][y-v2]=='#')){
					printf("Crash! %d %d\n",x-v2+1,y-v2+1);
					rush(x-v2+1,y-v2+1,k-1,7,0);
					flag =1;
					break;  
					}
				}
			if(flag==0) {
					printf("%d %d\n",x-v,y-v);
					rush(x-v,y-v,k-1,7,v);
				}		
			}
		else if(d==5){
				for(v2=1;v2<=v;v2++){
				if(map[x+v2][y-v2]=='#'||(map[x+v2][y-v2+1]=='#'&&map[x+v2-1][y-v2]=='#')){
					printf("Crash! %d %d\n",x+v2-1,y-v2+1);
					rush(x+v2-1,y-v2+1,k-1,5,0);
						flag =1;
							break;  
					}
				}
			if(flag==0) {
					printf("%d %d\n",x+v,y-v);
					rush(x+v,y-v,k-1,5,v);
				}		
			}
		else if(d==3){
				for(v2=1;v2<=v;v2++){	
				if(map[x+v2][y+v2]=='#'||(map[x+v2-1][y+v2]=='#'&&map[x+v2][y+v2-1]=='#')){
					printf("Crash! %d %d\n",x+v2-1,y+v2-1);
					rush(x+v2-1,y+v2-1,k-1,3,0);
					flag =1;
					break;  
					}
				}
			if(flag==0) {
					printf("%d %d\n",x+v,y+v);
					rush(x+v,y+v,k-1,3,v);
				}		
			}

		}
		else{
			printf("%d %d\n",x,y);
			rush(x,y,k-1,d,v);
		}
	}
	return ; 
}
	
int main(){
	int m,n,i,j;
	scanf("%d %d",&m,&n);
	//Boundary 1~m 1~n 
	for(i=0;i<=m+1;i++){
		map[i][0]='#';
		map[i][n+1]='#';
	}	
	for(i=0;i<=n+1;i++){
		map[0][i]='#';
		map[m+1][i]='#';
	}
	//input 
	getchar();
	for(i=1;i<=m;i++){
		for(j=1;j<=n;j++){
			scanf("%c",&map[i][j]);
		}
		getchar();
	} 
	int x1,y1;
	// X1, Y1 positioning 
	for(i=1;i<=m;i++){
		for(j=1;j<=n;j++){
			if(map[i][j]=='*'){
				x1 = i ;
				y1 = j ;
			}
		}
	}
	scanf("%d",&k);
	k1 = k;
	scanf("%s",ww);
	rush(x1,y1,k,0,0); 
	return 0;
} 

H - Prime Ring Problem

General idea of the topic

Input n (0 < n < 16) to find a circle composed of 1~n. It is required that the sum of two adjacent numbers on the circle should be prime numbers and output in the format of 1 first and a row of numbers.

analysis

DFS + backtracking, v[20] is used to mark whether the number has been used. Each time a number is marked, the deep search ends and the mark is cancelled to achieve the effect of traversal

Roughly, n-1 numbers are arranged from the second number. If the addition of adjacent numbers is a prime number and the last number + 1 is also a prime number, it will be output.

code
#include<stdio.h>
#include<math.h> 
int v[20],num[20];
int isprime(int x,int y){
	int z = x + y;
	for(int i=2;i<=sqrt(z);i++){
		if(z%i==0)
			return 0;
	} 
	return 1;
}
void dfs(int x,int n){ //x stands for the number   
	if(x==n&&isprime(1,num[n])){
		for(int j = 1;j<n;j++)
			printf("%d ",num[j]);
			printf("%d",num[n]);
			printf("\n");
	}
	else{
		for(int i=2;i<=n;i++){
			if(isprime(num[x],i)&&v[i]==0){
				v[i] = 1;
				num[x+1] = i;
				dfs(x+1,n);
				v[i] = 0;
				
			}
		}
	}
}
	
int main(){
	int n,k=1;
	num[1] = 1; 
	while(scanf("%d",&n)!=EOF){
		if(k!=1) 	printf("\n"); // . . .  
		for(int i = 1;i<=n;i++)
			v[i] = 0;
		printf("Case %d:\n",k++);
		dfs(1,n);

	}
	return 0;
} 

M - Protect Sheep

General idea of the topic

If there are wolves and sheep on the grassland, the adjacent sheep of wolves and sheep will be eaten. You can put dogs on the grassland to prevent wolves from eating sheep. Ask whether you can protect all sheep. If you can, you can output a possibility. If you can't, you can't.

analysis

As long as the wolf and the sheep are not adjacent, you can surround the sheep / wolf with a circle of dogs. You can't eat it. To simplify the output, you can replace all the grass with dogs.

code
#include<iostream>
using namespace std;
// Why can't L17 and L18 be interchanged 
char cc[505][505];
int main(){
	int r,c;
	scanf("%d %d",&r,&c);
	int i,j,flag = 0;
for(i=1;i<=r;i++){
		for(j=1;j<=c;j++){
			cin >> cc[i][j];
		}
	}
for(i=1;i<=r;i++){
		for(j=1;j<=c;j++){
			if(cc[i][j]=='S'){
				if(cc[i-1][j]=='W'||cc[i+1][j]=='W'||cc[i][j-1]=='W'||cc[i][j+1]=='W')
					{
						printf("No\n");
						return 0;
					}
			}			
		}	
	}

	printf("Yes\n");
	for(i=1;i<=r;i++){
		for(j=1;j<=c;j++){
			if(cc[i][j]=='.')
			printf("D");
			else
			printf("%c",cc[i][j]);
		}
	printf("\n");
	}
	return 0;
} 

N - Maximum Product

General idea of the topic

There are n numbers (n < 18) to form a sequence. Find the maximum product of continuous subsequences. If they are less than 0, 0 will be output

analysis

To find the maximum product of continuous subsequences, traverse each subsequence to find the product. The maximum product can reach \ (10 ^ {18} \), and use long long to store the maximum value.

code
#include<stdio.h>
int a[20];
int main(){
	long long max,temp;
	int N,i,j,l,t,k=0;
	while(scanf("%d",&N)!=EOF){
	max = 0;
	k++;
	for(i=0;i<N;i++)
	scanf("%d",&a[i]); 
	for(i=0;i<N;i++){ //head 
		for(l=1;i+l<=N;l++){ //length 
			temp = 1;
			for(t=0;t<l;t++)
				 temp *=a[i+t];
			if(temp>max)
				max = temp;
		}		
	} 
	printf("Case #%d: The maximum product is %lld.\n\n",k,max);
}
	return 0;
	
}