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 ...
随机推荐
- Boost学习笔记(三) progress_timer
progress_timer也是一个计时器,它继承自timer,会在析构时自动输出时间,省去timer手动调用elapsed()的工作,是一个用于自动计时相当方便的小工具. #include < ...
- python 初级1
List:Python内置的一种数据类型是列表:list.list是一种有序的集合,可以随时添加和删除其中的元素. 构造list非常简单,按照上面的代码,直接用 [ ] 把list的所有元素都括起来, ...
- Cheatsheet: 2016 04.01 ~ 04.30
.NET String format Setting up Ubuntu for .NET Development ASP.NET Core and Angular2 - Part 1 - Upda ...
- [Python]简易terminal歌词滚动播放器
整合了网易云的一些API,想写一个terminal版的音乐播放器,但是还没有想好写成什么样子. 暂时写了一个必须又的功能:带歌词滚动的播放器,用了pygame里的mixer来播放音乐. 准备有时间的时 ...
- margin塌陷现象
如果两个盒子是包含关系,如果让子盒子在父盒子之内向下平移100px:(margin塌陷现象)解决方案: padding , border , overflow <!DOCTYPE html> ...
- Query Designer:Hierarchy层级显示
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
- BLE编程中关键步骤
获取权限 <uses-permission android:name="android.permission.BLUETOOTH"/> <uses-permiss ...
- asp.net 一句话搞定分页
rows 是客户端传过来的行数,page是页码,传参就需要就两个参数就行,sql语句中_row 和_page 自己声明的局部变量,值还是相应的row 和page ,为了运算而已. 用数据库类获得它的D ...
- 搭建一个简单的Struts2(Struts2_HelloWorld)
1.导入Jar包 2.配置web.xml 1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-ap ...
- Python之路 day2 字符串/元组/列表/字典互转
#-*-coding:utf-8-*- #1.字典 dict = {'name': 'Zara', 'age': 7, 'class': 'First'} #字典转为字符串,返回:<type ' ...