Uva 1061 The Morning after Halloween
基本思路是BFS:
1. 题目中已经说了,每相连的2X2格子中必有一个‘#’,也就是,每个点周围最多也就三个方向可以走。因此,可以把所有空格都提出来,形成一个图,直接遍历每条边,而不是每次判断4个方向是否可以走
2.关于结点判重,最初的想法是想用一个六维数组,后来参考了其它,发现其实可以用一个三维数字代替,每个点可以用数字代替,因为题目中整个图最多为16X16,所以数字最大为16*16 = 256,这样做的另一个好处是,移动字母也方便了,可以用数字代替点,比如从(0, 0)移动到(0,1)可以考做是从 0 移动到 1
3.N的数量不一,移动时要不要判断是N是多少?这是我刚开始的想法,个人采用递归的方法,先放一个点,再放下一个点,观察这是不是最后一个要移动的点,相对而言,比对每个N值进行单独处理要简单点。
注意点:题目输入的W,H,N中W是指宽度,即列数,而H即为行数。
收获及感悟:刚开始看到这题的时候已经吓着了,首先就是如何保存三个点状态,真的差点用六维数组或map了。在这里也学了一招,用整数依次标记每个点。另外,关于题意,刚开始理解不透,还在想a,b,c三者的先后移动顺序,后来发现想多了,任意顺序移动,判断移动后的状态是否可行即可。这题是在看了别人代码后才A掉的,非常感谢发布题解的同学。
参考资料:
1.http://blog.csdn.net/acm_hkbu/article/details/42420503
2.《算法竞赛入门经典(第2版)》
#include <bits/stdc++.h> using namespace std; const int MAXN = 16 + 5;
char plan[MAXN][MAXN];
int W, H, N;
vector<int> link[MAXN*MAXN];
bool vis[MAXN*MAXN][MAXN*MAXN][MAXN*MAXN];
int dir[4][2] = {0,1, 0,-1, 1,0, -1,0}; struct State{
int ghostPos[5], step;
}; State finalState, firstState; // more than one ghost occupy a same position
// p is the prev state, whether two ghots in s exchange with their position
bool isCollisionOrExChange(State& s, State& p) {
for(int i=0; i<N; ++i) {
for(int j=0; j<N; ++j) {
if(i!=j && s.ghostPos[i] == s.ghostPos[j]) {
return true;
}
if(i!=j && s.ghostPos[i] == p.ghostPos[j] && s.ghostPos[j] == p.ghostPos[i]) {
return true;
}
}
}
return false;
} void Read() {
for(int i=0; i<H; ++i) {
cin.get();
for(int j=0; j<W; ++j) {
plan[i][j] = cin.get();
}
}
} int GetVisValue(int x, int y) {
return x * W + y;
} void SetVis(State& t, bool flag) {
int i, a[3]; // the positon of each ghost
for(i=0; i<N; ++i) {
a[i] = t.ghostPos[i];
}
// the number of ghosts may be less than 3
for(; i<3; ++i) {
a[i] = 0;
}
vis[a[0]][a[1]][a[2]] = flag;
} bool IsVis(State& t) {
int i, a[3]; // the positon of each ghost
for(i=0; i<N; ++i) {
a[i] = t.ghostPos[i];
}
// the number of ghosts may be less than 3
for(; i<3; ++i) {
a[i] = 0;
}
return vis[a[0]][a[1]][a[2]];
} bool IsFinal(State& t) {
for(int i=0; i<N; ++i) {
if(finalState.ghostPos[i] != t.ghostPos[i]) {
return false;
}
}
return true;
} // the ghost numbered g move
void NextState(State& s, int g, queue<State>& q, State& former) {
for(size_t i=0; i<link[former.ghostPos[g]].size(); ++ i) {
s.ghostPos[g] = link[former.ghostPos[g]][i];
if(g == N-1 && !IsVis(s) && !isCollisionOrExChange(s, former)) {
SetVis(s, true);
q.push(s);
} else if (g < N-1) {
NextState(s, g+1, q, former);
}
}
} // output the point of all ghosts
void Test(State& t) {
for(int i=0; i<N; ++i) {
cout << t.ghostPos[i] / W << " " << t.ghostPos[i] % W << endl;
}
cout << t.step << endl;
cout << endl;
} int Bfs() {
memset(vis, false, sizeof(vis));
queue<State> q;
firstState.step = 0;
q.push(firstState);
SetVis(firstState, true);
while(!q.empty()) {
State t = q.front();
q.pop();
// Test(t);
if(IsFinal(t)) {
return t.step;
}
State newS;
newS.step = t.step + 1;
NextState(newS, 0, q, t);
}
return -1;
} // Make a Graph
void MakeLink() {
for(int i=0; i<H; ++ i) {
for(int j=0; j<W; ++ j) {
if(plan[i][j] != '#') {
// the position means unmoving
link[GetVisValue(i, j)].clear();
link[GetVisValue(i, j)].push_back(GetVisValue(i, j));
for(int k=0; k<4; ++k) { // four directions
int x = i + dir[k][0];
int y = j + dir[k][1];
if(x>=0 && x<H && y>=0 && y<W && plan[x][y]!='#') {
link[GetVisValue(i, j)].push_back(GetVisValue(x, y));
}
}
}
if (isupper(plan[i][j])) {
finalState.ghostPos[plan[i][j]-'A'] = GetVisValue(i, j);
} else if (islower(plan[i][j])) {
firstState.ghostPos[plan[i][j]-'a'] = GetVisValue(i, j);
}
}
}
} void Work() {
Read();
MakeLink();
cout << Bfs() << endl;
} int main() {
ios::sync_with_stdio(false);
cin.tie(0);
while(cin >> W >> H >> N) {
if(!W && !H && !N) {
break;
}
Work();
}
return 0;
}
Uva 1061 The Morning after Halloween的更多相关文章
- [uva P1601] The Morning after Halloween
[uva P1601] The Morning after Halloween 题目链接 非常经典的一道题目,lrj的书上也有(貌似是紫书?). 其实这题看起来就比较麻烦.. 首先要保证小鬼不能相遇, ...
- UVA 1601 The Morning after Halloween
题意: 给出一个最大为16×16的迷宫图和至多3个ghost的起始位置和目标位置,求最少经过几轮移动可以使三个ghost都到达目标位置.每轮移动中,每个ghost可以走一步,也可以原地不动,需要注意的 ...
- UVA - 1601 The Morning after Halloween (BFS/双向BFS/A*)
题目链接 挺有意思但是代码巨恶心的一道最短路搜索题. 因为图中的结点太多,应当首先考虑把隐式图转化成显式图,即对地图中可以相互连通的点之间连边,建立一个新图(由于每步不需要每个鬼都移动,所以每个点需要 ...
- UVA - 1601 The Morning after Halloween (双向BFS&单向BFS)
题目: w*h(w,h≤16)网格上有n(n≤3)个小写字母(代表鬼).要求把它们分别移动到对应的大写字母里.每步可以有多个鬼同时移动(均为往上下左右4个方向之一移动),但每步结束之后任何两个鬼不能占 ...
- <<操作,&0xff以及|的巧妙运用(以POJ3523---The Morning after Halloween(UVa 1601)为例)
<<表示左移,如a<<1表示将a的二进制左移一位,加一个0,&0xff表示取最后8个字节,如a&0xff表示取a表示的二进制中最后8个数字组成一个新的二进制数, ...
- uva 11237 - Halloween treats(抽屉原理)
版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/u011328934/article/details/37612503 题目链接:uva 11237 ...
- UVA 11237 - Halloween treats(鸽笼原理)
11237 - Halloween treats option=com_onlinejudge&Itemid=8&page=show_problem&category=516& ...
- UVa 1601 || POJ 3523 The Morning after Halloween (BFS || 双向BFS && 降维 && 状压)
题意 :w*h(w,h≤16)网格上有n(n≤3)个小写字母(代表鬼).要求把它们分别移动到对应的大写字母里.每步可以有多个鬼同时移动(均为往上下左右4个方向之一移动),但每步结束之后任何两个鬼不能占 ...
- 【UVa】1601 The Morning after Halloween(双向bfs)
题目 题目 分析 双向bfs,对着书打的,我还调了好久. 代码 #include<cstdio> #include<cstring> #include<c ...
随机推荐
- FPGA STA(静态时序分析)
1 FPGA设计过程中所遇到的路径有输入到触发器,触发器到触发器,触发器到输出,例如以下图所看到的: 这些路径与输入延时输出延时,建立和保持时序有关. 2. 应用背景 静态时序分析简称STA,它是一种 ...
- 标准C函数库的使用方法
本篇介绍若干经常使用的标准C函数的使用方法,主要介绍stdio(标准输入输出).math(数字函数库).time(时间函数库).stdlib(标准函数库)string(标准字符串函数)等. 最后更新 ...
- table边框不显示
今日在做报表的时候发现,最后一行隐藏后整个报表的下边框会不显示,猜测是td的边框隐藏后但table并未设置边框,导致下边框没有出现.因此设置了table边框后问题解决.table和td的边框关系如下实 ...
- Junit 测试常见错误
1. java.lang.Exception: No tests found matching Method deleteUser(mybatis.dao.Usertest) from org.jun ...
- subversion和客户端的应用
1.安装svn的服务器端subversion.以及windows客户端TortoiseSVN: 2 cmd 建立库,名字为svnpro ----- svnadmin create F:\svnpro, ...
- mysql中varchar最长多少
4.0版本以下,varchar(20),指的是20字节,如果存放UTF8汉字时,只能存6个(每个汉字3字节) 5.0版本以上,varchar(20),指的是20字符,无论存放的是数字.字母还是UTF8 ...
- 解决spark运行中failed to locate the winutils binary in the hadoop binary path的问题
1.下载hadoop-common-2.2.0-bin并解压到某个目录 https://github.com/srccodes/hadoop-common-2.2.0-bin 2.设置hadoop.h ...
- pyhton10min系列之程序员的浪漫-足迹生成器,有视频教程
记录去过的足迹 本文为原创文章 项目主页 跪求star 程序员的浪漫,我女朋友蘑菇喜欢旅游,于是我做了这个,记录2015一起去过的地方,祝她圣诞快乐 如果觉得对你有帮助,github求个star 视频 ...
- IOS 学习笔记(3) 视图UITabbarController
1.UITabbarViewController标签试图控制器.由于标签页本就起着分类的作用,所以往往呈现的视图内容之间,可以是毫不相关的功能. UITabbarViewController仍然继承自 ...
- Matplotlib中文乱码
想要分析一批数据,画出图形会比较直观.所以就搜索了一下各种软件,最终选择使用python的matplotlib.原因也是因为python使用起来比较方便,虽然R才是分析数据的首选,不过,没有R的基础, ...