HDU 5010 Get the Nut(2014 ACM/ICPC Asia Regional Xi'an Online)

思路:广搜, 因为空格加上动物最多只有32个那么对这32个进行编号,就能可以用一个数字来表示状态了,因为只有 ‘P’   ‘S‘ ‘M‘ ‘.‘ 那么就可以用4进制刚好可以用64位表示。

接下去每次就是模拟了。

注意:  ‘S’ 不是只有一个。

一个东西如果不是‘P‘在动的话要先判断周围有没有‘P’,有的话要先吃掉

        ‘P‘在动的时候如果一个位置周围有多个东西,都要吃掉。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<time.h>
#include<string>
#define REP(i,n) for(int i=0;i<n;++i)
#define REP1(i,a,b) for(int i=a;i<=b;++i)
#define REP2(i,a,b) for(int i=a;i>=b;--i)
#define MP make_pair
#define LL long long
#define ULL unsigned long long
#define X first
#define Y second
#define MAXN 1000050
using namespace std;
map<ULL, int> mp;
int dx[] = { 0, 0, 1, -1 };
int dy[] = { 1, -1, 0, 0 };
char s[6][9];
int id[6][9];
int qx[100];
int qy[100];
int qcnt;
ULL q[MAXN];
ULL bas[100];
struct node {
    char a[6][9];
    int scnt;
    node() {
    }
    ;
    node(ULL now) {
        scnt = 0;
        REP(i,6)
            REP(j,8)
                a[i][j] = s[i][j];
        REP(i,qcnt)
        {
            ULL k = now & 3;
            now >>= 2;
            if (k == 0)
                continue;
            if (k == 1) {
                a[qx[i]][qy[i]] = ‘S‘;
                scnt++;
            }
            if (k == 2)
                a[qx[i]][qy[i]] = ‘M‘;
            if (k == 3)
                a[qx[i]][qy[i]] = ‘P‘;
        }
    }

    void debug(){
        puts("-------");
        REP(i,6)
        {
            REP(j,8)putchar(a[i][j]);
            puts("");
        }
        puts("------------------");
    }
};

void init() {
    qcnt = 0;
    int cid = 0;
    memset(id, -1, sizeof(id));
    REP(i,6)
        REP(j,8)
        {
            if (s[i][j] == ‘#‘ || s[i][j] == ‘N‘)
                continue;
            qx[qcnt] = i;
            qy[qcnt++] = j;
            id[i][j] = cid++;
        }
}

ULL geths(char s[6][9]) {
    ULL ans = 0;
    REP(i,6)
        REP(j,8)
        {
            if (s[i][j] == ‘S‘) {
                ans += bas[id[i][j] << 1];
                continue;
            }
            if (s[i][j] == ‘M‘) {
                ans += 2 * bas[id[i][j] << 1];
                continue;
            }
            if (s[i][j] == ‘P‘) {
                ans += 3 * bas[id[i][j] << 1];
            }
        }
    return ans;
}

bool check(int x, int y) {
    if (x < 0 || x >= 6 || y < 0 || y >= 8)
        return false;
    return true;
}

node move(node a, int x, int y, int dxx, int dyy, int &p) {
    char c = a.a[x][y];
    while (true) {
        int xx = x + dxx;
        int yy = y + dyy;
        if ((!check(xx, yy)) || a.a[xx][yy] != ‘.‘) {
            p = 1;
            return a;
        }
        a.a[xx][yy] = a.a[x][y];
        a.a[x][y] = ‘.‘;
        if (c == ‘P‘) {
            int flag=0;
            for (int j = 0; j < 4; ++j) {
                int px = xx + dx[j];
                int py = yy + dy[j];
                if (!check(px, py))
                    continue;
                if (a.a[px][py] == ‘N‘) {
                    p = 0;
                    return a;
                }
                if (a.a[px][py] == ‘S‘ || a.a[px][py] == ‘M‘) {
                    if (a.a[px][py] == ‘S‘) {
                        a.scnt--;
                        if (a.scnt == 0) {
                            p = 0;
                            return a;
                        }
                    }
                    a.a[px][py] = ‘.‘;
                    flag=1;
                }
            }
            if(flag)
            {
                p=1;
                return a;
            }
        } else {
            for (int i = 0; i < 4; ++i) {
                int px = xx + dx[i];
                int py = yy + dy[i];
                if (!check(px, py))
                    continue;
                if (a.a[px][py] == ‘P‘) {
                    if (c == ‘S‘) {
                        a.scnt--;
                        if (a.scnt == 0) {
                            p = 0;
                            return a;
                        }
                    }
                    a.a[xx][yy] = ‘.‘;
                    p = 1;
                    return a;
                }
            }

            for (int i = 0; i < 4; ++i) {
                int px = xx + dx[i];
                int py = yy + dy[i];
                if (!check(px, py))
                    continue;
                if (a.a[px][py] == ‘N‘) {
                    if (c == ‘S‘) {
                        p = 2;
                        return a;
                    }
                    p = 0;
                    return a;
                }
            }
        }
        x = xx;
        y = yy;
    }
    return a;
}
int d[MAXN];
int bfs(ULL st) {
    int tail = 0;
    d[0] = 0;
    q[tail++] = st;
    mp.clear();
    mp[st] = 1;
    for (int i = 0; i < tail; ++i) {
        node a = node(q[i]);
        REP(j,6)
            REP(k,8)
            {
                if (a.a[j][k] == ‘S‘ || a.a[j][k] == ‘M‘ || a.a[j][k] == ‘P‘) {
                    for (int x = 0; x < 4; ++x) {
                        int p;
                        node e = move(a, j, k, dx[x], dy[x], p);
                        if (p == 2) {
                            return d[i] + 1;
                        }
                        if (p == 1) {
                            ULL hs = geths(e.a);

                            if (mp.find(hs) == mp.end()) {
                                mp[hs] = 1;
                                q[tail] = hs;
                                d[tail++] = d[i] + 1;
                            }
                        }
                    }
                }
            }
    }
    printf("tail:%d\n",tail);
    return -1;
}

int main() {
//    freopen("1.txt","w",stdout);
    bas[0] = 1;
    for (int i = 1; i <= 64; ++i)
        bas[i] = bas[i - 1] * 2;
    while(scanf(" %s",s[0])!=EOF){
        for(int i=1;i<6;++i)scanf(" %s",s[i]);
        init();
        ULL hs=geths(s);
        REP(i,6)REP(j,8)if(s[i][j]==‘S‘||s[i][j]==‘M‘||s[i][j]==‘P‘)s[i][j]=‘.‘;
        int ans=bfs(hs);
        printf("%d\n",ans);
    }
    return 0;
}

HDU 5010 Get the Nut(2014 ACM/ICPC Asia Regional Xi'an Online)

时间: 2024-10-14 12:12:33

HDU 5010 Get the Nut(2014 ACM/ICPC Asia Regional Xi'an Online)的相关文章

HDU 5014 Number Sequence 贪心 2014 ACM/ICPC Asia Regional Xi&#39;an Online

尽可能凑2^x-1 #include <cstdio> #include <cstring> const int N = 100005; int a[N], p[N]; int init(int x) { int cnt = 0; while(x > 1) { x /= 2; cnt ++; } return cnt + 1; } int main() { int n; while(~scanf("%d", &n)){ for(int i = 0;

HDU 5014 Number Sequence(2014 ACM/ICPC Asia Regional Xi&#39;an Online) 题解

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5014 Number Sequence Problem Description There is a special number sequence which has n+1 integers. For each number in sequence, we have two rules: ● ai ∈ [0,n] ● ai ≠ aj( i ≠ j ) For sequence a and sequ

hdu 5008(2014 ACM/ICPC Asia Regional Xi&#39;an Online ) Boring String Problem(后缀数组&amp;二分)

Boring String Problem Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 219    Accepted Submission(s): 45 Problem Description In this problem, you are given a string s and q queries. For each que

2014 ACM/ICPC Asia Regional Xi&#39;an Online(HDU 5007 ~ HDU 5017)

题目链接 A题:(字符串查找,水题) 题意 :输入字符串,如果字符串中包含“ Apple”, “iPhone”, “iPod”, “iPad” 就输出 “MAI MAI MAI!”,如果出现 “Sony” 就输出“SONY DAFA IS GOOD!” ,大小写敏感. 思路 : 字符串查找,水题. 1 #include <string.h> 2 #include <stdio.h> 3 #include <iostream> 4 5 using namespace st

hdu 5016 点分治(2014 ACM/ICPC Asia Regional Xi&#39;an Online)

Mart Master II Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 675    Accepted Submission(s): 237 Problem Description Trader Dogy lives in city S, which consists of n districts. There are n - 1

2014 ACM/ICPC Asia Regional Xi&#39;an Online 233 Matrix,hdu 5015

比赛的时候若是这题过了就进前50 刚开始的时候大家的思路都以为是找规律的题目,于是再推公式,此外还发现类似于杨辉三角.于是又去套杨辉三角的通项去求. 于是TLE了无数次.(每次取范围的最大值也要3s多). 对于明显的矩阵样子,其实可以转化为矩阵的运算,每一行的转移.就是对一个转移矩阵的幂运算.然后再用快速矩阵幂即可. A: 10 0 0 1 10 1 0 1 10 1 1 1 0  0  0 1 B: 23 0 0 3 C=A^M  *B,ans=C[N] 教训:对于时间限制,即便是最大数据也要

2014 ACM/ICPC Asia Regional Xi&#39;an Online

03 hdu5009 状态转移方程很好想,dp[i] = min(dp[j]+o[j~i]^2,dp[i]) ,o[j~i]表示从j到i颜色的种数. 普通的O(n*n)是会超时的,可以想到o[]最大为sqrt(n),问题是怎么快速找到从i开始往前2种颜色.三种.四种...o[]种的位置. 离散化之后,可以边走边记录某个数最后一个出现的位置,初始为-1,而所要求的位置就等于 if(last[a[i]]==-1) 该数没有出现过,num[i][1] = i,num[i][j+1] = num[i-1

HDU 5000 Clone 规律+dp 2014 ACM/ICPC Asia Regional Anshan Online

每只羊有n个属性 下面n个数字表示每个属性的值范围为[ 0, T[i] ] 对于羊圈里的a羊和b羊,若a羊的每个属性都>=b羊,则a羊会杀死b羊. 问羊圈里最多存活多少只羊. 规律1:sum相同的羊不会互相杀死. 因为若2个羊的属性都相同,a羊某个属性要增加1,则a羊另一个属性要减少1,这样ab一定能共存. 规律2: sum不同的羊不会重合. 我们设a羊sum = x,b羊sum = y,若a,b羊能共存,但不会把ab同时放到羊圈里. 因为一定存在一只羊c ,sum = x,且c和b不能共存,既

HDU 5003 Osu! 水题 2014 ACM/ICPC Asia Regional Anshan Online

水.. #include <iostream> #include <cstdio> #include <string.h> #include <queue> #include <vector> #include <algorithm> #include <set> using namespace std; #define N 100 double a[N]; bool cmp(double x, double y){ re