Towers of Hanoi Grid (Regular)

Insert picture description hereInsert picture description hereInsert picture description hereInsert picture description here
Question: Given a nail in





n n format and d disks with increasing size from top to bottom (placed at (1,1) coordinates), ask to move all d disks to (n, n) What is the minimum number of moves in the coordinates, of course, there are restrictions on the problem: 1. Only one can be placed on each nail (except for the first and last one). 2. You can only move down or to the right; this question can be regarded as a sign-in question. . I misread the title myself, and I thought it was possible to put more than one nail. . . . . Speechless. . . In fact, if you understand the question in place, you can find the rule by writing twice; because d disks must move from the upper left corner to the lower right corner, then no matter how you move, there must be a moving path for the last one to the bottom right corner; then For example , a matrix of 3 3;
Insert picture description here
then the largest disk is the moving path of the green part above; then we can find that in this matrix, if d=1, d=2, d=3=d=4, d=5 It is all true;
then when d=6, there is a problem;
Insert picture description here
this must be impossible to move;
because when the largest disk moves to the end, the number of grids traversed is 2*(n-1) (here Excluding the starting point), the rest of the remaining grids are n n-2 (n-1), because we need to empty one out to get there, so if the number of d is greater than or equal to n n-2 (n-1)+1 then It is impossible; that is, it means: the
Insert picture description here
asterisk is a disk, and the fork is an empty seat, if d>=n n+2 (n-1)+1 is impossible for a long time;
so the AC code:

#include<bits/stdc++.h>
using namespace std;
int main(){
    
    
	int T;
	scanf("%d",&T);
	int g=1;
	while(T--){
    
    
		  int d,n;
		  scanf("%d %d",&d,&n);
		  if(d>=n*n-2*(n-1)+1)printf("Grid #%d: impossible\n",g++);
		  else {
    
    
		  	 printf("Grid #%d: %d\n",g++,d*2*(n-1));
		  }
        puts("");
	}
	return 0;
} 

Guess you like

Origin blog.csdn.net/qq_44555205/article/details/104545080