思路:广搜, 因为空格加上动物最多只有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. java模式-工厂模式

    今天在学习工厂模式,从最简单的简单工厂模式开始. 我们现在需要通过工厂Factory生产A,B两款产品(都是产品,实现了接口Product). 产品A: public class A implemen ...

  2. 【翻译】利用Qt设计师窗体在运行时创建用户界面(Creating a user interface from a Qt Designer form at run-time)

    利用Qt设计师窗体在运行时创建用户界面 我们利用Calculator窗体例子中创建的窗体(Form)来展示当一个应用(application)已经生成后,是可以在其运行时产生与例子中相同的用户界面. ...

  3. xcopy中提示“无效的参数数量”的解决方法

    原因是DOS下不支持长文件名,只支持8.3格式的文件名 .如果是Windows下的命令行,对于有空格的命令行要加引号.应该是 copy "c:\program files" &qu ...

  4. hdu-5525 Product(费马小定理)

    题目来源:http://bestcoder.hdu.edu.cn/contests/contest_chineseproblem.php?cid=644&pid=1003 前面用奇偶性约掉2, ...

  5. Linux英文全称

    su:Swith user  切换用户,切换到root用户cat: Concatenate  串联uname: Unix name  系统名称df: Disk free  空余硬盘du: Disk u ...

  6. js获取当前日期

    var myDate = new Date();myDate.getYear();        //获取当前年份(2位)myDate.getFullYear();    //获取完整的年份(4位,1 ...

  7. 平衡二叉树AVL

    1.定义 平衡二叉树(Balanced Binary Tree)是二叉查找树的一个改进,也是第一个引入平衡概念的二叉树.1962年,G.M. Adelson-Velsky 和 E.M. Landis发 ...

  8. php使用PDO连接mysql数据库

    <?php $dsn='mysql:host=localhost;dbname=mssc'; $user='root'; $password=''; $status=1; try { $sql= ...

  9. 重写toString()方法来描述一个类

    package com.zch.test; /* toString方法以及重写toString方法 toString方法是一个自我描述方法 方法本身返回的是该对象的实现类的 类名 + @ + hash ...

  10. 建立eureka服务和客户端(客户端获取已经注册服务)

    1. 新建sping boot eureka server 新建立spring starter  project 修改pom.xml文件 在parent后追加 <dependencyManage ...