思路:广搜, 因为空格加上动物最多只有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[] = { , , , - };
int dy[] = { , -, , };
char s[][];
int id[][];
int qx[];
int qy[];
int qcnt;
ULL q[MAXN];
ULL bas[];
struct node {
char a[][];
int scnt;
node() {
}
;
node(ULL now) {
scnt = ;
REP(i,)
REP(j,)
a[i][j] = s[i][j];
REP(i,qcnt)
{
ULL k = now & ;
now >>= ;
if (k == )
continue;
if (k == ) {
a[qx[i]][qy[i]] = 'S';
scnt++;
}
if (k == )
a[qx[i]][qy[i]] = 'M';
if (k == )
a[qx[i]][qy[i]] = 'P';
}
} void debug(){
puts("-------");
REP(i,)
{
REP(j,)putchar(a[i][j]);
puts("");
}
puts("------------------");
}
}; void init() {
qcnt = ;
int cid = ;
memset(id, -, sizeof(id));
REP(i,)
REP(j,)
{
if (s[i][j] == '#' || s[i][j] == 'N')
continue;
qx[qcnt] = i;
qy[qcnt++] = j;
id[i][j] = cid++;
}
} ULL geths(char s[][]) {
ULL ans = ;
REP(i,)
REP(j,)
{
if (s[i][j] == 'S') {
ans += bas[id[i][j] << ];
continue;
}
if (s[i][j] == 'M') {
ans += * bas[id[i][j] << ];
continue;
}
if (s[i][j] == 'P') {
ans += * bas[id[i][j] << ];
}
}
return ans;
} bool check(int x, int y) {
if (x < || x >= || y < || y >= )
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 = ;
return a;
}
a.a[xx][yy] = a.a[x][y];
a.a[x][y] = '.';
if (c == 'P') {
int flag=;
for (int j = ; j < ; ++j) {
int px = xx + dx[j];
int py = yy + dy[j];
if (!check(px, py))
continue;
if (a.a[px][py] == 'N') {
p = ;
return a;
}
if (a.a[px][py] == 'S' || a.a[px][py] == 'M') {
if (a.a[px][py] == 'S') {
a.scnt--;
if (a.scnt == ) {
p = ;
return a;
}
}
a.a[px][py] = '.';
flag=;
}
}
if(flag)
{
p=;
return a;
}
} else {
for (int i = ; i < ; ++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 == ) {
p = ;
return a;
}
}
a.a[xx][yy] = '.';
p = ;
return a;
}
} for (int i = ; i < ; ++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 = ;
return a;
}
p = ;
return a;
}
}
}
x = xx;
y = yy;
}
return a;
}
int d[MAXN];
int bfs(ULL st) {
int tail = ;
d[] = ;
q[tail++] = st;
mp.clear();
mp[st] = ;
for (int i = ; i < tail; ++i) {
node a = node(q[i]);
REP(j,)
REP(k,)
{
if (a.a[j][k] == 'S' || a.a[j][k] == 'M' || a.a[j][k] == 'P') {
for (int x = ; x < ; ++x) {
int p;
node e = move(a, j, k, dx[x], dy[x], p);
if (p == ) {
return d[i] + ;
}
if (p == ) {
ULL hs = geths(e.a); if (mp.find(hs) == mp.end()) {
mp[hs] = ;
q[tail] = hs;
d[tail++] = d[i] + ;
}
}
}
}
}
}
printf("tail:%d\n",tail);
return -;
} int main() {
// freopen("1.txt","w",stdout);
bas[] = ;
for (int i = ; i <= ; ++i)
bas[i] = bas[i - ] * ;
while(scanf(" %s",s[])!=EOF){
for(int i=;i<;++i)scanf(" %s",s[i]);
init();
ULL hs=geths(s);
REP(i,)REP(j,)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 ;
}

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

  1. hdu 5016 点分治(2014 ACM/ICPC Asia Regional Xi'an Online)

    Mart Master II Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)T ...

  2. 2014 ACM/ICPC Asia Regional Xi'an Online(HDU 5007 ~ HDU 5017)

    题目链接 A题:(字符串查找,水题) 题意 :输入字符串,如果字符串中包含“ Apple”, “iPhone”, “iPod”, “iPad” 就输出 “MAI MAI MAI!”,如果出现 “Son ...

  3. 2014 ACM/ICPC Asia Regional Xi'an Online

    03 hdu5009 状态转移方程很好想,dp[i] = min(dp[j]+o[j~i]^2,dp[i]) ,o[j~i]表示从j到i颜色的种数. 普通的O(n*n)是会超时的,可以想到o[]最大为 ...

  4. 2014 ACM/ICPC Asia Regional Xi'an Online Paint Pearls

    传说的SB DP: 题目 Problem Description Lee has a string of n pearls. In the beginning, all the pearls have ...

  5. HDU 5000 2014 ACM/ICPC Asia Regional Anshan Online DP

    Clone Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/65536K (Java/Other) Total Submiss ...

  6. HDU 4291 A Short problem(2012 ACM/ICPC Asia Regional Chengdu Online)

    HDU 4291 A Short problem(2012 ACM/ICPC Asia Regional Chengdu Online) 题目链接http://acm.hdu.edu.cn/showp ...

  7. HDU 5029 Relief grain(离线+线段树+启发式合并)(2014 ACM/ICPC Asia Regional Guangzhou Online)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5029 Problem Description The soil is cracking up beca ...

  8. HDU 5002 Tree(动态树LCT)(2014 ACM/ICPC Asia Regional Anshan Online)

    Problem Description You are given a tree with N nodes which are numbered by integers 1..N. Each node ...

  9. HDU 5000 Clone(离散数学+DP)(2014 ACM/ICPC Asia Regional Anshan Online)

    Problem Description After eating food from Chernobyl, DRD got a super power: he could clone himself ...

随机推荐

  1. 数据库:django ORM如何处理N+1查询

    数据库N+1查询是个常见的问题,简单描述场景如下 基本场景 class Category(models.Model): name = models.CharField(max_length=30) c ...

  2. Openstack+Kubernetes+Docker微服务实践之路--服务发布

    结合上文,我们的服务已经可以正常运行了,但它的访问方式只能通过服务器IP加上端口来访问,如何通过域名的方式来访问到我们服务,本来想使用Kubernetes的Ingress来做,折腾一天感觉比较麻烦,I ...

  3. 使用react做的聊天对话列表

    刚刚接触react   可能写的地方有错误或者不完善的地方欢迎大家给指正 下面开始正题 首先分析页面基于react的特性--把页面中所有显示内容看做模块加载 本页面可以看做一个大的模块,我们暂且命名为 ...

  4. 关于json-p

    关于json-p 目录 关于json-p json-p是什么 json-p原理分析 json-p的缺点 json-p是什么 json-p实际上是一种跨域ajax发送http请求的方法,它不是什么全新的 ...

  5. hdu4273Rescue(三维凸包重心)

    链接 模板题已不叫题.. 三维凸包+凸包重心+点到平面距离(体积/点积)  体积-->混合积(先点乘再叉乘) #include <iostream> #include<cstd ...

  6. 参考__Linux

    教程 billie66.github.iocentos下配置vsftpd虚拟用户教程Linux命令大全ubuntu14.04 配置vsftp 实用技能 移动 Ubuntu16.04 桌面左侧的启动器到 ...

  7. iOS开发 二维码生成

    基于libqrencode的二维码生成 + (void)drawQRCode:(QRcode *)code context:(CGContextRef)ctx size:(CGFloat)size { ...

  8. iOS开发 贝塞尔曲线

    iOS开发 贝塞尔曲线UIBezierPath - 陌云 时间 2014-03-14 11:04:00  博客园-所有随笔区 原文  http://www.cnblogs.com/moyunmo/p/ ...

  9. navigation controller

    一.程序框架 1.程序结构

  10. nodejs&npm等概念梳理

    nodejs node node版本 npm nvmw\gnvm等多版本管理 CommonJS.AMD.requirejs grunt.gulp package.json .npmrc npm\nod ...