蓝桥杯-卡片换位(java)

卡片换位

你玩过华容道的游戏吗?
这是个类似的,但更简单的游戏。
看下面 3 x 2 的格子

+—+—+—+
| A | * | * |
+—+—+—+
| B | | * |
+—+—+—+

在其中放5张牌,其中A代表关羽,B代表张飞,* 代表士兵。
还有一个格子是空着的。

你可以把一张牌移动到相邻的空格中去(对角不算相邻)。
游戏的目标是:关羽和张飞交换位置,其它的牌随便在哪里都可以。

输入格式:
输入两行6个字符表示当前的局面

输出格式:
一个整数,表示最少多少步,才能把AB换位(其它牌位置随意)

例如,输入:
* A
**B

程序应该输出:
17

再例如,输入:
A B
* **

程序应该输出:
12

资源约定:
峰值内存消耗 < 256M
CPU消耗 < 1000ms

请严格按要求输出,不要画蛇添足地打印类似:“请您输入…” 的多余内容。

所有代码放在同一个源文件中,调试通过后,拷贝提交该源码。

注意: main函数需要返回0
注意: 只使用ANSI C/ANSI C++ 标准,不要调用依赖于编译环境或操作系统的特殊函数。
注意: 所有依赖的函数必须明确地在源文件中 #include , 不能通过工程设置而省略常用头文件。

提交时,注意选择所期望的编译器类型。

import java.util.*;

public class KaPianHuanWei {
    static Queue<Pos> q = new LinkedList<Pos>();
    static int Pos_a_x = -1, Pos_a_y = -1;
    static int Pos_b_x = -1, Pos_b_y = -1;
//  static Pos sign = new Pos(-1,-1);
    static int[][] arr = {{-1,0},{1,0},{0,-1},{0,1}};
    public static void main(String[] args) throws InterruptedException {
        String[][] str = new String[2][3];
        Scanner sc = new Scanner(System.in);
        for (int i = 0; i < 2; i++) {
            String temp = sc.nextLine();
            for (int j = 0; j < 3; j++) {
                str[i][j] = temp.substring(j, j+1);
                if(str[i][j].equals("A")){Pos_a_x = i; Pos_a_y = j;}
                if(str[i][j].equals("B")){Pos_b_x = i; Pos_b_y = j;}
            }
        }
        sc.close();
        long start = System.currentTimeMillis();
        Pos result = null;
        q.add(new Pos(0,str));
        while(!q.isEmpty()){
            Pos t = q.remove();
            if(finish(t.str)){ result = t; break;}
            else{
                move(t);
            }
        }
        System.out.println(result.step);
//      System.out.println(result.s);
        long end = System.currentTimeMillis();
        System.out.println("用时:"+(end-start)+"ms");
    }
    public static void move(Pos p) throws InterruptedException{
        String[][] t = p.str;
        for (int i= 0;  i< 2; i++) {
            for (int j = 0; j < 3; j++) {
                if(t[i][j].equals(" ")){
                    if(i-1>=0 && (i-1!=p.x || j!=p.y)){
                        String[][] temp = new String[2][3];
                        copy(t, temp);
                        temp = swap(temp,i,j,i-1,j);
                        q.add(new Pos(i,j,p.step+1,temp,p.s+"->"+t[i-1][j]));
//                      p(temp);
                    }
                    if(i+1<=1 && (i+1!=p.x || j!=p.y)){
                        String[][] temp = new String[2][3];
                        copy(t, temp);
                        temp = swap(temp,i,j,i+1,j);
                        q.add(new Pos(i,j,p.step+1,temp,p.s+"->"+t[i+1][j]));
//                      p(temp);
                    }
                    if(j-1>=0 && (i!=p.x || j-1!=p.y)){
                        String[][] temp = new String[2][3];
                        copy(t, temp);
                        temp = swap(temp,i,j,i,j-1);
                        q.add(new Pos(i,j,p.step+1,temp,p.s+"->"+t[i][j-1]));
//                      p(temp);
                    }
                    if(j+1<=2 && (i!=p.x || j+1!=p.y)){
                        String[][] temp = new String[2][3];
                        copy(t, temp);
                        temp = swap(temp,i,j,i,j+1);
                        q.add(new Pos(i,j,p.step+1,temp,p.s+"->"+t[i][j+1]));
//                      p(temp);
                    }
                }
            }
        }
    }
    public static void copy(String[][] src,String[][] dis){
        for (int i = 0; i < src.length; i++) {
            for (int j = 0; j < src[i].length; j++) {
                dis[i][j] = src[i][j];
            }
        }
    }
    public static String[][] swap(String[][] s,int i,int j,int x,int y){
        String t = s[i][j];
        s[i][j] = s[x][y];
        s[x][y] = t;
        return s;
    }
    public static boolean finish(String[][] str){
        int count = 0;
        for (int i = 0; i < 2; i++) {
            for (int j = 0; j < 3; j++) {
                if(str[i][j].equals("A") && i==Pos_b_x && j==Pos_b_y) count++;
                if(str[i][j].equals("B") && i==Pos_a_x && j==Pos_a_y) count++;
            }
        }
        if(count == 2)return true;
        return false;
    }
//  public static void p(String[][] s) throws InterruptedException{
//      for (int i = 0; i < s.length; i++) {
//          for (int j = 0; j < s[i].length; j++) {
//              System.out.print(s[i][j]);
//          }
//          System.out.println();
//      }
//      System.out.println();
//      Thread.sleep(500);
//  }
}
class Pos{
    int x = -1;
    int y = -1;
    int step;
    String s = "";
    String[][] str = new String[2][3];
    public Pos() {
    }
    public Pos(int step, String[][] str) {
        super();
        this.step = step;
        this.str = str;
    }
    public Pos(int x, int y, int step, String[][] str,String s) {
        super();
        this.x = x;
        this.y = y;
        this.step = step;
        this.str = str;
        this.s = s;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值