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[] = { , , , - };
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)的更多相关文章
- 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 ...
- 2014 ACM/ICPC Asia Regional Xi'an Online(HDU 5007 ~ HDU 5017)
题目链接 A题:(字符串查找,水题) 题意 :输入字符串,如果字符串中包含“ Apple”, “iPhone”, “iPod”, “iPad” 就输出 “MAI MAI MAI!”,如果出现 “Son ...
- 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[]最大为 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- linux 脚本小试系列
实现100以内的奇数和和偶数和的脚本 1 #!/bin/bash 2 # #声明一个偶数变量和一个奇数变量 3 declare -i evensum=0 4 declare -i oddsum=0 # ...
- UVA 11464 偶数矩阵
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- 最全面的常用正则表达式大全 zz
很多不太懂正则的朋友,在遇到需要用正则校验数据时,往往是在网上去找很久,结果找来的还是不很符合要求.所以我最近把开发中常用的一些正则表达式整理了一下,包括校验数字.字符.一些特殊的需求等等.给自己留个 ...
- mysql查询一个小知识点,查询结果是空与查询出错是不一样的
$conn = new mysqli(....); $sql = ""; $query = $conn->query($sql); 这里,如果查询正常,有数据返回,那么$qu ...
- 什么是原生的javascript
在www.cocos.com的cocos2d-js的介绍中写道“Cocos2d-JS 是跨全平台的游戏引擎,采用原生JavaScript语言,可发布到包括Web平台,iOS,Android,Windo ...
- ASP.NET MVC 静态资源打包和压缩问题小记
ASP.NET MVC 中有个 BundleConfig 用于静态资源的打包和压缩,我在使用的过程中遇到一些问题,现在做下总结,并给出具体的解决方案. 问题一:打包压缩后的 JavaScript 和 ...
- Lucene 简单API使用
本demo 简单模拟实现一个图书搜索功能. 模拟向数据库添加数据的时候,添加书籍索引. 提供搜索接口,支持按照书名,作者,内容进行搜索. 按默认规则排序返回搜索结果. Jar依赖: <prope ...
- linux应用程序开发-进程通信(IPC)
IPC why: 1.数据传输 2.资源共享 目的: 3.通知事件 4.进程控制 发展: 1.UNIX进程间通信 2.基于SYStem V 3.POSIX 方式分类: 1.pipe(管道) FIFO( ...
- loadrunner json
Action(){ web_custom_request("JRPT_WriteLog", //VuGen中树形视图中显示的名称 "Url=url", //请求 ...
- MySQL DML 整理
DML(Data Manipulation Language)数据操纵语言statements are used for managing data within schema objects. 由D ...