Blue Bridge Cup card transposition

/ *
 * Subject description:
Card transposition
you played Huarong game?
This is a similar but simpler game.
See the following lattice 3 x 2
+ --- + --- + --- +
| A | * | * |
+ --- + --- + --- +
| B | | * |
+ --- + --- + --- +
where to put 5 cards, where A represents Guan Yu, B on behalf of Zhang Fei, * on behalf of the soldiers. There is also a grid is empty. You can put a card to move to an adjacent space to go (not diagonally adjacent). The objective is to: Guan Yu and Zhang Fei exchange position, the other cards can be just about anywhere.
Input formats: two lines of six characters representing the current situation
output format:
An integer representing the minimum number of steps in order to put AB transposition (other card position random)
For example, enter:
* A
** B
program should output:
17
again For example, type:
AB
***
program should output:
12
 * * /

Just think of when you see the method entitled BFS, first original condition into the queue, and then remove the comparison did not reach the termination, the vacancy from the beginning step, each case up and down, a total of four cases (of course, cross-border negligible), the situation will take a step stored in the queue, and then turn out the comparison, if they meet the termination conditions, the resulting number of steps, as is more the case step by step, so the first match to the termination conditions must be at least step number, if you do not meet the termination conditions, continue to start from space, take a step up and down, back on the queue, all circulation is roughly removed from the queue, compared to non-compliance with another step on the basis of the case out of the then take out. . . But I do not use this method. . .

DFS is to start from space, to recursively traverse up and down, encountered the termination of recording the number of steps compared with the original number of steps, number of steps to ensure that each store is minimal.

BFS is to save all the circumstances of each step up, find the minimum number of steps
DFS is traversing each of the number of steps to reach the termination condition, take the minimum

There are several points,
1, do not have to store a two-dimensional array, simply save your A, B, and coordinate space is saved the whole situation
2, the array needs to be marked so as not to turn back
3, when the tag array must be erased first back, to ensure that another traversal may traverse
4, the array direction of the vector saved in each direction x, y increment, go up and down each time convenient

On the code:

public class huarongdao {
 static class Gezi{
  public int x;
  public int y;
  public Gezi() {
  }
 };
 static int go[][]={{0,-1},{0,1},{-1,0},{1,0}};
 static Gezi a;
 static Gezi b;
 static Gezi k;
 static int minstep=10000000;
 static int[][][][][][] vis;
 public static void main(String[] args) throws IOException {
  vis=new int[3][3][3][3][3][3];
  a=new Gezi();
  b=new Gezi();
  k=new Gezi();
  Scanner scanner=new Scanner(System.in);
  
 
  
  for(int i=0;i<2;i++){
   String string=scanner.next();
   string.trim();
   for(int j=0;j<3;j++){
    if (string.charAt(j)=='A') {
     a.x=i;
     a.y=j;
    }
    if (string.charAt(j)=='B') {
     b.x=i;
     b.y=j;
    }
    if (string.charAt(j)=='#') {
     k.x=i;
     k.y=j;
    }
   }
  }
  dfs(a.x,a.y,b.x,b.y,k.x,k.y,0);
  System.out.println(minstep);
  
  }


 private static void dfs(int ax,int ay,int bx,int by,int kx,int ky,int step) {
  
  if (ax==b.x&&ay==b.y&&bx==a.x&&by==a.y) {
   minstep=Math.min(minstep, step);
   return;
  }
  /*
   * A#B
***
   * */
  if(ax<0||ax>1||ay<0||ay>2){
   return;
  }
  if(bx<0||bx>1||by<0||by>2){
   return;
  }
  
  if(kx<0||kx>1||ky<0||ky>2){
   return;
  }
  
  if (vis[ax][ay][bx][by][kx][ky]==1) {
   return;
  }
  vis[ax][ay][bx][by][kx][ky]=1;
  for(int i=0;i<4;i++){
   int xx=kx+go[i][0];
   int yy=ky+go[i][1];
   if(xx==ax&&yy==ay){
    dfs(kx, ky, bx, by, xx, yy, step+1);
   }
   else {
    if(xx==bx&&yy==by){
     dfs(ax, ay, kx, ky, xx, yy, step+1);
    }
   
    else{
     dfs(ax, ay, bx, by, xx, yy, step+1);
    }
   }
   
  }
  vis[ax][ay][bx][by][kx][ky]=0;
  
 }
}
Published 14 original articles · won praise 2 · Views 5389

Guess you like

Origin blog.csdn.net/Wolf_South/article/details/78630370
Recommended