【九度】题目1482:玛雅人的密码

108 篇文章 0 订阅
102 篇文章 5 订阅
题目地址:http://ac.jobdu.com/problem.php?pid=1482
题目描述:

玛雅人有一种密码,如果字符串中出现连续的2012四个数字就能解开密码。给一个长度为N的字符串,(2=<N<=13)该字符串中只含有0,1,2三种数字,问这个字符串要移位几次才能解开密码,每次只能移动相邻的两个数字。例如02120经过一次移位,可以得到20120,01220,02210,02102,其中20120符合要求,因此输出为1.如果无论移位多少次都解不开密码,输出-1。

输入:

输入包含多组测试数据,每组测试数据由两行组成。
第一行为一个整数N,代表字符串的长度(2<=N<=13)。
第二行为一个仅由0、1、2组成的,长度为N的字符串。

输出:

对于每组测试数据,若可以解出密码,输出最少的移位次数;否则输出-1。

样例输入:
5
02120
样例输出:
1
来源:
2012年清华大学计算机研究生机试真题
找出2012。每次移动都是相邻位交换位置。
典型的bfs吧。
没什么大的问题。
和一般的bfs问题相比的话,可能处理visited数组要相对麻烦些。
我用的是map。
C++ AC
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <string.h>
#include <queue>
#include <map>
using namespace std;
map<string , int> visitedMap; 
map<string,int>::iterator it;
int n;    
struct Node{
    char input[14];
    int step;
};
string str;
Node node; 
void swapArr(char *input, int a, int b) {
    if (input[a] == input[b]) {
        return;
    }
    char temp = input[a];
    input[a] = input[b];
    input[b] = temp;
} 
bool judgeIs2012(char *input) {
    for (int i = 0; i < n-3; i++) {
        if (input[i] == '2' && input[i+1] == '0'
                && input[i+2] == '1' && input[i+3] == '2') {
            return true;
        }
    }
    return false;
}    
int bfs() {
    queue<Node> q ;
    while(!q.empty()) q.pop();
    q.push(node);
    while (!q.empty()) {
        node = q.front();
        q.pop();
        if (judgeIs2012(node.input)) {
            return node.step;
        }
        str = node.input;
        it = visitedMap.find(node.input);  
        if (it == visitedMap.end() || it->second == 0) {
            visitedMap.insert(make_pair(str,1));  
        }
        char *newInput;
        newInput = new char[str.size()];
        for (int i = 0; i < n-1; i++) {
            strcpy(newInput,node.input);
            swapArr(newInput,i,i+1);
            str = newInput;
            it = visitedMap.find(str);  
            if (it == visitedMap.end() || it->second == 0) {
                visitedMap.insert(make_pair(str,1));  
                Node temp;
                temp.step = node.step+1;
                strcpy(temp.input,newInput);
                q.push(temp);
            }
        }
    }
    return -1;
}
int main()
{
    while(scanf("%d",&n) != EOF ){
        char input[14];
        scanf("%s",input);
        if(n < 4){
            printf("-1\n");
            continue;
        }
        visitedMap.clear();
        node.step = 0;
        strcpy(node.input,input);
        printf("%d\n",bfs());
    }     
    return 0;
} 
/**************************************************************
    Problem: 1482
    User: wangzhenqing
    Language: C++
    Result: Accepted
    Time:30 ms
    Memory:1324 kb
****************************************************************/

Java AC

import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Queue;
import java.util.Scanner;  
public class Main {
    /*
     * 1407
     */
    private static char array[];
    private static int n;
    private static Map<String, Integer> visitedMap;
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            n = scanner.nextInt();
            String input = scanner.next();
            int len = input.length();
            if (len < 4) {
                System.out.println(-1);
                continue;
            }
            array = input.toCharArray();
            visitedMap = new HashMap<String, Integer>();
            System.out.println(bfs());
        }
    } 
    private static int bfs() {
        Node node = new Node(array, 0);
        Queue<Node> queue = new LinkedList<Node>();
        queue.add(node);
        while (!queue.isEmpty()) {
            Node newNode = queue.poll();
            String newInput = String.valueOf(newNode.input);
            if (judgeIs2012(newNode.input)) {
                return newNode.step;
            }
            visitedMap.put(newInput, 1);
            for (int i = 0; i < n-1; i++) {
                char tempArr[] = newInput.toCharArray();
                swapArr(tempArr,i,i+1);
                String secInput = String.valueOf(tempArr);
                Integer num = visitedMap.get(secInput);
                if (num == null || num == 0) {
                    visitedMap.put(secInput, 1);
                    Node tmpNode = new Node(tempArr, newNode.step+1);
                    queue.add(tmpNode);
                }
            }
        }
        return -1;   
    }     
    private static void swapArr(char[] input, int i, int j) {
        char temp = input[i];
        input[i] = input[i+1];
        input[i+1] = temp;
    } 
    private static boolean judgeIs2012(char[] input) {
        for (int i = 0; i < n-3; i++) {
            if (input[i] == '2' && input[i+1] == '0' 
                && input[i+2] == '1' && input[i+3] == '2') {
                return true;
            }
        }
        return false;
    } 
    private static class Node{
        private char input[];
        private int step;
        public Node(char[] input, int step) {
            super();
            this.input = input;
            this.step = step;
        }
    }
}
/**************************************************************
    Problem: 1482
    User: wangzhenqing
    Language: Java
    Result: Accepted
    Time:260 ms
    Memory:31640 kb
****************************************************************/
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值