ACM week 3

Keywords: Programming less

When the trees are withered, they are no longer flourishing. They are young and cherish the people beside the mirror

This week's question is a little difficult, I adjusted several of them to do it;
1
Define a 2D array:

int maze[5][5] = {

0, 1, 0, 0, 0,

0, 1, 0, 1, 0,

0, 0, 0, 0, 0,

0, 1, 1, 1, 0,

0, 0, 0, 1, 0,

};

It represents a maze, in which 1 represents the wall, 0 represents the path that can be walked, can only walk horizontally or vertically, can't walk obliquely, and requires programming to find the shortest path from the upper left corner to the lower right corner.
Input

   A 5 × 5 two-dimensional array represents a maze. Data guarantees a unique solution.
  Output
  
   The shortest path from the upper left corner to the lower right corner, in the format shown in the example.
  Sample Input0 1 0 0 0

0 1 0 1 0
0 0 0 0 0
0 1 1 1 0
0 0 0 1 0Sample Output(0, 0)
(1, 0)
(2, 0)
(2, 1)
(2, 2)
(2, 3)
(2, 4)
(3, 4)
(4, 4)

#include<stdio.h>
struct note
{
	int x;
	int y;
	int pre; 
};
struct note que[2501];
int a[51][51]={0},book[51][51]={0};
void f(int i)
{
	if(que[i].pre!=-1)
	{
		f(que[i].pre);
		printf("(%d, %d)\n",que[i].x,que[i].y);
	}
}
int main()
{
	int next[4][2]={{0,1},{1,0},{0,-1},{-1,0}};
	int head,tail;
	int i,j,k,n,m,startx,starty,p,q,tx,ty;
	for(i=0;i<5;i++)
      for(j=0;j<5;j++)
	  scanf("%d",&a[i][j]);
	 head=0;tail=1;
	 startx=0;starty=0;
	 que[head].x=startx;
	 que[head].y=starty;
	 que[head].pre=-1;
	 book[startx][starty]=1;
	 printf("(0, 0)\n");
	  while(head<tail)
	 {
	 	for(k=0;k<=3;k++)
	 	{
	 		tx=que[head].x+next[k][0];
	 		ty=que[head].y+next[k][1];
	 		if(tx<0||tx>4||ty<0||ty>4)
	 		continue;
	 		if(a[tx][ty]==0&&book[tx][ty]==0)
	 		{
	 			book[tx][ty]=1;
	 			que[tail].x=tx;
	 			que[tail].y=ty;
	 			que[tail].pre=head;
	 			tail++;
			 }
			 if(tx==4&&ty==4)
			 f(head);
		 }
		 head++;
	}
	printf("(4, 4)"); 
} 

**This is a bfs problem,
void f(int i)
{
if(que[i].pre!=-1)
{
f(que[i].pre);
printf("(%d, %d)\n",que[i].x,que[i].y);
}
}, this method is called the rear end method. pre is the one in front of que [i]. So it can be tracked all the time! , but the last step can't be specified. There's a first step, so you have to write it.
**
2.
There is no difference between pieces placed on a given shape board (the shape may be irregular). It is required that any two pieces cannot be placed in the same row or column in the chessboard. Please program and solve all feasible placement schemes C for the given shape and size chessboard to place k pieces.
Input

   The input contains multiple sets of test data. 

The first row of each group of data is two positive integers, n k, separated by a space, indicating that the board will be described in a n*n matrix and the number of pieces placed. n <= 8 , k <= n

When it is - 1 - 1, it indicates the end of input.

The next N lines describe the shape of the chessboard: each line has n characters, in which ා represents the chessboard area and. Represents the blank area (the data ensures that there are no redundant blank lines or columns).

  Output
  
   For each group of data, a row of output is given, and the number of output placement schemes C (data guarantee C < 2 ^ 31).
  Sample Input2 1

#.
.#
4 4
...#
...#.
.#...
#...
-1 -1
Sample Output2
1

#include<stdio.h>

#include<string.h>

char map[9][9];

int vis[50],n,k,ans;

void dfs(int m,int k)//m represents the number of layers, k represents the number of pieces.

{
	int i,j; 
    if(k==0)

    {

        ans++;

        return ;

    }

    for( i=m;i<n;i++)//Enumerate the possibility of putting flags in each row and column.

        for( j=0;j<n;j++)

    {

        if(map[i][j]=='.'||vis[j]==1) 
        continue;

        vis[j]=1;

        dfs(i+1,k-1);//The number of lines plus one, the number of pieces minus one.

        vis[j]=0;

    }

}

int main()

{
	int i,j;

    while(~scanf("%d %d",&n,&k))

    {

        if(n==-1&&k==-1)

            break;

        for(i=0;i<n;i++)

            scanf("%s",&map[i]);

        memset(vis,0,sizeof(vis));

        ans=0;

        dfs(0,k);

        printf("%d\n",ans);

    }

    return 0;

}

Just mark it in the code, a simple running chart problem.

You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of unit cubes which may or may not be filled with rock. It takes one minute to move one unit north, south, east, west, up or down. You cannot move diagonally and the maze is surrounded by solid rock on all sides.

Is an escape possible? If yes, how long will it take?

  Input
  
   The input consists of a number of dungeons. Each dungeon description starts with a line containing three integers L, R and C (all limited to 30 in size). 

L is the number of levels making up the dungeon.

R and C are the number of rows and columns making up the plan of each level.

Then there will follow L blocks of R lines each containing C characters. Each character describes one cell of the dungeon. A cell full of rock is indicated by a '#' and empty cells are represented by a '.'. Your starting position is indicated by 'S' and the exit by the letter 'E'. There's a single blank line after each level. Input is terminated by three zeroes for L, R and C.
Output

   Each maze generates one line of output. If it is possible to reach the exit, print a line of the form 
   

   
    Escaped in x minute(s). 

where x is replaced by the shortest time it takes to escape.

If it is not possible to escape, print the line

    Trapped! 
   
  Sample Input3 4 5

S...
.###.
.##...
###.#

##.##
##...

#.###
####E

1 3 3
S##
#E#

0 0 0
Sample OutputEscaped in 11 minute(s).
Trapped!

#include<stdio.h>
#include<string.h>
int next[6][3] = {{0,0,1},{0,0,-1},{0,1,0},{0,-1,0},{1,0,0},{-1,0,0}};//The three numbers represent X,Y,Z respectively;
struct note
{
	int x,y,z,s;
};
int i,j,z,startx,starty,t,startz,tx,ty,tz,p,q,r,flag,m,n,k,ans;
struct note que[100000];
int  book[35][35][35]={0};
char map[35][35][35];
int head,tail;
int check(int x,int y,int z)
{
    if(x<0 || y<0 || z<0 || x>=k || y>=n || z>=m)
	        return 1;
    else if(map[x][y][z] == '#')        
	   return 1;   
	else if(book[x][y][z])        
	   return 1;    
	return 0;
}

int bfs()
{
	head=1;tail=1;
	que[tail].x=startx;
	que[tail].y=starty;
	que[tail].z=startz;
	que[tail].s=0;
	tail++;
	book[startx][starty][startz]=1;
	flag=0;
	while(head<tail)
	{
		for(t=0;t<6;t++)
		{
			tx=que[head].x+next[t][0];
			ty=que[head].y+next[t][1];
			tz=que[head].z+next[t][2];
			if(check(tx,ty,tz))
			continue;
		    else
			{
				book[tx][ty][tz]=1;
				que[tail].x=tx;
				que[tail].y=ty;
				que[tail].z=tz;
				que[tail].s=que[head].s+1;
				tail++;
			}
			if(tx==p&&ty==q&&tz==r)
			{
				flag=1;
				break;
			}
		}
			if(flag==1)
			{
				break;
			}
			head++;
	}
		return que[tail-1].s;
}
int main()
{
	while(scanf("%d %d %d",&k,&n,&m),k+n+m)
	{
		for(i=0;i<k;i++)
		{
			for(j=0;j<n;j++)
			{
				scanf("%s",map[i][j]);
				for(z=0;z<m;z++)
				{
					if(map[i][j][z]=='S')
					{
						startx=i;
						starty=j;
						startz=z;
					}
					else if(map[i][j][z]=='E')
					{
						p=i;q=j;r=z;
				    }
				}
			}
	    }
		memset(book,0,sizeof(book));
		ans=bfs();
	    if(ans)
	        printf("Escaped in %d minute(s).\n",ans);
	    else
	        printf("Trapped!\n");
	    ans=0;
    }
    return 0;
	
}

It's a bfs of three-dimensional array. It's OK to record the steps. Three-dimensional array should be written carefully, so mark it in the code. Simple annotation, can understand two-dimensional bfs
5.
Farmer John knows that an intellectually satisfied cow is a happy cow who will give more milk. He has arranged a brainy activity for cows in which they manipulate an M × N grid (1 ≤ M ≤ 15; 1 ≤ N ≤ 15) of square tiles, each of which is colored black on one side and white on the other side.
As one would guess, when a single white tile is flipped, it changes to black; when a single black tile is flipped, it changes to white. The cows are rewarded when they flip the tiles so that each tile has the white side face up. However, the cows have rather large hooves and when they try to flip a certain tile, they also flip all the adjacent tiles (tiles that share a full edge with the flipped tile). Since the flips are tiring, the cows want to minimize the number of flips they have to make.
Help the cows determine the minimum number of flips required, and the locations to flip to achieve that minimum. If there are multiple ways to achieve the task with the minimum amount of flips, return the one with the least lexicographical ordering in the output when considered as a string. If the task is impossible, print one line with the word "IMPOSSIBLE".
Input

   Line 1: Two space-separated integers: 
   M and 
   N 

Lines 2...
M+1: Line
i+1 describes the colors (left to right) of row i of the grid with
N space-separated integers which are 1 for black and 0 for white
Output

   Lines 1..
   M: Each line contains 
   N space-separated integers, each specifying how many times to flip that particular location.
  Sample Input4 4

1 0 0 1
0 1 1 0
0 1 1 0
1 0 0 1Sample Output0 0 0 0
1 0 0 1
1 0 0 1
0 0 0 0

This problem uses binary, for example, 4-bit binary can represent 16 cases, and uses mask operation.

#include<stdio.h>
#include<string.h>
#define N 16

int g[N][N], t[N][N], f[N][N];
int cnt, n, m;
int x[4] = {0, 0, -1, 1};
int y[4] = { -1, 1, 0, 0};
void flip(int i, int j)

{
	int k;

    ++cnt, f[i][j] = 1;//Add one step and mark.
    t[i][j] = !t[i][j];//Flip yourself

    for(k = 0; k < 4; ++k)
        if(i + x[k] > -1 && j + y[k] > -1)

            t[i + x[k]][j + y[k]] ^= 1;//Look in four directions. XOR is used here, i.e. the same is 0, the different is 1, because the title requires to turn one and move four.

}

 

int ok(int k)
{
	 int i,j;
    cnt = 0;

    memcpy(t, g, sizeof(t));//Copy original table

    for( j = 0; j < m; ++j)

        if(k & (1 << (m - 1 - j)))//Find the turning point by mask operation

            flip(0, j);

 

 

    for( i = 1; i < n; ++i)

        for( j = 0; j < m; ++j)

            if(t[i - 1][j]) 
			flip(i, j);//See if the previous line is 1, if it is 1, flip

 

    for( j = 0; j < m; ++j)

        if(t[n - 1][j]) //See if the last line is all 0
		return 0;

    return 1;

}

 

int main()

{

    int ans, p;
    int i,j;

    while(~scanf("%d %d", &n, &m))

    {

        for( i = 0; i < n; ++i)

            for( j = 0; j < m; ++j)

                scanf("%d", &g[i][j]);

        ans = n * m + 1, p = -1;

        for(i = 0; i < (1 << m); ++i)//List each situation

            if(ok(i) && cnt < ans) //Judgment is the number of steps in this situation, and the situation.
                ans = cnt, p = i;

 

        memset(f, 0, sizeof(f));

        if(p >= 0)

        {

            ok(p);

            for(i = 0; i < n; ++i)

                for( j = 0; j < m; ++j)

                    printf("%d%c", f[i][j], j < m - 1 ? ' ' : '\n');

        }

        else puts("IMPOSSIBLE");

    }

    return 0;

}

Of course, such a powerful code is written by reference to others. Just have a look
5.
My birthday is coming up and traditionally I'm serving pie. Not just one pie, no, I have a number N of them, of various tastes and of various sizes. F of my friends are coming to my party and each of them gets a piece of pie. This should be one piece of one pie, not several small pieces since that looks messy. This piece can be one whole pie though.

My friends are very annoying and if one of them gets a bigger piece than the others, they start complaining. Therefore all of them should get equally sized (but not necessarily equally shaped) pieces, even if this leads to some pie getting spoiled (which is better than spoiling the party). Of course, I want a piece of pie for myself too, and that piece should also be of the same size.

What is the largest possible piece size all of us can get? All the pies are cylindrical in shape and they all have the same height 1, but the radii of the pies can be different.
Input

   One line with a positive integer: the number of test cases. Then for each test case:
   
    One line with two integers N and F with 1 ≤ N, F ≤ 10 000: the number of pies and the number of friends.
    One line with N integers ri with 1 ≤ ri ≤ 10 000: the radii of the pies.
   
  Output
  
   For each test case, output one line with the largest possible volume V such that me and my friends can all get a pie piece of size V. The answer should be given as a floating point number with an absolute error of at most 10
   −3.
  Sample Input3

3 3
4 3 3
1 24
5
10 5
1 4 2 3 4 5 6 5 4 2Sample Output25.1327
3.1416
50.2655

#include<stdio.h>
#define pi 3.1415926535868
int main()
{
	int n,i,f,t,r,sum2;
	double a[10003];
	double min,max,mid,s,sum=0;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d %d",&n,&f);
		for(i=0;i<n;i++)
		{
			scanf("%d",&r);
			s=pi*r*r;//Volume seeking
			a[i]=s;
			sum=a[i]+sum;//Maximum volume
		}
		min=0;max=(sum/(f+1))*1.0;//No one can share the size of the cake 
		while(min+1e-4<max)//In fact, it means that MIN and MAX don't want to be the same
		{
				mid=(max+min)/2;//Start two points
				sum2=0;
				for(i=0;i<n;i++)
				{
					sum2=(int)(a[i]/mid)+sum2;//Number of people available
				}
				if(sum2>=(f+1))
				min=mid;//More people, more people
				else//Otherwise, each person will get less points
				max=mid;
		}
		printf("%.4lf\n",mid);
	}
	return 0;
 } 

The main idea is the idea of dichotomy. See the code specifically
6.
Now,given the equation 8x^4 + 7x^3 + 2x^2 + 3x + 6 == Y,can you find its solution between 0 and 100;

Now please try your lucky.
InputThe first line of the input contains an integer T(1<=T<=100) which means the number of test cases. Then T lines follow, each line has a real number Y (fabs(Y) <= 1e10);OutputFor each test case, you should just output one real number(accurate up to 4 decimal places),which is the solution of the equation,or "No solution!",if there is no solution for the equation between 0 and 100.Sample Input2
100
-4Sample Output1.6152
No solution!

#include<stdio.h> 
#include<math.h>
  double f(double x)
  {
     return (8*x*x*x*x+7*x*x*x+2*x*x+3*x+6);
   } 
  int main()
  {
      
    int t;
      double l,r;
     while(~scanf("%d",&t))
     {
         while(t--)
         {
             double y;
             scanf("%lf",&y);
             if(f(0)>y||f(100)<y)//Increasing, judge whether it is a solution between 0 and 100.
             {
                 printf("No solution!\n");
                 continue;
             }
           
             l=0.0;r=100.0;
             double mid=50.0;
             while(fabs(f(mid)-y)>1e-5)//Judge inequality
             {
                 if(f(mid)>y)
                 {
                     r=mid;
                     mid=(l+r)/2.0;
                     
                 }
                 else
                 {
                     l=mid;
                     mid=(l+r)/2.0;
                 }
                 
             }
             printf("%.4lf\n",mid);
         }
     }
     return 0;
 }

It's also a dichotomy problem. Find a solution from 0 to 100 li
6
.
You must feel that drinking coke after exercise is a very comfortable thing, but see you don't think so. Because every time seeyou bought a coke, a Niu asked to share this coke with seeyou, and he must drink as much as seeyou. However, there are only two cups in seeyou's hand. Their capacity is N ml and M ml respectively. The volume of coke is s (s < 101) ml (just full of a bottle). The three cups can pour coke to each other (all without scale, and S==N+M, 101 > s > 0, N > 0, M > 0). Smart ACMER do you think they can share equally? If you can output the minimum number of times to pour coke, if you can't output "NO".
Input three integers: the volume of S cola, N and M are the capacity of two cups, ending with "0.00". If Output can be divided equally, please Output the minimum number of times to be inverted, otherwise Output "NO". Sample Input7 4 3
4 1 3
0 0 0Sample OutputNO
3
This question is not very good. I understand the big guy's code, saying that it is breadth first search, and I search in six situations

#include<stdio.h>
#include<string.h>
int b[3],book[101][101][101],half;

struct node

{

    int cur[3],s;        //cur is the coke content of each quilt


}p,temp;         
struct node que[100000];
void bfs()
{
	int head=1,tail=1;
    que[tail].cur[0]=b[0];         
    que[tail].cur[1]=que[tail].cur[2]=que[tail].s=0;//Set 0 every time  
    tail++;                  
    while(head<tail)//bfs
    {
    	p=que[head];

        for(int i=0;i<3;i++)     
        {

            if(p.cur[i]>0)      

            {

                for(int j=0;j<3;j++)  

                {

                    temp=p;
					        
                    if(i==j)  continue ;   
                    if(temp.cur[i]>b[j]-temp.cur[j])   
                    {
                        temp.cur[i]-=b[j]-temp.cur[j];    
                        temp.cur[j]=b[j];
                    }
                    else    
                    {
                        temp.cur[j]+=temp.cur[i];
                        temp.cur[i]=0;
                    }
                    if(!book[temp.cur[0]][temp.cur[1]][temp.cur[2]])  
                    {
                        book[temp.cur[0]][temp.cur[1]][temp.cur[2]]=1; 
                        temp.s++;
                        if((temp.cur[0]==half&&temp.cur[1]==half)|| (temp.cur[0]==half&&temp.cur[2]==half)||(temp.cur[1]==half&&temp.cur[2]==half))
                            {
                                printf("%d\n",temp.s);
                                return ;             

                            }
                            que[tail]=temp;
                            tail++;
                            head++;
                    }

                }

            }

        }
    }

       printf("NO\n"); 
}

int main()

{

    while(scanf("%d %d %d",&b[0],&b[1],&b[2]),b[0]+b[1]+b[2])  

    {

        memset(book,0,sizeof(book));  

        book[b[0]][b[1]][b[2]]=1;   

        if(b[0]%2)   printf("NO\n"); 

        else 
		{
		half=b[0]/2;
		bfs();
		}

    }

    return 0;

}

You can understand this problem well. There are six ways to pour three cups: 12, 13, 21, 23, 31, 32;
That's all for today.

Published 6 original articles, won praise 1, visited 424
Private letter follow

Posted by Merdok on Sat, 15 Feb 2020 18:23:21 -0800