这道题思路还是比较清晰的,建图加bfs或双向bfs,其实后者比前者少了将近一半的时间。。

建图可以把某一点所拥有邻接点长度(数目)记录在数组0这个位置,因为这道题使用vector会超时。

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <queue>
#include <map>
using namespace std;
char a[][];
int nex[][]= {,,,,-,,,-};
int book[][],top,book1[];
int w,h,n;
int sta[],ent[];
int mat[][];
inline bool is_inrage(int x,int y)
{
if(x<||x>=h||y<||y>=w) return false;
return true;
}
int findid(int x,int y)
{
if(book[x][y]) return book[x][y];
book[x][y]=++top;
return book[x][y];
}
struct note
{
int status[];
int length;
int step;
note()
{
status[]=status[]=status[]=;
}
};
short vis1[][][],vis2[][][];
bool invilid(int prea,int preb,int nowa,int nowb)
{
if(nowa==nowb||(prea==nowb&&preb==nowa)) return true;
return false;
}
int bfs(int len)
{
memset(vis1,,sizeof(vis1));
memset(vis2,,sizeof(vis2));
queue<note>que1,que2;
note start,ends;
ends.length=start.length=len;
ends.step=start.step=;
for(int i=; i<len; i++)
{
start.status[i]=sta[i];
ends.status[i]=ent[i];
}
que1.push(start);
que2.push(ends);
vis1[start.status[]][start.status[]][start.status[]]=;
vis2[ends.status[]][ends.status[]][ends.status[]]=;
int ac[],flag;
struct note u1,v1,u2,v2;
while(!que1.empty()||!que2.empty())
{
if(!que1.empty())
{
u1=que1.front();
que1.pop();
for(int i=; i<=mat[u1.status[]][]; i++)
{
ac[]=mat[u1.status[]][i];
for(int j=; j<=mat[u1.status[]][]; j++)
{
ac[]=mat[u1.status[]][j];
if(invilid(u1.status[],u1.status[],ac[],ac[])) continue;
for(int h=; h<=mat[u1.status[]][]; h++)
{
ac[]=mat[u1.status[]][h];
if(len==) if(invilid(u1.status[],u1.status[],ac[],ac[])) continue;
if(len==) if(invilid(u1.status[],u1.status[],ac[],ac[])) continue;
v1.step=u1.step+;
v1.length=u1.length;
v1.status[]=ac[];
v1.status[]=ac[];
v1.status[]=ac[];
if(vis1[v1.status[]][v1.status[]][v1.status[]]) continue;
vis1[v1.status[]][v1.status[]][v1.status[]]=v1.step;
que1.push(v1);
int a1,b1,c1;
a1=v1.status[];b1=v1.status[];c1=v1.status[];
if(vis2[a1][b1][c1]) return vis1[a1][b1][c1]+vis2[a1][b1][c1];
}
}
}
}
if(!que2.empty())
{
u2=que2.front();
que2.pop();
for(int i=; i<=mat[u2.status[]][]; i++)
{
ac[]=mat[u2.status[]][i];
for(int j=; j<=mat[u2.status[]][]; j++)
{
ac[]=mat[u2.status[]][j];
if(invilid(u2.status[],u2.status[],ac[],ac[])) continue;
for(int h=; h<=mat[u2.status[]][]; h++)
{
ac[]=mat[u2.status[]][h];
if(len==) if(invilid(u2.status[],u2.status[],ac[],ac[])) continue;
if(len==) if(invilid(u2.status[],u2.status[],ac[],ac[])) continue;
v2.step=u2.step+;
v2.length=u2.length;
v2.status[]=ac[];
v2.status[]=ac[];
v2.status[]=ac[];
if(vis2[v2.status[]][v2.status[]][v2.status[]]) continue;
vis2[v2.status[]][v2.status[]][v2.status[]]=v2.step;
que2.push(v2);
int a2,b2,c2;
a2=v2.status[];b2=v2.status[];c2=v2.status[];
if(vis1[a2][b2][c2]) return vis1[a2][b2][c2]+vis2[a2][b2][c2];
}
}
}
}
}
return -;
}
struct node
{
int x,y;
char v;
node() {}
node(int xx,int yy,char vv):x(xx),y(yy),v(vv) {}
bool operator < (const node &another) const
{
return v<another.v;
}
};
int main()
{
while(scanf("%d%d%d",&w,&h,&n),w+h+n)
{
char ch[];
memset(book,,sizeof(book));
node t1[],t2[];
int s1=,s2=;
memset(mat,,sizeof(mat));
getchar();
for(int i=; i<h; i++)
{
gets(ch);
for(int j=; j<w; j++)
{
a[i][j]=ch[j];
if('a'<=ch[j]&&ch[j]<='z') t1[s1++]=node(i,j,ch[j]);
if('A'<=ch[j]&&ch[j]<='Z') t2[s2++]=node(i,j,ch[j]);
}
}
int nx,ny,idpre,idnex;
top=;
for(int i=; i<h; i++)
{
for(int j=; j<w; j++)
{
for(int k=; k<; k++)
{
if(a[i][j]=='#') continue;
nx=i+nex[k][];
ny=j+nex[k][];
if(is_inrage(nx,ny)&&a[nx][ny]!='#')
{
idpre=findid(i,j);
idnex=findid(nx,ny);
int lenth=++mat[idpre][];
mat[idpre][lenth]=idnex;
}
}
}
}
for(int i=; i<=top; i++)
{
++mat[i][];
mat[i][mat[i][]]=i;
}
sort(t1,t1+n);
sort(t2,t2+n);
for(int i=; i<n; i++)
{
sta[i]=findid(t1[i].x,t1[i].y);
ent[i]=findid(t2[i].x,t2[i].y);
// cout<<sta[i]<<" "<<ent[i]<<endl;
}
printf("%d\n",bfs(n));
}
return ;
}

The Morning after Halloween uva1601的更多相关文章

  1. 7-9The Morning after Halloween uva1601

    这题可以用普通bfs来做  也可以用双向bfs来做(先欠着) 有点类似专题训练的一题   不过那题是找钥匙开门   不过都用了状态压缩 题意:  n,m(<=16) 的网络上有t(<=3) ...

  2. UVA1601 The Morning afther Halloween

    题目大意 w h (w, h <= 16)的网格有 n ( n <= 3) 个小写字母(代表鬼)其余的是‘#’(代表障碍格) 或 ‘ ’(代表空格. 要求把他们移动到对应的大写字母里.每步 ...

  3. 【例题 7-9 UVA-1601】The Morning after Halloween

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 对于没有出现的,当成0节点就好. 所以总是认为有3个人需要走到各自的终点. 将平面图转成点边图.这样比较好枚举. (二维变成一维,模 ...

  4. UVa1601 - The Morning after Halloween [单向bfs]

    解题思路: 1.注意到2*2方格中必有一个#,那么最多只有192条通道,可以将所有非‘#’的位置提取出来用邻接表的方式建图,通过bfs搜索目标位置. 2.将三个ghost的位置(a,b,c)作为状态量 ...

  5. UVa 1601 || POJ 3523 The Morning after Halloween (BFS || 双向BFS && 降维 && 状压)

    题意 :w*h(w,h≤16)网格上有n(n≤3)个小写字母(代表鬼).要求把它们分别移动到对应的大写字母里.每步可以有多个鬼同时移动(均为往上下左右4个方向之一移动),但每步结束之后任何两个鬼不能占 ...

  6. POJ 3370. Halloween treats 抽屉原理 / 鸽巢原理

    Halloween treats Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7644   Accepted: 2798 ...

  7. Lightoj 题目1422 - Halloween Costumes(区间DP)

    1422 - Halloween Costumes   PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 ...

  8. CSUFT 1004 This is Halloween: Saving Money

    1004: This is Halloween: Saving Money Time Limit: 1 Sec      Memory Limit: 128 MB Submit: 11      So ...

  9. [POJ 3370] Halloween treats

    Halloween treats Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7143   Accepted: 2641 ...

随机推荐

  1. java多线程——饥饿和公平

    一.概念 饥饿:如果一个线程因为 CPU 时间全部被其他线程抢走而得不到 CPU 运行时间,这种状态被称之为“饥饿”: 二.饥饿原因 高优先级线程吞噬所有的低优先级线程的 CPU 时间. 线程被永久堵 ...

  2. HDU 5862Counting Intersections

    Counting Intersections Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/ ...

  3. bzoj3661

    网络流/贪心 网络流做法是对于每一列,如果一个兔子下一天继续可以存在,那么连一条容量为1的边,然后设立一个中转站,来控制可以换的数量,容量限制l.时限100s,能跑过去我的太慢了,一个点100s 正解 ...

  4. Windows7安装SQLServer 2008图解

    Windows7安装SQL Server 2008图解        这几天因为需要,一直想安装SQL Server 2008来作为Web后台的数据库进行些实验,但总是没有时间,今天终于有时间了,便安 ...

  5. E20170627-hm

    confirmation   n. 证实; 证明; 确认,

  6. HTML--文本输入框、密码输入框

    当用户要在表单中键入字母.数字等内容时,就会用到文本输入框.文本框也可以转化为密码输入框. 语法: <form> <input type="text/password&qu ...

  7. UNIX环境高级编程--2

    UNIX标准及实现 ISO C: 国际标准化组织(International Organization for standardization , ISO)ISO C标准的意图是提供C程序的可移植性, ...

  8. Elasticsearch的索引模块(正排索引、倒排索引、索引分析模块Analyzer、索引和搜索、停用词、中文分词器)

    正向索引的结构如下: “文档1”的ID > 单词1:出现次数,出现位置列表:单词2:出现次数,出现位置列表:…………. “文档2”的ID > 此文档出现的关键词列表. 一般是通过key,去 ...

  9. T-SQL查询基础

    今天来带大家了解下在sql server 中的查询机制 使用select语句进行查询 1.查询所有的数据行和列 select * from student 2.查询部分行和列 select scode ...

  10. vue项目杂记

    vue项目杂记 文件目录结构 src main.js app.vue package.json webpack_config_dev.js 需要安装的包 1. vue cnpm i vue --sav ...